Tensorflow——placeholder(矩阵运算小实例)

1.前言

这次我们讲 Tensorflow 中的 placeholder , placeholder 是 Tensorflow 中的占位符,暂时储存变量.

2.矩阵计算

Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数据 sess.run(***, feed_dict={input: **}).

import tensorflow as tf

input1 = tf.placeholder(tf.float32)  #占位符,暂时储存变量
input2 = tf.placeholder(tf.float32)

output_add = tf.add(input1,input2)    #加法
output_multi = tf.multiply(input1,input2)   #乘法

接下来, 传值的工作交给了 sess.run() , 需要传入的值放在了feed_dict={} 并一一对应每一个 input. placeholder 与 feed_dict={} 是绑定在一起出现的。

with tf.Session() as sess:   
    print('input1+input2:',sess.run(output_add, feed_dict={input1:[10],input2:[4]}))  #在sess.run()中传值
    print('input1*input2:',sess.run(output_multi, feed_dict={input1:[10],input2:[4]}))

在这里插入图片描述

发布了227 篇原创文章 · 获赞 633 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/105493341