TensorFlow基础知识2-张量
来源:互联网 发布:数据库分析工具 编辑:程序博客网 时间:2024/05/22 13:09
1张量是什么?
张量是TensorFlow管理数据的形式。在TensorFlow程序中,所有数据都是通过张量的形式来表示的。张量是TensorFlow中运算结果的引用,在张量中并没有真正保存数字,它保存的是如何得到这些数字的计算过程。
如下代码不会得到加法的结果,而是得到对结果的一个引用。
import tensorflow as tfa = tf.constant([1.0, 2.0], name="a")b = tf.constant([2.0, 3.0], name="b")result = tf.add(a,b,name="add")# result = a + bprint(result)
输出
Tensor("add:0", shape=(2,), dtype=float32)
TensorFlow计算的结果不是一个具体的数字,而是一个张量的结构。
张量保存了三个属性:名字name、维度shape、类型type
张量的第一个属性名字不仅是张量的唯一标识符,同样也给出了这个张量是如何计算出来的。
张量的命名形式“node:src_output”,
node:是节点名称,
src_output:表示当前张量来自节点的第几个输出。
再次运行后可以看到输出变成如下所示:
Tensor("add_1:0", shape=(2,), dtype=float32)
连续运行几次代码,node节点名会变化,我们可以看看tensorboard显示
2张量的使用
张量的用途主要可以总结为两类
1、对中间计算结果的引用。
import tensorflow as tf#使用张量记录中间结果a = tf.constant([1.0, 2.0], name="a")b = tf.constant([2.0, 3.0], name="b")result = a + b#直接计算向量的和,可读性会比较差result = tf.constant([1.0, 2.0], name="a") + tf.constant([2.0, 3.0], name="b")
通过张量存储中间结果,这样可以方便获取中间结果。
2、当计算图构造完成后,张量可以用来获得计算结果。
可以使用tf.Session().run(result)来得到计算结果。
tf.Session().run(result)
3TensorFlow的数据类型
1、Python原生类型
TensorFlow可接收Python数值、布尔值、字符串或由它们构成的列表。
t_0 = 50 #0阶张量或标量t_1 = ["apple","peach","banana"] #1阶张量或向量t_2 = [[True,False,False], #2阶张量或矩阵 [False,False,True], [False,True,False]]t_3 = [[[0,0],[0,1],[0,2]], #3阶张量 [[1,0],[1,1],[1,2]], [[2,0],[2,1],[2,2]]]
2、Numpy
TensorFlow与专门操作N维数组而设计的科学计算包Numpy是紧密集成的。
import numpy as npt_0 = np.array(50,dtype=np.int32) #0阶张量或标量t_1 = np.array(["apple","peach","banana"]) #1阶张量或向量t_2 = np.array([[True,False,False], #2阶张量或矩阵 [False,False,True], [False,True,False]],dtype=np.bool)t_3 = np.array([[[0,0],[0,1],[0,2]], #3阶张量 [[1,0],[1,1],[1,2]], [[2,0],[2,1],[2,2]]], dtype=np.int64)
手工指定Tensor对象时,使用NumPy是推荐的方式。
TensorFlow数据类型如下表所列
4张量的形状shape
TensorFlow文档中使用了三种记号来方便地描述张量的维度:阶,形状以及维数.下表展示了他们之间的关系:
可以通过tf.shape查看shape
t3_3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]shape = tf.shape(t3_3)sess = tf.Session()print(sess.run(shape))
输出:[3 3]
t3_3_1 = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]shape = tf.shape(t3_3_1)sess = tf.Session()print(sess.run(shape))
输出:[3 3 1]
t3_3_2 = [[[2,2], [4,4], [6,6]], [[8,8], [10,10], [12,12]], [[14,14], [16,16], [18,18]]]shape = tf.shape(t3_3_2)sess = tf.Session()print(sess.run(shape))
输出:[3 3 2]
参考:http://wiki.jikexueyuan.com/project/tensorflow-zh/resources/dims_types.html
- TensorFlow基础知识2-张量
- tensorflow-张量
- TensorFlow基础知识:计算图中的Op,边,和张量
- tensorflow张量详解
- tensorflow张量使用
- Tensorflow——张量
- TensorFlow数据模型--张量
- tensorflow 关于张量 shape 数组
- tensorflow中张量的理解
- TensorFlow的张量与会话
- tensorflow 关于张量 shape 数组
- tensorflow学习笔记(2)——基础(二)——张量和会话
- tensorflow的张量(tensor)的理解
- Tensorflow中张量数据类型的转换
- tensorflow如何查看张量属性,如阶数
- TensorFlow计算图、张量、回话详细介绍
- 3、TensorFlow 的数据模型-----张量(Tensor)
- (Tensorflow+python)张量、会话、向前传播
- [双连通分量]LA3523 Knights of the Round Table
- Uinux命令一
- jQuery选择器——属性过滤选择器
- 堆排序
- 学习笔记-面向对象思想
- TensorFlow基础知识2-张量
- ThinkPHP5 域名单独分组路由
- Class类
- 32. Longest Valid Parentheses
- MySQL中子查询IN,EXISTS,ANY,ALL,SOME,UNION介绍
- $.ajax中contentType: “application/json” 的用法
- 网络搭建
- python爬虫学习第八天
- 【CodeForces