tensorflow基础使用2

来源:互联网 发布:a 算法解决八数码难题 编辑:程序博客网 时间:2024/06/15 05:41

Fetch和Feed的用法



1.fetch的用法也就是在session运行时,可以以列表的方式传参,从而同时run多个op

# -*- coding:utf-8 -*-import tensorflow as tf# how to use Fetchinput1 = 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]) ##Fetch    print(result)
2.Feed的用法

即在调用session的run方法时,传入一个函数,再以字典的形式传入其参数

# -*- coding:utf-8 -*-import tensorflow as tf# how to use Feedinput1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32)output = tf.multiply(input1,input2)with tf.Session() as sess:    # use Feed with Dict    print(sess.run(output,feed_dict={input1:[7.],input2:[2.]}))