tf.placeholder(dtype, shape=None, name=None)
此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值
1、参数:
dtype:数据类型。常用的是tf.float32, tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
name:名称。
x = tf.placeholder(tf.float32, shape=(1024, 1024))
#矩阵乘法
y = tf.matmul(x, x)
with tf.Session() as sess:
print(sess.run(y)) # ERROR: 此处x还没有赋值.
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
2、赋值
在session运行阶段,需要给placeholder提供数据,利用feed_dict的字典结构给placeholdr变量“喂数据”
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 3 11:20:22 2017
@author: zhangshaoxing
"""
import tensorflow as tf
a=tf.placeholder(tf.float32)
b=tf.placeholder(tf.float32)
c=tf.add(a,b)
with tf.Session() as sess:
print(sess.run(c,feed_dict={a:10,b:30})) #把10赋给a,30赋给b
结果
40.0