2022-07-03计算图中形式存储

2022-07-03

上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作提交补丁, 直接插入一个 tensor.

feed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为run()调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最常见的用例是将某些特殊的操作指定为 “feed” 操作, 标记的方法是使用 tf.placeholder() 为这些操作创建占位符.


input1 = tf.placeholder(tf.float32)

图片[1]-2022-07-03计算图中形式存储-唐朝资源网

input2 = tf.placeholder(tf.float32) output = tf.mul(input1, input2) with tf.Session() as sess: print sess.run([output], feed_dict={input1:[7.], input2:[2.]})

图片[2]-2022-07-03计算图中形式存储-唐朝资源网

# 输出: # [array([ 14.], dtype=float32)]
-------------------------------------------------------------------------------------------------------------

trX = np.linspace(-1, 1, 101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is approximately linear 

X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")

sess = tf.Session()
init = tf.initialize_all_variables() # you need to initialize variables (in this case just variable W)
sess.run(init)

图片[3]-2022-07-03计算图中形式存储-唐朝资源网

for i in range(100): for (x, y) in zip(trX, trY): sess.run(train_op, feed_dict={X: x, Y: y}) print(sess.run(w)) # something around 2




 

分类:

技术点:

相关文章:

© 版权声明
THE END
喜欢就支持一下吧
点赞85赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容