下面的代码在教程中运行正常,但是在本地运行时出错。有安装错误或其他错误吗?请帮忙。这是该教程的链接:
https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=tfprogconcepts-colab&hl=en#scrollTo=Md8ze8e9geMi
和代码:
import tensorflow as tf
#create a graph
g = tf.Graph()
#establish the graph as the default graph
with g.as_default():
x = tf.constant(8, name = "x_const")
y = tf.constant(5, name = "y_const")
my_sum = tf.add(x,y, name = "x_y_sum")
#create the session
#this runs the default graph
with tf.Session() as sess:
print(my_sum.eval())
下面是发生的错误:
gunjan@gunjan-Inspiron-3558:~/Desktop$ python tf1.py
/home/gunjan/anaconda3/lib/python3.5/site-
packages/h5py/__init__.py:34: FutureWarning: Conversion of the second
argument of issubdtype from `float` to `np.floating` is deprecated. In
future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
2018-08-20 22:10:41.619062: I
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports
instructions that this TensorFlow binary was not compiled to use: AVX2
FMA
Traceback (most recent call last):
File "tf1.py", line 15, in <module>
print(my_sum.eval())
File "/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py", line 680, in eval
return _eval_using_default_session(self, feed_dict, self.graph,
session)
File "/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py", line 4942, in
_eval_using_default_session
raise ValueError("Cannot use the default session to evaluate tensor: "
ValueError: Cannot use the default session to evaluate tensor: the
tensor's graph is different from the session's graph. Pass an explicit
session to `eval(session=sess)`.
答案 0 :(得分:2)
问题是您创建了一个图形(require(reshape2)
require(ggplot2)
require(dplyr)
require(tidyr)
#setting up labels -- find out why italics expression isn't working??
Fig.labels<-c(expression(paste(italic("C. ret"), "-0d")),
expression(paste(italic("C. ret"), "-4d")),
expression(paste(italic("C. ret"), "-14d")),
expression(paste(italic("M. pan"), "-4d")),
expression(paste(italic("M. pan"), "-14d")))
A1_0d_ret<-rnorm(1:100,20)
A2_4d_ret<-rnorm(1:100,18)
A3_14d_ret<-rnorm(1:100,30)
A4_4d_pan<-rnorm(1:100,7)
A5_14d_pan<-rnorm(1:100,40)
data<-data.frame(A1_0d_ret,
A2_4d_ret,
A3_14d_ret,
A4_4d_pan,
A5_14d_pan)
long.data<-melt(data)
long.data_<-separate(data = long.data, col = variable, into = c("group", "treatment", "species"), sep = "_", remove=FALSE)
ggplot(long.data_, aes(x=treatment, y=log(value), group=variable))+
geom_boxplot(outlier.shape = NA, width=0.2 )+
scale_x_discrete("Never mind weird plot", labels=Fig.labels)+
theme_classic()
),并且正在另一个图形(g
)中执行代码。如果不需要两个图,则可以使用sess
:
sess
答案 1 :(得分:1)
要使其正常工作,您可以按照错误消息的建议显式地通过会话:
print(my_sum.eval(session=sess))
要了解为什么它不能完全按照本教程的规定工作,可以先将Python和TensorFlow的版本与本教程中使用的版本进行比较。
import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)
对于您链接的colab环境,将打印:
Python version: 2.7.14
TensorFlow version 1.10.0
编辑
再看看您的代码示例,这不是版本兼容性的问题。问题是您的教程副本没有正确保留缩进。第二个with
块需要放在第一个块中。
# Establish the graph as the "default" graph.
with g.as_default():
# ...
# Now create a session.
# The session will run the default graph.
with tf.Session() as sess:
print(my_sum.eval())
这可确保将g
用作会话的默认图形,而不是像MatthewScarpino那样创建一个新图形来指出您的缩进版本。
答案 2 :(得分:0)
如果您显式创建/使用Graph
对象而不是使用默认图形,则需要(a)将图形对象传递给Session
构造函数,或者(b)创建会话在图上下文中。
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session(graph=graph) as sess:
do_stuff_with(sess)
或
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session() as sess:
do_stuff_with(sess)