1. tf.placeholder
tf.placeholder(
dtype,
shape=None,
name=None
)
Args:
dtype
: The type of elements in the tensor to be fed.shape
: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.name
: A name for the operation (optional).
Returns:
A Tensor
that may be used as a handle for feeding a value, but not evaluated directly.
2.tf.Session.run
run(
fetches,
feed_dict=None,
options=None,
run_metadata=None
)
Args:
fetches
: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above).feed_dict
: A dictionary that maps graph elements to values (described above).options
: A [RunOptions
] protocol bufferrun_metadata
: A [RunMetadata
] protocol buffer
Returns:
Either a single value if fetches
is a single graph element, or a list of values if fetches
is a list, or a dictionary with the same keys as fetches
if that is a dictionary (described above). Order in which fetches
operations are evaluated inside the call is undefined.
3.例子
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.placeholder(tf.int32)
b = tf.placeholder(tf.int32)
c= tf.multiply(a,b)
with tf.Session() as sess:
print(sess.run(c,feed_dict = {a:100,b:200}))
x1 = tf.placeholder(tf.float32,[2,3])
x2 = tf.placeholder(tf.float32,[3,2])
x3 = tf.matmul(x1,x2)
with tf.Session() as sess:
print(sess.run(x3,feed_dict = {x1:[[1,2,3],[4,5,6]],x2:[[1,2],[3,4],[5,6]]}))
====================================================================
4.实践
import tensorflow as tf
# Y=W*X+b
W = tf.placeholder(tf.float32,shape=(None,2))
X = tf.placeholder(tf.float32,shape=(None,2))
b = tf.placeholder(tf.float32,shape=(None,2))
Y= tf.add(tf.matmul(W,X),b)
with tf.Session() as sess:
sess.run(Y,feed_dict = {W:[[1,2],[2,3]], X:[[2,1],[3,4]], b:[[1,1],[1,1]]} )
#result=sess.run(c,feed_dict={x_batch:[[1],[2],[3]],y_batch:[[2],[3],[4]]})
tf.summary.FileWriter("D:\\test",tf.get_default_graph())
程序运行后会在D:\\test目录下生成event文件,然后在终端运行
tensorboard --logdir="D:\\test"
H:\AIE12code\tensorflow_tutorial_1>tensorboard --logdir="D:\\test"
Microsoft Windows [版本 6p://cathy-PC:6006 (Press CTRL+C to quit)版权所有 (c) 2009 Microso
生成图显示如下