Python中tensorflow Import使用错误集合

cannot import name ‘BatchNormalization’ from 'keras.layers.normalization’

  • 错误
    拉取一份代码,报错cannot import name ‘BatchNormalization’ from ‘keras.layers.normalization’
  • 解决
    将原导入from keras.layers.normalization import BatchNormalization
    修改为from keras.layers.normalization.batch_normalization_v1 import BatchNormalization
from keras.layers.normalization.batch_normalization_v1 import BatchNormalization

No module named ‘keras.backend.tensorflow_backend’; ‘keras.backend’ is not a package

  • 错误
    报错 No module named ‘keras.backend.tensorflow_backend’; ‘keras.backend’ is not a package
  • 解决
    from keras.backend.tensorflow_backend import set_session替换为 from keras.backend import set_session
from keras.backend import set_session

module ‘tensorflow’ has no attribute ‘ConfigProto’

  • 错误
    报错 module ‘tensorflow’ has no attribute ‘ConfigProto’
  • 解决
    使用Tensorflow 2.0会出现AttributeError: module ‘tensorflow’ has no attribute 'ConfigProto’这个问题,这个是由于现在新版本中一些1.0版本的函数被和2.0版本函数区分开的缘故,需要将tf.ConfigProto修改为tf.compat.v1.ConfigProto

Unable to open file (truncated file: eof = 18044416, sblock->base_addr = 0, stored_eof = 574710816)

  • 错误
    报错 Unable to open file (truncated file: eof = 18044416, sblock->base_addr = 0, stored_eof = 574710816)
  • 解决
    Vgg模型未下载完成,重新下载即可,如果github下载过慢,可以手动下载,下载后复制到C:\Users\Administrator.keras\models目录下即可

module ‘keras.optimizers’ has no attribute ‘SGD’

  • 错误
    报错 module ‘keras.optimizers’ has no attribute ‘SGD’
  • 解决
    修改sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)为
    sgd = optimizers.gradient_descent_v2.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
sgd = optimizers.gradient_descent_v2.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)

module ‘keras.utils’ has no attribute 'to_categorical’

  • 错误
    报错 module ‘keras.utils’ has no attribute ‘to_categorical’
  • 解决
    导入from keras.utils import np_utils,使用np_utils
    keras.utils.to_categorical修改为 np_utils.to_categorical

Import “cv2” could not be resolved

  • 错误
    报错 Import “cv2” could not be resolved
  • 解决
    pip install opencv-python
    pip install opencv-python-headless
    如果不行,那么安装相应Python对应版本的opencv
    pip install opencv_python==3.3.1.11
    如果还是不行,在setting.json中添加
    “python.analysis.extraPaths”: [
    “你的对应路径\Python36\Lib\site-packages”
    ]

AttributeError: module ‘tensorflow’ has no attribute 'config’

  • 错误
    报错 AttributeError: module ‘tensorflow’ has no attribute ‘config’
  • 解决
    config 替换为ConfigProto(tensorflow1.0改2.0代码)

FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.

  • 错误
    报错 FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
  • 解决
    安装低版本的Numpy即可
    pip install numpy==1.16.0

google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices

  • 错误
    报错 google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices
  • 解决
    直接干掉了相关代码,用cpu跑,跑起来了再说,然后就被教育了,cpu太慢了

expected predictions to have shape (5,) but got array with shape (3,)

  • 错误
    报错 google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices
  • 解决
    Dense输出类别错误
    x = Dense(3, activation=‘softmax’, name=‘predictions’)(x) # 输出类别

OSError: cannot write mode RGBA as JPEG

  • 错误
    报错 OSError: cannot write mode RGBA as JPEG
  • 解决
    RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度。而JPG不支持透明度,所以要么丢弃Alpha,要么保存为.png文件
    im=im.convert(‘RGB’)
    im.save(abs_path0+filename,format=“jpeg”,quality=80)

** 5 out of the last 5 calls to <function Model.make_predict_function..predict_function at 0x00000000178B3798> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. **

  • 错误
    官方原因:
    This warning occurs when a TF function is retraced because its arguments change in shape or dtype (for Tensors) or even in value (Python or np objects or variables).
    当一个 TF 函数被回溯时会出现这个警告,因为它的参数在形状或 dtype(对于张量)甚至值(Python 或 np 对象或变量)上发生了变化.
    解决方案是1.循环中使用,2使用不重复的参数,3使用宽松灵活的参数,对这个地方理解不是很明白,但是用下面的代码确实可以解决这个警告。
  • 解决
    model.predict(image)改成model(X_test)

’NoneType’ object has no attribute 'reshape’

  • 错误
    在图像分类前期进行图像处理时报错,原因是文件中有中文路径,看了一下果然有一个文件为中文名称
  • 解决
    修改中文名称

OSError: SavedModel file does not exist at: ./model/.hdf5{saved_model.pbtxt|saved_model.pb}

  • 错误
    在flask中加载训练完成的模型时报错,使用了相对路径
  • 解决
    修改中文名称
    修改相对路径为绝对路径
  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 文本生成是一种人工智能技术,可以使用给定的模型和训练数据,自动生成新的文本内容。在 Python ,可以使用多种工具和库来实现文本生成。 下面是一个使用 Python 和 GPT-3 的文本生成示例代码: ``` import openai # 设置 API 密钥 openai.api_key = "YOUR_API_KEY" # 设置要生成的文本的长度 prompt = "生成一篇文本,包含100个字。" model = "text-davinci-002" completions = openai.Completion.create( engine=model, prompt=prompt, max_tokens=100, n=1, stop=None, temperature=0.5, ) message = completions.choices[0].text print(message) ``` 在上面的代码,我们使用了 OpenAI 的 GPT-3 API 来生成文本。首先,我们需要导入 openai 库并设置 API 密钥。然后,我们设置要生成的文本的提示(prompt)和使用的模型(model),并调用 openai.Completion.create() 方法生成文本。最后,我们将生成的文本赋值给变量 message,并使用 print() 函数将其输出。 请注意,使用 GPT-3 API 需要付费,并且需要在 OpenAI 网站上申请 API 密钥。此外,还有许多其他工具和库可用于在 Python 实现文本生成,例如 TensorFlowKeras 和 spaCy 等。 ### 回答2: 文本生成是一种通过计算机程序自动生成文本的技术。这个过程可以通过各种算法和模型实现,其一个常用的方法是使用Python来开发。下面是一个简单的Python代码示例: ```python import random # 定义一个文本生成函数 def generate_text(): # 指定生成文本的长度为100个字符 text_length = 100 # 定义字符集合,可以根据需要进行扩展 characters = 'abcdefghijklmnopqrstuvwxyz' # 初始化生成的文本为空字符串 generated_text = '' # 通过循环生成指定长度的文本 for _ in range(text_length): # 随机选择一个字符并添加到生成的文本 random_character = random.choice(characters) generated_text += random_character # 返回生成的文本 return generated_text # 调用文本生成函数并打印生成的文本 generated_text = generate_text() print(generated_text) ``` 在这个代码示例,我们首先定义了一个名为`generate_text`的函数,该函数用于生成指定长度的随机文本。在函数内部,我们通过循环迭代生成指定长度的文本。在每次迭代,我们通过`random.choice`函数随机选择一个字符,并将其添加到已生成的文本。最后,我们返回生成的文本并打印结果。 这只是一个非常简单的文本生成示例,实际的文本生成可以采用更复杂的算法和模型。例如,可以使用循环神经网络(RNN)或生成对抗网络(GAN)来生成更符合语法和语义规则的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值