tensorflow学习笔记--feed_dict使用(代码片段)

大葱拌豆腐 大葱拌豆腐     2022-11-03     795

关键词:

个人理解:就是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:[5.],Num2:[6.]))
[ 30.]

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: 5., Num2: 6.))
30.0

5.矩阵乘法

顺道学点新东西

定义两个矩阵,分别为 2*3 和 3*2矩阵,完成乘法运算

Matrix1 = tf.Variable(tf.random_normal([2,3]))  
Matrix2 = tf.Variable(tf.truncated_normal([3,2]))  

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([2,3])
Matrix2_Value = tf.truncated_normal([3,2])

Matrix1 = tf.placeholder(dtype=tf.float32,shape=[2,3]) 
Matrix2 = tf.placeholder(dtype=tf.float32,shape=[3,2])   

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 问题

】TensorFlowfeed_dict问题【英文标题】:Tensorflowfeed_dictissue【发布时间】:2017-12-2116:52:34【问题描述】:所以我是Tensorflow的新手,我试图准确了解何时使用feed_dict以及何时不需要。但是,我对feed_dict的工作方式感到困惑。例如:1和... 查看详情

Tensorflow:feed_dict 的形状错误

】Tensorflow:feed_dict的形状错误【英文标题】:Tensorflow:wrongshapeoffeed_dictTensorflow:feed_dict的形状错误【发布时间】:2018-04-0403:48:04【问题描述】:第一次遇到这样的问题。错误是关于feed_dict=tfkids:kids,tfkids_fit:kids_fit,看来需要重塑... 查看详情

在 C++ 中为 Tensorflow 模型定义 feed_dict

】在C++中为Tensorflow模型定义feed_dict【英文标题】:Defineafeed_dictinc++forTensorflowmodels【发布时间】:2016-04-2223:18:14【问题描述】:这个问题与这个问题有关:ExportTensorflowgraphsfromPythonforuseinC++我正在尝试将Tensorflow模型从Python导出到C+... 查看详情

tensorflow机器学习入门——读取数据(代码片段)

TensorFlow中可以通过三种方式读取数据:一、通过feed_dict传递数据;input1=tf.placeholder(tf.float32)input2=tf.placeholder(tf.float32)output=tf.multiply(input1,input2)withtf.Session()assess:feed_dict=input1:[[7.,2.]],input2:[[2.],[3.]]print(sess.run(output,feed_dict))二、从文... 查看详情

如何使用队列方法(没有 feed_dict)#tensorflow 在保存的模型上使用测试数据?

】如何使用队列方法(没有feed_dict)#tensorflow在保存的模型上使用测试数据?【英文标题】:HowtouseTestdataonsavedmodelwithqueueapproach(withoutfeed_dict)#tensorflow?【发布时间】:2017-11-1316:44:43【问题描述】:我是张量流的新手。我已经为mnis... 查看详情

tensorflow学习笔记

简单神经网络构造importtensorflowastfimportmatplotlib.pyplotaspltimportnumpyasnptf.set_random_seed(1)np.random.seed(1)fakedatax=np.linspace(-1,1,100)[:,np.newaxis]#shape(100,1)noise=np.random.normal(0,0.1,size 查看详情

tensorflow学习笔记

importtensorflowastfimportnumpyasnpimportmathimporttensorflow.examples.tutorials.mnistasmnsess=tf.InteractiveSession()mnist=mn.input_data.read_data_sets("E:\Python35\Lib\site-packages\tensorflow\ 查看详情

tensorflow学习笔记一——justgetstarted

  我现在什么都不知道,打算开始学习tensorflow作为机器学习入门的开始。  昨天完成了对tensorflow官方入门介绍的学习,了解了tensorflow的基本原理和编程方法。我们在进行tensorflow编程时,程序的逻辑是:建立数据流图-->... 查看详情

《tensorflow实战google深度学习框架(第二版)》学习笔记及书评(代码片段)

《TensorFlow实战Google深度学习框架(第二版)》学习笔记文章目录《TensorFlow实战Google深度学习框架(第二版)》学习笔记写在前面1.TensorFlow图像处理函数学习总结2.TensorFlow图像预处理完整样例3.TensorFlow多线程输入... 查看详情

tensorflow学习笔记3:tensorboard可视化学习

TensorBoard简介Tensorflow发布包中提供了TensorBoard,用于展示Tensorflow任务在计算过程中的Graph、定量指标图以及附加数据。大致的效果如下所示, TensorBoard工作机制TensorBoard通过读取TensorFlow的事件文件来运行。TensorFlow的事件文件... 查看详情

keras 使用 tensorflow 作为后端:无法将 feed_dict 键解释为张量:无法将 int 转换为张量

】keras使用tensorflow作为后端:无法将feed_dict键解释为张量:无法将int转换为张量【英文标题】:kerasusingtensorflowasbackend:Cannotinterpretfeed_dictkeyasTensor:CannotconvertaintintoaTensor【发布时间】:2017-07-1721:18:20【问题描述】:我正在尝试在ke... 查看详情

tensorflow学习笔记(代码片段)

TensorFlow学习笔记一、常量表示importtensorflowastfm1=tf.constant([[3,3]])m2=tf.constant([[2],[3]])product=tf.matmul(m1,m2)print(product)sess=tf.Session()result=sess.run(product)print(result)sess.close()withtf.S 查看详情

tensorflow基础笔记参考资源学习文档

1官方文档https://www.tensorflow.org/api_docs/2极客学院中文文档http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html3TensorFlow基础笔记(2)minist分类学习 查看详情

tensorflow学习笔记2:aboutsession,graph,operationandtensor

简介上一篇笔记:Tensorflow学习笔记1:GetStarted 我们谈到Tensorflow是基于图(Graph)的计算系统。而图的节点则是由操作(Operation)来构成的,而图的各个节点之间则是由张量(Tensor)作为边来连接在一起的。所以Tensorflow的计... 查看详情

tensorflow学习笔记

一.背景  本人学习Tensorflow是为了完成毕业设计的工作,之前并没有用过其他的深度学习平台,Tensorflow作为当前很流行的一个平台,无论是教学文档,还是使用其开发的工程,这些资源都是很丰富的,所以很适合新手来进行入... 查看详情

tensorflow学习笔记---2--dcgan代码学习

...rks)的网络结构。代码下载地址https://github.com/carpedm20/DCGAN-tensorflow注1:发现代码中以mnist为训练集的网络和以无标签数据集(以下简称unlabeled_dataset)为训练集的网络不同,结构有别。以下笔记主要针对 查看详情

tensorflow学习笔记:共享变量

本文是根据TensorFlow官方教程翻译总结的学习笔记,主要介绍了在TensorFlow中如何共享参数变量。教程中首先引入共享变量的应用场景,紧接着用一个例子介绍如何实现共享变量(主要涉及到tf.variable_scope()和tf.get_variable()两个接口... 查看详情

tensorflow学习笔记一

今天开始正式学习Tensorflow, 首先从源码开始,装好了CentOS7X64,并且安装了桌面版本,计划能够构建Tensorflow成功首先从Github从Fork了一个版本到我的Git账号,然后clone到本地首先要了解Bazel,这是什么东西?这是谷歌代码构建工... 查看详情