<深度学习>Tensorflow遇到的坑之一


AttributeError: module 'tensorflow' has no attribute 'random_normal'

AttributeError: module 'tensorflow' has no attribute 'Session'

 

查了很多,发现是tensorflow2.0不支持session了,之前试了

sess = tf.compat.v1.Session()

这句无效。

with tf.compat.v1.Session() as ses:

  # Build a graph.
  a = tf.constant(5.0)
  b = tf.constant(6.0)
  c = a * b

  # Evaluate the tensor `c`.
  print(ses.run(c))

 

这种写法可以解决,但是不兼容,我换个代码就不行了。。

 

然后在博客的回复里看到一个,可以解决这个问题:

import os
import tensorflow as tf
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

tf.disable_v2_behavior()
a = tf.random_([2, 30])
sess = tf.Session()
out = sess.run(a)
x, y = out
plt.scatter(x, y)
plt.show()



导入的时候直接导入tensorflow1的包


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM