<深度學習>Tensorflow遇到的坑之一

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

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

 

查了不少,發現是tensorflow2.0不支持session了,以前試了dom

sess = tf.compat.v1.Session()ui

這句無效。lua

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

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

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

 

這種寫法能夠解決,可是不兼容,我換個代碼就不行了。。orm

 

而後在博客的回覆裏看到一個,能夠解決這個問題:博客

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()



it

導入的時候直接導入tensorflow1的包

相關文章
相關標籤/搜索