天天看点

Tensorflow 打印常量hello tensorflow

环境信息

Data Science Workshop Dev

打印常量hello tensorflow

import tensorflow as tf
tf.__version__

'2.4.0'           
# 定义常量
hello_tf_constant = tf.constant("hello,tensorflow")           
# 版本不兼容,无法直接运行
with tf.Session() as sess:
    sess.run(hello_tf_constant)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-4eb9d4d3287a> in <module>
----> 1 with tf.Session() as sess:
      2     sess.run(hello_tf_constant)

AttributeError: module 'tensorflow' has no attribute 'Session'               
with tf.compat.v1.Session() as sess:
    sess.run(hello_tf_constant)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-420f364e899d> in <module>
      1 with tf.compat.v1.Session() as sess:
----> 2     sess.run(hello_tf_constant)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    966     try:
    967       result = self._run(None, fetches, feed_dict, options_ptr,
--> 968                          run_metadata_ptr)
    969       if run_metadata:
    970         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1114       raise RuntimeError('Attempted to use a closed Session.')
   1115     if self.graph.version == 0:
-> 1116       raise RuntimeError('The Session graph is empty.  Add operations to the '
   1117                          'graph before calling run().')
   1118 

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().               
tf.compat.v1.disable_eager_execution()
with tf.compat.v1.Session() as sess:
    sess.run(hello_tf_constant)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-95efe77ef2e2> in <module>
      1 tf.compat.v1.disable_eager_execution()
      2 with tf.compat.v1.Session() as sess:
----> 3     sess.run(hello_tf_constant)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    966     try:
    967       result = self._run(None, fetches, feed_dict, options_ptr,
--> 968                          run_metadata_ptr)
    969       if run_metadata:
    970         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1114       raise RuntimeError('Attempted to use a closed Session.')
   1115     if self.graph.version == 0:
-> 1116       raise RuntimeError('The Session graph is empty.  Add operations to the '
   1117                          'graph before calling run().')
   1118 

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().           
tf.compat.v1.disable_eager_execution()
hello_tf_constant = tf.constant("hello,tensorflow")
with tf.compat.v1.Session() as sess:
    print(sess.run(hello_tf_constant))

b'hello,tensorflow'           

参考链接

  1. RuntimeError: The Session graph is empty. Add operations to the graph before calling run().解决方法
  2. AttributeError: module 'tensorflow' has no attribute 'Session'错误解决
  3. tensorflow 无法执行sess =tf .Session ()

相关链接

备注

工匠精神,精益求精,踏实学习,再接再厉 O(∩_∩)O

欢迎各位同学一起来交流学习心得!