安装好TensorFlow之后,开一个python环境,就可以开始运行和使用TensorFlow了。

先给一个实例,

#先导入TensorFlow
import tensorflow as tf hello_constant = tf.constant('Hello World!') # Create TensorFlow object called hello_constant
with tf.Session() as sess: 
output = sess.run(hello_constant) # Run the tf.constant operation in the session
   print(output)

  

也许有人奇怪,为什么不直接输出“Hello World!”呢,这个看起来很麻烦,是吗?其实不是的 
1.Tensor是什么? 
在 TensorFlow 中,数据不是以整数,浮点数或者字符串形式存在的,而是被封装在一个叫做 tensor 的对象中。Tensor是张量的意思,张量包含了0到任意维度的量,其中,0维的叫做常数,1维的叫做向量,二维叫做矩阵,多维度的就直接叫张量量。在 hello_constant = tf.constant(‘Hello World!’) 代码中,hello_constant是一个 0 维度的字符串 tensor,tensors 还有很多不同大小:

# tensor1 是一个0维的 int32 tensor
tensor1 = tf.constant(1234)
# tensor2 是一个1维的 int32 tensor
tensor2 = tf.constant([123,456,789])
# tensor3 是一个二维的 int32 tensor
tensor3 = tf.constant([ [123,456,789], [222,333,444] ])

  

2.Session是Tensorflow中的一个重要概念 
Tensorflow中的所有计算都构建在一张计算图中,这是一种对数学运算过程的可视化方法。就像刚才的代码:

with tf.Session() as sess:
output = sess.run(hello_constant)

  

这个session就是负责让这个图运算起来,session的主要任务就是负责分配GPU或者CPU的。

3.tf.placeholder() 
前面代码中出现了tf.constant(‘Hello World!’),这个tf.constant是用来定义常量的,其值是不变的,但是如果你需要用到一个变量怎么办呢?

这个时候就需要用到tf.placeholder() 和 feed_dict了。 
先给代码

x = tf.placeholder(tf.string)

with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Hello World'})

  

tf.placeholder表示一个占位符,至于是什么类型,看自己定义了,这里定义的是tf.string类型,然后呢,在session开始run以前,也就死这个图开始计算以前,就使用feed_dict将对应的值传入x,也就是这个占位符。 
同样的feed_dict可以设置多个tensor

x = tf.placeholder(tf.string)
y = tf.placeholder(tf.int32)
z = tf.placeholder(tf.float32) with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Test String', y: 123, z: 45.67})

  

但是需要注意的是,使用feed_dict设置tensor的时候,需要你给出的值类型与占位符定义的类型相同。

#coding=utf-8
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #忽略烦人的警告
#import numpy; hello_constant=tf.constant("Hello Ryan!")
tensor1 = tf.constant(1234)
tensor2 = tf.constant([123,456,789])
tensor3 = tf.constant([ [123,456,789], [222,333,444] ])
x = tf.placeholder(tf.string)
y = tf.placeholder(tf.int32)
z = tf.placeholder(tf.float32)
with tf.Session() as sess:
output=sess.run(hello_constant)
print(output)
output1=sess.run(tensor1)
print(output1)
output2=sess.run(tensor2)
print(output2)
output3=sess.run(tensor3)
print(output3)
output4=sess.run(x,feed_dict={x:'Hello World'})
print(output4)
output5=sess.run(x,feed_dict={x:'Hello zhu',y:123,z:55.5555})
print(output5)

  

$ python zhuyr.py
b'Hello Ryan!'
1234
[123 456 789]
[[123 456 789]
[222 333 444]]
Hello World
Hello zhu
(tensorflow)

Tensorflow入门----占位符、常量和Session的更多相关文章

  1. Tensorflow学习笔记——占位符和feed_dict(二)

    创建了各种形式的常量和变量后,但TensorFlow 同样还支持占位符.占位符并没有初始值,它只会分配必要的内存.在会话中,占位符可以使用 feed_dict 馈送数据. feed_dict是一个字典 ...

  2. 知乎TensorFlow入门学习记录

    知乎地址:https://zhuanlan.zhihu.com/p/30487008 import tensorflow as tf a=tf.placeholder(tf.int16) # 接受的数 ...

  3. tensorflow中张量_常量_变量_占位符

    1.tensor 在tensorflow中,数据是被封装在tensor对象中的.tensor是张量的意思,即包含从0到任意维度的张量.常数是0维度的张量,向量是1维度的张量,矩阵是二维度的张量,以及还 ...

  4. tensorflow学习笔记(二)常量、变量、占位符、会话

    常量.变量.占位符.会话是tensorflow编程的基础也是最常用到的东西,tensorflow中定义的变量.常量都是tensor(张量)类型. 常量tf.constant() tensorflow中 ...

  5. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

  6. TensorFlow从0到1之常量、变量和占位符详解(6)

    最基本的 TensorFlow 提供了一个库来定义和执行对张量的各种数学运算.张量,可理解为一个 n 维矩阵,所有类型的数据,包括标量.矢量和矩阵等都是特殊类型的张量.   TensorFlow 支持 ...

  7. TensorFlow解析常量、变量和占位符

    TensorFlow解析常量.变量和占位符 最基本的 TensorFlow 提供了一个库来定义和执行对张量的各种数学运算.张量,可理解为一个 n 维矩阵,所有类型的数据,包括标量.矢量和矩阵等都是特殊 ...

  8. TensorFlow占位符操作:tf.placeholder_with_default

    tf.placeholder_with_default 函数 placeholder_with_default( input, shape, name=None ) 请参阅指南:输入和读取器>占 ...

  9. python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出

    # 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...

随机推荐

  1. 简单拼接图像的tile_images和tile_images_offset算子

    有时候通常需要简单的拼图,不涉及图像融合之类的,仅仅是简单的平移将多张图拼接成一张图.tile_images和tile_images_offset就是用于简单拼图的2个算子. 谈到拼图,肯定有以下问题 ...

  2. DBA的工作职责和每日工作

    DBA一般职责 1.安装和升级数据库服务器,以及应用程序工具构建和配置网络环境. 2.熟悉数据库系统的存储结构预测未来的存储需求,制订数据库的存储方案. 3.根据开发人员设计的应用系统需求创建数据库存 ...

  3. Resolving multicopy duplications de novo using polyploid phasing 用多倍体相位法解决多拷贝复制的新问题

    抽象.虽然单分子测序系统的兴起已经实现组装复杂地区的能力空前提高在基因组中,基因组中的长节段重复仍然是装配中具有挑战性的前沿. 分段重复同时具有丰富的基因并且倾向于大的结构重排,使得它们的序列的分辨率 ...

  4. 随机分布 + action 计数

    For random samples from , use:  注意平方: sigma * np.random.randn(...) + mu 2.5*2.5 = 6.25 Two-by-four a ...

  5. shell 用环境变量的值修改properties文件

    假设有如下属性文件 demo.properties user.name=test user.password=123456 ............................... 需求:先需要 ...

  6. CString->char*.,char*->CString,char*->LPCTSTR

    CString->char* CString strSource;//宣告CString char* charSource; //宣告char* 法1: charSource = (char*) ...

  7. Ubuntu命令基础

    Ubuntu命令基础 1.打开终端窗口快捷键. Ctrl+alt+t 2.更新设置root密码. $sudo passwd  root  3.切换到root用户用su,前提是自己设置了root密码(看 ...

  8. 企业搜索引擎开发之连接器connector(十七)

    本文描述连接器的提供与外界交互的servlet接口,连接器与外部是通过xml格式数据交互的 1)  获取所有连接类型 提交地址:http://localhost:8080/connector-mana ...

  9. 创建TFS团队项目时自动建立代码库的文件夹结构

    很多客户都跟我提过一个这样的需求,即需要在创建团队TFS项目时,自动创建起源代码库的文档结构,例如类似下列结构的文件夹: <teamProject>   |- DEVELOPMENT   ...

  10. 在TFS 2013中选择一周中的工作日,例如增加星期日

    默认情况下,TFS在迭代视图中不计算周末的工作,如果出现调休的情况,则周末的工作日不会出现在迭代视图中,也不会参与燃尽图的计算.但是可以调整团队一周中的工作日,从而修正迭代计算方式,修改的方式参考下图 ...