Tensorflow学习笔记——占位符和feed_dict(二)
来源:互联网 发布:职业经理人 知乎 编辑:程序博客网 时间:2024/05/18 03:06
创建了各种形式的常量和变量后,但TensorFlow 同样还支持占位符。占位符并没有初始值,它只会分配必要的内存。在会话中,占位符可以使用 feed_dict 馈送数据。
feed_dict是一个字典,在字典中需要给出每一个用到的占位符的取值。在训练神经网络时需要每次提供一个批量的训练样本,如果每次迭代选取的数据要通过常量表示,那么TensorFlow 的计算图会非常大。因为每增加一个常量,TensorFlow 都会在计算图中增加一个结点。所以说拥有几百万次迭代的神经网络会拥有极其庞大的计算图,而占位符却可以解决这一点,它只会拥有占位符这一个结点。
下面一段代码分别展示了使用常量和占位符进行计算:
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+w1b=x1+w1sess=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 的计算过程使用常量。
下面是使用占位符的案例(计算两点之间的距离):
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)) 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 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.8284271]
阅读全文
2 0
- Tensorflow学习笔记——占位符和feed_dict(二)
- tensorflow学习笔记(2)-feed_dict
- TensorFlow学习之路(0):tf.placeholder() & feed_dict={}
- TensorFlow学习笔记之二——安装和运行
- Tensorflow学习: Placeholder占位符
- TensorFlow学习笔记(二):TensorFlow入门
- tensorflow学习笔记(2)——基础(二)——张量和会话
- TensorFlow学习笔记(二)
- TensorFlow学习笔记(二)
- TensorFlow学习笔记(二)
- 深度学习笔记——TensorFlow学习笔记(二)激活函数、损失函数、优化算法和正则项
- [tensorflow] feed_dict 传入序列
- Tensorflow深度学习笔记(二)-图、会话和变量
- TensorFlow学习笔记(二十一) tensorflow机器学习模型
- TensorFlow学习笔记(二)Tensorflow+VScode和Jupyter NoteBook新姿势
- Tensorflow入门----占位符、常量和Session
- tensorflow中的变量和占位符
- Tensorflow入门----占位符、常量和Session
- Unable to load native-hadoop library for your platform... using builtin-java classes where applicabl
- bzoj1087 [SCOI2005]互不侵犯King
- Android Volley框架的基本用法
- vector的使用
- bzoj1588 [HNOI2002]营业额统计
- Tensorflow学习笔记——占位符和feed_dict(二)
- 解决Android SDK Manager 无法更新下载SDK版本问题
- 字符串置换-lintcode-c++
- bzoj2301 [HAOI2011]Problem b
- ZigZag Conversion
- CentOS 系统启动流程(四)
- bzoj2743 [HEOI2012]采花
- css3-划过div放大突出效果
- java多线程(5)之线程池概念理解