Tensorflow入门----占位符、常量和Session
2017-10-12 10:00
351 查看
安装好TensorFlow之后,开一个python环境,就可以开始运行和使用TensorFlow了。
先给一个实例,
2
3
4
5
6
7
8
9
10
也许有人奇怪,为什么不直接输出“Hello World!”呢,这个看起来很麻烦,是吗?其实不是的
1.Tensor是什么?
在 TensorFlow 中,数据不是以整数,浮点数或者字符串形式存在的,而是被封装在一个叫做 tensor 的对象中。Tensor是张量的意思,张量包含了0到任意维度的量,其中,0维的叫做常数,1维的叫做向量,二维叫做矩阵,多维度的就直接叫张量量。在 hello_constant = tf.constant(‘Hello World!’) 代码中,hello_constant是一个 0 维度的字符串 tensor,tensors 还有很多不同大小:
2
3
4
5
6
2.Session是Tensorflow中的一个重要概念
Tensorflow中的所有计算都构建在一张计算图中,这是一种对数学运算过程的可视化方法。就像刚才的代码:
2
这个session就是负责让这个图运算起来,session的主要任务就是负责分配GPU或者CPU的。
3.tf.placeholder()
前面代码中出现了tf.constant(‘Hello World!’),这个tf.constant是用来定义常量的,其值是不变的,但是如果你需要用到一个变量怎么办呢?
这个时候就需要用到tf.placeholder() 和 feed_dict了。
先给代码
2
3
4
tf.placeholder表示一个占位符,至于是什么类型,看自己定义了,这里定义的是tf.string类型,然后呢,在session开始run以前,也就死这个图开始计算以前,就使用feed_dict将对应的值传入x,也就是这个占位符。
同样的feed_dict可以设置多个tensor
2
3
4
5
6
但是需要注意的是,使用feed_dict设置tensor的时候,需要你给出的值类型与占位符定义的类型相同。
先给一个实例,
#先导入TensorFlow import tensorflow as tf # Create TensorFlow object called hello_constant hello_constant = tf.constant('Hello World!') with tf.Session() as sess: # Run the tf.constant operation in the session output = sess.run(hello_constant) print(output)1
2
3
4
5
6
7
8
9
10
也许有人奇怪,为什么不直接输出“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] ])1
2
3
4
5
6
2.Session是Tensorflow中的一个重要概念
Tensorflow中的所有计算都构建在一张计算图中,这是一种对数学运算过程的可视化方法。就像刚才的代码:
with tf.Session() as sess: output = sess.run(hello_constant)1
2
这个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'})1
2
3
4
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})1
2
3
4
5
6
但是需要注意的是,使用feed_dict设置tensor的时候,需要你给出的值类型与占位符定义的类型相同。
相关文章推荐
- Tensorflow入门----占位符、常量和Session
- tensorflow入门之Session
- tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
- 7天入门php-session基础使用及预定义常量
- Tensorflow 入门学习2 常量、变量和数据类型
- TensorFlow入门教程(2)占位符、变量、损失函数
- Tensorflow 入门一 (安装配置,会话控制,变量常量,传入值)
- Tensorflow实战学习(五)【Graph对象、Session对象、占位符】
- Tensorflow实战学习(七)【Tensor、Graph、Op、Variable、占位符、Session、名称作用域、Board综合例子】
- 超简单tensorflow入门优化程序&&tensorboard可视化
- TensorFlow入门练习(一)
- tensorflow 学习专栏(二):tensorflow基础操作 常量加乘 矩阵乘
- 45、Docker 加 tensorflow的机器学习入门初步
- CNN入门详解及TensorFlow源码实现--深度学习笔记
- 【JavaWeb-19】Hibernate简介、入门案例、Session操作、2个配置文件详解
- 机器学习-tensorflow入门教程一——基础内容
- Tensorflow深度学习入门——采用卷积和池化优化训练MNIST数据——代码+注释
- tensorflow75 Keras on Tensorflow 入门
- TensorFlow入门教程:1:安装和第一个例子程序
- Tensorflow深度学习入门——基础运行流程