个人理解:就是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.矩阵乘法

顺道学点新东西

定义两个矩阵,分别为 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 使用的更多相关文章

  1. tensorflow学习笔记三:实例数据下载与读取

    一.mnist数据 深度学习的入门实例,一般就是mnist手写数字分类识别,因此我们应该先下载这个数据集. tensorflow提供一个input_data.py文件,专门用于下载mnist数据,我们 ...

  2. tensorflow学习笔记三----------基本操作

    tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作 import tensorflow as tf #定义三行四列的零矩阵 tf.zeros([3,4]) #定义两行三列 ...

  3. tensorflow学习笔记(三):实现自编码器

    黄文坚的tensorflow实战一书中的第四章,讲述了tensorflow实现多层感知机.Hiton早年提出过自编码器的非监督学习算法,书中的代码给出了一个隐藏层的神经网络,本人扩展到了多层,改进了代 ...

  4. tensorflow学习笔记(三十四):Saver(保存与加载模型)

    Savertensorflow 中的 Saver 对象是用于 参数保存和恢复的.如何使用呢? 这里介绍了一些基本的用法. 官网中给出了这么一个例子: v1 = tf.Variable(..., nam ...

  5. tensorflow学习笔记(三十九):双向rnn

    tensorflow 双向 rnn 如何在tensorflow中实现双向rnn 单层双向rnn 单层双向rnn (cs224d) tensorflow中已经提供了双向rnn的接口,它就是tf.nn.b ...

  6. 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别

    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...

  7. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)

    tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...

  8. tensorflow学习笔记——自编码器及多层感知器

    1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...

  9. tensorflow学习笔记——VGGNet

    2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...

  10. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

随机推荐

  1. jquery mobile小经验

    现在网站上关于jquery mobile的demo和帖子可真少啊,我刚开始接触,遇到了一些问题,都找不到人请教. 这是我的个人经验总结,或多或少会对刚入门的童鞋有点帮助吧. 如果想一开始进入页面的时候 ...

  2. php 显示一个干净的,易被解析的json

    header("Content-type: text/html; charset=utf-8"); //试着从数据库里读取一条数据放进来 $con = mysql_connect( ...

  3. 关于VS2013的安装遇到的问题

    老师突然说实验一需要用代码实现,我之前配置的cocos的编程环境是cocos+VS2013,是很稳定的 但是,我安装unity5.5的时候,不小心选择了顺带安装了VS2015,就等于我电脑里面有了两个 ...

  4. UNMET PEER DEPENDENCY @angular/common@2.3.1

    install of angular-cli results in unmeet peer dependencies. OSX 10.11.6node v6.9.1npm v3.10.8 [sudo] ...

  5. 用原生Canvas写贪吃蛇及问题解决

    为了学习Canvas,写了这个小游戏贪吃蛇供自己和大家学习 Github: https://github.com/zhiyishou/Gsnake Play On: http://zhiyishou. ...

  6. js实现jquery的offset()

    用过jQuery的offset()的同学都知道 offset().top或offset().left很方便地取得元素相对于整个页面的偏移. 而在js里,没有这样直接的方法,节点的属性offsetTop ...

  7. SQLServer------数据类型在C#中的转换

    SQL C#tinyint bytesmallint Int16char stringint int

  8. 【javaScript基础】马上调用函数表达式

    在javaScript中,每一个函数被调用时,都会创建一个新的运行上下文.由于在一个函数里面定义的变量和函数仅仅能在里面訪问.在外面是不行的.上下文提供了一种非常easy的方法来创建私有性. //ma ...

  9. mySubmit.js

    function mySubmit(theForm,url,result){ function default_callback(res){ result.html(res.info); if(res ...

  10. pybot/robot命令参数说明【dos下执行命令pybot.bat --help查看】

    Robot Framework -- A generic test automation framework Version: 3.0 (Python 3.4.0 on win32) Usage: r ...