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的包

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/painKiller/p/12066631.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!