tf.placeholder

ⅰ亾dé卋堺 提交于 2019-11-27 12:53:43

tf.placeholder

x = placeholder(dtype, shape=None, name=None)

可以理解为先占位,后面用数据feed,

注意: Placeholders are not compatible with eager execution.

所以在调用时,要加上下面这一句,才可以成功运行

tf.compat.v1.disable_eager_execution()

优于版本更新,用 tf.compat.v1. 来兼容tensorflow版本1的方法。

示例:

import tensorflow as tf
import numpy as np

tf.compat.v1.disable_eager_execution()
a = tf.compat.v1.placeholder(dtype=tf.int32)  # 定义一个占位符Tensor
b = tf.compat.v1.placeholder(dtype=tf.int32)
c = a + b

a_ = input("a = ")  # 从终端读入一个整数并放入变量a_
b_ = input("b = ")

sess = tf.compat.v1.Session()
c_ = sess.run(c, feed_dict={a: a_, b: b_})  # feed_dict参数传入为了计算c所需要的张量的值
print("a + b = %d" % c_)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!