TensorFlow学习笔记(三)-- feed_dict 使用
个人理解:就是TF的一种输入语法。
跟C语言的scanf(),C++的 cin>> 意思差不多,只是长相奇怪了点而已。
做完下面几个例子,基本也就适应了。
首先占位符申请空间;使用的时候,通过占位符“喂(feed)”给程序。然后程序就可以run了。。。
理解的不一定对,也不够深入,仅供参考。
import tensorflow as tf
- tf.placeholder 占位符
- tf.Session 会话
1. 输出 Hello World
Str = tf.placeholder(tf.string) with tf.Session() as sess:
output = sess.run(Str, feed_dict={Str: 'Hello World !'})
print(output)
Hello World !
2.字符串拼接
Str1 = tf.placeholder(tf.string)
Str2 = tf.placeholder(tf.string)
Str3 = tf.placeholder(tf.string) Str = tf.string_join([Str1, Str2, Str3], separator=" ") with tf.Session() as sess:
output = sess.run(Str, feed_dict={Str1: 'I', Str2: 'like', Str3: 'TensorFlow !'})
print(output.decode())
I like TensorFlow !
3.浮点数乘积
Num1 = tf.placeholder(tf.float32)
Num2 = tf.placeholder(tf.float32) Result = tf.multiply(Num1, Num2) with tf.Session() as sess:
print(sess.run(Result, feed_dict={Num1:[.],Num2:[.]}))
[ .]
4.不用占位符,而用常量,也可完成。
只是验证一下,不推荐使用。初始化时的常量值将会被覆盖。
Num1 = tf.constant(1.0)
Num2 = tf.constant(2.0) Result = tf.multiply(Num1, Num2) with tf.Session() as sess:
print (sess.run(Result, feed_dict = {Num1: ., Num2: .}))
30.0
5.矩阵乘法
顺道学点新东西
- tf.random_normal 从正态分布中输出随机值
- tf.truncated_normal 从截断的正态分布中输出随机值
定义两个矩阵,分别为 2*3 和 3*2矩阵,完成乘法运算
Matrix1 = tf.Variable(tf.random_normal([,]))
Matrix2 = tf.Variable(tf.truncated_normal([,])) Result = tf.matmul(Matrix1,Matrix2) with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print ('Matrix1:')
print (sess.run(Matrix1))
print ('Matrix2:')
print (sess.run(Matrix2))
print ('Result:')
print (sess.run(Result))
Matrix1:
[[-1.00879586 0.61892986 -0.39552122]
[-0.83463311 -0.54309726 -0.31309164]]
Matrix2:
[[ 1.35596943 0.67712855]
[-0.09836598 -0.41533285]
[ 0.20601694 -0.14825489]]
Result:
[[-1.51026201 -0.88150841]
[-1.14281678 -0.29317039]]
使用 feed_dict完成矩阵乘法
表达看上去更繁琐。。。对比一下是为了更好地理解feed_dict。。。
Matrix1_Value = tf.random_normal([,])
Matrix2_Value = tf.truncated_normal([,]) Matrix1 = tf.placeholder(dtype=tf.float32,shape=[,])
Matrix2 = tf.placeholder(dtype=tf.float32,shape=[,]) Result = tf.matmul(Matrix1,Matrix2) with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print ('Matrix1:')
print (sess.run(Matrix1_Value))
print ('Matrix2:')
print (sess.run(Matrix2_Value))
print ('Result:')
print (sess.run(Result,feed_dict={Matrix1:sess.run(Matrix1_Value),Matrix2:sess.run(Matrix2_Value)}))
Matrix1:
[[-0.6228959 0.04135797 -0.76592982]
[ 0.04814498 -0.98519218 -0.88335162]]
Matrix2:
[[-0.73028505 0.62314421]
[-0.64763296 -0.18691106]
[ 0.0198773 0.68467569]]
Result:
[[-1.66321826 -2.89716744]
[ 1.28906226 2.08242035]]
TensorFlow学习笔记(三)-- feed_dict 使用的更多相关文章
- tensorflow学习笔记三:实例数据下载与读取
一.mnist数据 深度学习的入门实例,一般就是mnist手写数字分类识别,因此我们应该先下载这个数据集. tensorflow提供一个input_data.py文件,专门用于下载mnist数据,我们 ...
- tensorflow学习笔记三----------基本操作
tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作 import tensorflow as tf #定义三行四列的零矩阵 tf.zeros([3,4]) #定义两行三列 ...
- tensorflow学习笔记(三):实现自编码器
黄文坚的tensorflow实战一书中的第四章,讲述了tensorflow实现多层感知机.Hiton早年提出过自编码器的非监督学习算法,书中的代码给出了一个隐藏层的神经网络,本人扩展到了多层,改进了代 ...
- tensorflow学习笔记(三十四):Saver(保存与加载模型)
Savertensorflow 中的 Saver 对象是用于 参数保存和恢复的.如何使用呢? 这里介绍了一些基本的用法. 官网中给出了这么一个例子: v1 = tf.Variable(..., nam ...
- tensorflow学习笔记(三十九):双向rnn
tensorflow 双向 rnn 如何在tensorflow中实现双向rnn 单层双向rnn 单层双向rnn (cs224d) tensorflow中已经提供了双向rnn的接口,它就是tf.nn.b ...
- 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——自编码器及多层感知器
1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...
- tensorflow学习笔记——VGGNet
2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
随机推荐
- 终极方法,pjsip发起多方对讲出错Too many objects of the specified type (PJ_ETOOMANY)
http://blog.csdn.net/zhangjm_123/article/details/26727221 —————————————————————————————————————————— ...
- 第二百六十九节,Tornado框架-Session登录判断
Tornado框架-Session登录判断 Session需要结合cookie来实现 Session的理解 1.用户登录系统时,服务器端获取系统当前时间,进行nd5加密,得到加密后的密串 2.将密串作 ...
- ResultSet是结果集对象
ResultSet是结果集对象 DriverManager管理一组驱动程序 PreparedStatement预编译的,用来发送和执行SQL语句的
- hive经常使用命令
hive经常使用命令 show tables; 列出hive里面全部数据表名 desc userProfile; 显示数据表userProfile的基本表字段及字段type desc extended ...
- jQuery动态星级评分效果实现方法
本文实例讲述了jQuery动态星级评分效果实现方法.分享给大家供大家参考.具体如下: 这里的jQuery星级评分代码,是大家都很喜欢的功能,目前广泛应用,本星级评分加入了动画效果,注意,如果要真正实现 ...
- Jquery之编辑不可修改
<td class="queryTitle" width="100">优惠券批次号</td> <td class="qu ...
- Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 解决办法
相信很多人都遇到过这个问题,用Android Studio正在运行程序的时候,突然不知道什么原因,报一个找不到application或者找不到activity的错误(java.lang.ClassNo ...
- Socket通信编程实例(SIB和SS'SOB)
客户端: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...
- jquery 判断ul下是否存在li
$("ul").has("li").length > 0;$("ul > li").length > 0;$(" ...
- Windows游戏编程大师技巧之三角形填充
一.三角形的种类 三角形一般可以分为如下的四种类型(这四种类型是对于计算机来说的,不是数学意义上的分类): 平顶三角形:就是在计算机中显示的上面两个顶点的Y坐标相同. 平底三角形:就是在计算机中显示的 ...