占位符和feed_dict

  虽然TensorFlow可以创建各种形式的常量和变量,但同样还支持占位符。占位符并没有初始值,它只会分配必要的内存。在会话中,占位符可以使用feed_dict馈送数据。
  feed_dict是一个字典,在字典中需要给出每一个用到的占位符的取值。在训练神经网络时,需要每次提供一个批量的训练样本,如果每次迭代选取的数据要通过常量表示,那么TensorFlow的计算图会非常大,因为每增加一个常量,TensorFlow都会在计算图中增加一个结点。所以拥有几百万次迭代的神经网络会拥有极其庞大的计算图,而占位符却可以解决这一问题,它只会拥有占位符这一个结点。
  下面一段代码分别展示了使用常量和占位符进行计算:

import tensorflow as tf

w1 = tf.Variable(tf.random_normal([1, 2], stddev=1, seed=1))
# 因为需要重复输入x,而每建一个x就会生成一个结点,计算图的效率会很低,所以使用占位符
x = tf.placeholder(tf.float32, shape=(1, 2))
x1 = tf.constant([[0.7, 0.9]])

a = x + w1
b = x1 + w1

sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 运行y时将占位符填上,feed_dict为字典,变量名不可变
y_1 = sess.run(a, feed_dict={x: [[0.7, 0.9]]})
y_2 = sess.run(b)
print(y_1)
print(y_2)
sess.close()

其中y_1的计算过程使用占位符,而y_2的计算过程使用常量。下面是使用占位符的案例(计算两点之间的距离):

import tensorflow as tf
import numpy as np

def calculate_eucledian_distance(point1, point2):
    difference = tf.subtract(point1, point2)
    power2 = tf.pow(difference, tf.constant(2.0, shape=(1, 2)))
    add = tf.reduce_sum(power2)
    eucledian_distance = tf.sqrt(add)
    return eucledian_distance

list_of_points1_ = [[1, 2], [3, 4], [5, 6], [7, 8]]
list_of_points2_ = [[15, 16], [13, 14], [11, 12], [9, 10]]

list_of_points1 = np.array([np.array(elem).reshape(1, 2) for elem in list_of_points1_])
list_of_points2 = np.array([np.array(elem).reshape(1, 2) for elem in list_of_points2_])

graph = tf.Graph()

with graph.as_default():
    # 使用tf.placeholder创建占位符 ,在session.run过程中再投递数据
    point1 = tf.placeholder(tf.float32, shape=(1, 2))
    point2 = tf.placeholder(tf.float32, shape=(1, 2))
    dist = calculate_eucledian_distance(point1, point2)

with tf.Session(graph=graph) as session:
    tf.global_variables_initializer().run()

    for ii in range(len(list_of_points1)):
        point1_ = list_of_points1[ii]
        point2_ = list_of_points2[ii]
        # 使用feed_dict将数据投入到[dist]中
        feed_dict = {point1: point1_, point2: point2_}
        distance = session.run([dist], feed_dict=feed_dict)
        print("the distance between {} and {} -> {}".format(point1_, point2_, distance))

执行结果:

the distance between [[1 2]] and [[15 16]] -> [19.79899]
the distance between [[3 4]] and [[13 14]] -> [14.142136]
the distance between [[5 6]] and [[11 12]] -> [8.485281]
the distance between [[7 8]] and [[ 9 10]] -> [2.828427]

  补充说明:也可以这样理解:feed_dict是为placeholder准备数据的。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值