Python 报错 AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘

雨点打透心脏的1/2处 2024-04-18 22:13 127阅读 0赞

placeholder是TensorFlow的占位符节点,由placeholder方法创建,其也是一种常量,但是由用户在调用run方法是传递的,也可以将placeholder理解为一种形参。即其不像constant那样直接可以使用,需要用户传递常数值。

输入:

  1. x = tf.placeholder(tf.int32, [batch_size, num_steps], name='input_placeholder')
  2. y = tf.placeholder(tf.int32, [batch_size, num_steps], name='output_placeholder')

结果报错:

AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

解决方法:

Tensorflow 1.x 版本提供placeholder,而 2.0版本暂时没有这个模块。

因此把Tensorflow 换回 1.x版本即可。


(2020.10.27更新)评论区@末得名字 提供的解决方法:

不要使用:

  1. import tensorflow as tf

替换为:

  1. import tensorflow.compat.v1 as tf tf.disable_v2_behavior()

发表评论

表情:
评论列表 (有 0 条评论,127人围观)

还没有评论,来说两句吧...

相关阅读