栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【2021/10/3】from keras.preprocessing import sequence 报错解决

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【2021/10/3】from keras.preprocessing import sequence 报错解决

from keras.preprocessing import sequence 报错解决
  • 问题描述
  • 解决方法

问题描述

自然语言处理学习过程中,在文本长度规范代码测试时,发生报错:

from keras.preprocessing import sequence

# cutlen根据数据分析中句子长度分布,覆盖90%左右语料的最短长度.
# 这里假定cutlen为10
cutlen = 10

def padding(x_train):
    """
       description: 对输入文本张量进行长度规范
       :param x_train: 文本的张量表示, 形如: [[1, 32, 32, 61], [2, 54, 21, 7, 19]]
       :return: 进行截断补齐后的文本张量表示
       """
    # 使用sequence.pad_sequences即可完成
    return sequences(x_train, cutlen)


# 假定x_train里面有两条文本, 一条长度大于10, 一天小于10
x_train = [[1, 23, 5, 32, 55, 63, 2, 21, 78, 32, 23, 1],
           [2, 32, 1, 23, 1]]

res = padding(x_train)
print(res)
2021-10-03 19:35:29.266282: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-10-03 19:35:29.266423: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "D:/Python/PycharmProjects/PyTorch/textPreprocessing/10.文本长度规范.py", line 2, in 
    from keras.preprocessing import sequence
  File "D:Anaconda3envsPyTorchlibsite-packageskeras__init__.py", line 20, in 
    from . import initializers
  File "D:Anaconda3envsPyTorchlibsite-packageskerasinitializers__init__.py", line 124, in 
    populate_deserializable_objects()
  File "D:Anaconda3envsPyTorchlibsite-packageskerasinitializers__init__.py", line 49, in populate_deserializable_objects
    LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'

AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'

搜索后感觉是Tensorflow和Keras版本不对应的问题,重新安装仍然报错:

pip install tensorflow==2.1
pip install keras==2.3.1

百度搜索后,再次尝试!

之后修改导入代码:

from tensorflow.keras.preprocessing import sequence

再次报错:

2021-10-03 19:37:39.192743: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-10-03 19:37:39.192896: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "D:/Python/PycharmProjects/PyTorch/textPreprocessing/10.文本长度规范.py", line 25, in 
    res = padding(x_train)
  File "D:/Python/PycharmProjects/PyTorch/textPreprocessing/10.文本长度规范.py", line 18, in padding
    return sequences(x_train, cutlen)
NameError: name 'sequences' is not defined

NameError: name 'sequences' is not defined

没有该模块!

解决方法

之后经查阅发现有替代的模块使用:

from tensorflow.keras.preprocessing.sequence import pad_sequences

修改后的代码:

from tensorflow.keras.preprocessing.sequence import pad_sequences
# 忽略警告
import os
os.environ["TF_KERAS"] = '2'

# cutlen根据数据分析中句子长度分布,覆盖90%左右语料的最短长度.
# 这里假定cutlen为10
cutlen = 10

def padding(x_train):
    """
       description: 对输入文本张量进行长度规范
       :param x_train: 文本的张量表示, 形如: [[1, 32, 32, 61], [2, 54, 21, 7, 19]]
       :return: 进行截断补齐后的文本张量表示
       """
    # 使用sequence.pad_sequences即可完成
    return pad_sequences(x_train, cutlen)


# 假定x_train里面有两条文本, 一条长度大于10, 一天小于10
x_train = [[1, 23, 5, 32, 55, 63, 2, 21, 78, 32, 23, 1],
           [2, 32, 1, 23, 1]]

res = padding(x_train)
print(res)
[[ 5 32 55 63  2 21 78 32 23  1]
 [ 0  0  0  0  0  2 32  1 23  1]]

即可解决!

参考:https://www.cnblogs.com/qizhou/p/13179099.html


加油!

感谢!

努力!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号