Tensorflow——placeholder (small example of matrix operation)

1 Introduction

This time we will talk about placeholders in Tensorflow. Placeholders are placeholders in Tensorflow and temporarily store variables.

2. Matrix calculation

If Tensorflow wants to transfer data from outside, it needs to use tf.placeholder (), and then transfer data in this form 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)   #乘法

Next, the work of passing values ​​is handed over to sess.run (), and the values ​​that need to be passed are placed in feed_dict = {} and correspond to each input one by one. The placeholder and feed_dict = {} appear together.

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]}))

Insert picture description here

Published 227 original articles · praised 633 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105493341