TensorFlow基础知识2-张量

1张量是什么?

张量是TensorFlow管理数据的形式。在TensorFlow程序中,所有数据都是通过张量的形式来表示的。张量是TensorFlow中运算结果的引用,在张量中并没有真正保存数字,它保存的是如何得到这些数字的计算过程。 
如下代码不会得到加法的结果,而是得到对结果的一个引用。

import tensorflow as tf
a = 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 + b
print(result)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出

Tensor("add:0", shape=(2,), dtype=float32)
  • 1

TensorFlow计算的结果不是一个具体的数字,而是一个张量的结构。 
张量保存了三个属性:名字name、维度shape、类型type

张量的第一个属性名字不仅是张量的唯一标识符,同样也给出了这个张量是如何计算出来的。

张量的命名形式“node:src_output”, 
node:是节点名称, 
src_output:表示当前张量来自节点的第几个输出。

再次运行后可以看到输出变成如下所示:

Tensor("add_1:0", shape=(2,), dtype=float32)
  • 1

连续运行几次代码,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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

通过张量存储中间结果,这样可以方便获取中间结果。

2、当计算图构造完成后,张量可以用来获得计算结果。

可以使用tf.Session().run(result)来得到计算结果。

tf.Session().run(result)
  • 1

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]]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、Numpy 
TensorFlow与专门操作N维数组而设计的科学计算包Numpy是紧密集成的。

import numpy as np
t_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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

手工指定Tensor对象时,使用NumPy是推荐的方式。

数学实例Python 例子
0纯量 (只有大小)s = 483
1向量(大小和方向)v = [1.1, 2.2, 3.3]
2矩阵(数据表)m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
33阶张量 (数据立体)t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
nn阶 (自己想想看)….

TensorFlow数据类型如下表所列

数据类型(dtype)Python 类型描述
DT_FLOATtf.float3232 位浮点数.
DT_DOUBLEtf.float6464 位浮点数.
DT_INT64tf.int6464 位有符号整型.
DT_INT32tf.int3232 位有符号整型.
DT_INT16tf.int1616 位有符号整型.
DT_INT8tf.int88 位有符号整型.
DT_UINT8tf.uint88 位无符号整型.
DT_STRINGtf.string可变长度的字节数组.每一个张量元素都是一个字节数组.
DT_BOOLtf.bool布尔型.
DT_COMPLEX64tf.complex64由两个32位浮点数组成的复数:实数和虚数.
DT_QINT32tf.qint32用于量化Ops的32位有符号整型.
DT_QINT8tf.qint8用于量化Ops的8位有符号整型.
DT_QUINT8tf.quint8用于量化Ops的8位无符号整型.

4张量的形状shape

TensorFlow文档中使用了三种记号来方便地描述张量的维度:阶,形状以及维数.下表展示了他们之间的关系:

形状维数实例
0[ ]0-D一个 0维张量. 一个纯量.
1[D0]1-D一个1维张量的形式[5].
2[D0, D1]2-D一个2维张量的形式[3, 4].
3[D0, D1, D2]3-D一个3维张量的形式 [1, 4, 3].
n[D0, D1, … Dn]n-D一个n维张量的形式 [D0, D1, … Dn].

可以通过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))
  • 1
  • 2
  • 3
  • 4

输出:[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))
  • 1
  • 2
  • 3
  • 4

输出:[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))
  • 1
  • 2
  • 3
  • 4

输出:[3 3 2]

文章出处:https://blog.csdn.net/hongxue8888/article/details/76560115

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值