个人理解:就是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. 浅析 Hadoop 中的数据倾斜

    转自:http://my.oschina.net/leejun2005/blog/100922 最近几次被问到关于数据倾斜的问题,这里找了些资料也结合一些自己的理解. 在并行计算中我们总希望分配的每一 ...

  2. git error Another git process seems to be running in this repository

    How to fix error Another git process seems to be running in this repository When you use Git, you se ...

  3. java为安全起见对Applet有所限制

    Applet消亡的原因: ①java为安全起见对Applet有所限制:Applet不允许访问本地文件信息.敏感信息,不能执行本地指令(比如FORMAT),不能访问初原服务器之外的其他服务器. ① IE ...

  4. 转载 Python导入模块的几种姿势

    作为一名新手Python程序员,你首先需要学习的内容之一就是如何导入模块或包.但是我注意到,那些许多年来不时使用Python的人并不是都知道Python的导入机制其实非常灵活.在本文中,我们将探讨以下 ...

  5. linux -- 查看ip,路由,dns

    查看ip地址:ifconfig 查看gateway:route 查看dns:nm-tool

  6. hdu 3008:Warcraft(动态规划 背包)

    Warcraft Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. 【OpenWRT】网络配置

    cd /etc/config vim network vim wireless cd /etc/init.d/network

  8. Js 的几种去重(一维)

    写的几种数组去重方法: 第一种: [利用排序方法,然后比较当前元素与下一个元素是否相等] function repeat1(arr) { var length = arr.length; var re ...

  9. 'not all arguments converted during string formatting'错误告警信息解决办法

    问题描述:

  10. [素数个数模板] HDU 5901 Count primes

    #include<cstdio> #include<cmath> using namespace std; #define LL long long ; bool np[N]; ...