1、第一个TensorFlow程序
import tensorflow as tf#导入Tensorflow库,并允许我们使用其精彩的功能
#定义一个字符串常量
message = tf.constant("Welcome to the exciting world of Deep Neural Networks!")
with tf.Session() as sess:#启动计算图
print(sess.run(message).decode())
2、placeholder和feed_dict的使用,placeholder是占位符,在神经网络中常常作为输入数据的节点,而feed_dict可以将数据传进去。写的代码如下,会报错
import tensorflow as tf
y = tf.constant("Hello world")#定义一个常量
x_placeholder = tf.placeholder(tf.string)
with tf.Session() as sess:
print(sess.run(x_placeholder,feed_dict ={x_placeholder:y}))
报错信息:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("Const:0", shape=(), dtype=string) which was passed to the feed with key Tensor("Placeholder:0", dtype=string).
它说传进去的值不能是Tensor类型。
修改如下:
import tensorflow as tf
#y = tf.constant("Hello world")#定义一个常量
x_placeholder = tf.placeholder(tf.string)
with tf.Session() as sess:
print(sess.run(x_placeholder,feed_dict ={x_placeholder:"Hello world"}))
这时可以正常输出。



