【问题标题】:Error when running tensorflow in virtualenv: module 'tensorflow' has no attribute 'truncated_normal'在 virtualenv 中运行 tensorflow 时出错:模块 'tensorflow' 没有属性 'truncated_normal'
【发布时间】:2020-02-08 23:48:36
【问题描述】:

运行 keras 制作的 CNN 时出现以下错误

文件 “venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, 第 4185 行,在 truncated_normal return tf.truncated_normal(shape, mean, stddev, dtype=dtype, seed=seed) AttributeError: module 'tensorflow' has no attribute '截断_正常'

我已经多次安装并重新安装了 Tensorflow 2.0。会发生什么?

【问题讨论】:

  • 您使用的是哪个版本的 Keras?
  • Keras==2.2.4 Keras-Applications==1.0.6 Keras-Preprocessing==1.0.5
  • 那个版本的Keras不支持TensorFlow 2.0,你应该降级TF或者使用Keras 2.3
  • 很高兴知道,让我试试你的第二个建议,我会给你一个反馈
  • 成功了。如果您回答我的问题,我肯定会将我的问题标记为已解决。非常非常感谢。

标签: python tensorflow keras


【解决方案1】:

对于 TewnsorFlow 2.x 和 keras 2.3 或以上版本,以下用法适应性更强。用户无需同时降级 TensorFlow 和 Keras。

# Add the import statements 
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# Change it to the sample expression as follows. 
init = tf.compat.v1.random.truncated_normal()

干杯!

【讨论】:

    【解决方案2】:

    Keras 2.2.4 不支持 TensorFlow 2.0(它早于 TF 2.0 发布),因此您可以将 TensorFlow 降级到 1.x 版,或者将 Keras 升级到支持 TensorFlow 2.0 的 2.3 版。

    【讨论】:

      【解决方案3】:

      在 TensorFlow v2.0 及更高版本中,“tf.truncated_normal”替换为“tf.random.truncated_normal

      【讨论】:

      • 很好奇为什么这没有被选为正确答案?
      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多