tensorflow学习(3)- Fetch&Feed

前言

上一篇博客:tensorflow学习(2)- 变量的使用
上一节学习了变量的使用,这一节学习Fetch和Feed操作

Fetch代码

import tensorflow as tf
#Fetch 
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

add = tf.add(input2,input3)
mul = tf.multiply(input1,add)

with tf.Session() as sess:
    result = sess.run([mul,add])
    print(result)

输出结果:
在这里插入图片描述

  • Fetch的用处就是在一个会话中可以执行多个op

Feed代码

#feed
#创建占位符
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)

with tf.Session() as sess:
    #通过字典传入
    print(sess.run(output,feed_dict = {input1:[7.0],input2:[2.0]}))

输出结果:
在这里插入图片描述

  • feed方法需要先给变量赋值占位符
  • feed在执行的时候通过python中的字典数据类型赋值
tf.placeholder(dtype,shape=None,name=None)
  • dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
  • shape:数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)
  • name:名称
发布了53 篇原创文章 · 获赞 5 · 访问量 2215

猜你喜欢

转载自blog.csdn.net/qq_37668436/article/details/104794487