TensorFlow之张量

张量的概念

张量是数学对象,是标量、向量和矩阵的概括。我们可以直接理解张量是一个列表,一个多维数组。

张量的维数由它的阶数表示:

0阶张量标量单值示例:a = 1

1 阶张量向量的一维数组示例:a = [1,2,3]

二维张量矩阵二维数组示例:a = [[1,2,3],[4,5,6]]

n阶张量张量n维数组示例:a = [[[[[…n个括号…]]]]]

要判断张量的顺序,请查看等号右侧的方括号。有几个0,分别是0个订单,2个是2个订单。

多维张量也作为一维数组连续存储在内存中。

创建张量

tf.() 方法用于创建张量,传入的内容可以是数字、列表或 NumPy 数组。第二个参数是指定的数据类型,可以省略。

喜欢:

import tensorflow as tf
a = tf.constant([1,2,3],dtype=tf.int32)
print(a)

打印结果:

tf.([1 2 3], shape=(3,), dtype=int32)

shape 是张量的形状。张量有多少秩取决于形状中用逗号分隔的数字。例如,一个形状为 (3,) 的张量与数字 3 用逗号隔开,所以它的 rank 数字是 1 阶的,其中有 3 个元素。例如,形状为 (3,3) 的张量将两个数字 3 和 3 分开,因此它的阶数为 2,它是一个 3 行和 3 列的矩阵。

当然也可以以属性的形式查看张量信息

print('张量的阶数:', a.ndim)
print('张量的形状:', a.shape)
print('张量的类型:', a.dtype)

打印结果:

张量的阶数:1

张量的形状:(3,)

张量的类型:

张量的数据类型

为了创建上面的张量,我添加了 dtype 参数来指定数据类型为 int32。如果未指定数据类型,也是如此。类型会根据张量的内容自动推断。如果张量的内容是数字,则默认数据类型为 int32。内容为浮点数,则默认数据类型为,如果需要指定int64等其他数字或者需要传参数手动指定。

张量的数据类型有:

int 有符号整数

float 有符号浮点数

uint 无符号整数

以上三个都有数字的划分。例如,int 有 tf.int8、tf.int16、tf.int32、tf.int64。

细绳

bool 布尔值

复数由实数和虚数组成

tf.cast() 方法可以转换张量的数据类型,例如:

import tensorflow as tf
a = tf.constant([1,2,3],dtype=tf.int32)
print(a)
b = tf.cast(a,dtype=tf.int64)
print(b)

打印结果:

tf.([1 2 3], shape=(3,), dtype=int32)

tf.([1 2 3], shape=(3,), dtype=int6<​​@4)

NumPy 数据转换

如果数据是 numpy 创建的,则需要使用 tf.() 函数将 numpy 数据类型转换为 数据类型。

import tensorflow as tf
import numpy as np
a = np.arange(0,10)
print(a)
b = tf.convert_to_tensor(a)
print(b)

打印结果:

[0 1 2 3 4 5 6 7 8 9]

tf.([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

需要注意的一点是,numpy在创建浮点矩阵时,默认是type,在转换的时候也会接收到它的数据类型。因此,从 numpy 数据生成的张量将具有默认的浮点类型 bit,如下所示:

import tensorflow as tf
import numpy as np
a = np.array([1.1,2.2,3.3])
print(a)
b = tf.constant(a)
print(b)
c = tf.convert_to_tensor(a)
print(c)

打印结果:

[1.1 2.2 3.3]

tf.([1.1 2.2 3.3], shape=(3,), dtype=)

tf.([1.1 2.2 3.3], shape=(3,), dtype=)

在大多数机器学习场景中,我们使用 32 位浮点数来满足计算需求。32 位浮点数的运算比 64 位浮点数的运算快得多。因此,使用 numpy 数组生成张量时,可以显式指定数据类型:

import tensorflow as tf
import numpy as np
a = np.array([1.1,2.2,3.3])
print(a)
b = tf.constant(a,dtype=tf.float32)
print(b)
c = tf.convert_to_tensor(a,dtype=tf.float32)
print(c)

打印结果:

[1.1 2.2 3.3]

tf.([1.1 2.2 3.3], shape=(3,), dtype=)

tf.([1.1 2.2 3.3], shape=(3,), dtype=)

固定张量

提供一些函数来创建固定张量,例如创建全0的张量,以及创建全1的张量。

全 0 张量

tf.zeros(形状,dtype=)

参数的写法不止一种,只记一种容易记的,比如一维写直接,二维写【行数,列数】,三维写【行数, [n,m,k,j,…]的列数、高度]、多维书写用等

#创建一维张量 4个元素
a = tf.zeros(4)
print(a)
#创建一维张量 4个元素
aa = tf.zeros([4])
print(aa)
#创建3行3列的二维张量
b = tf.zeros([3,3])
print(b)
#创建3行3列的二维张量
bb = tf.zeros((3,3))
print(bb)
#指定类型
c = tf.zeros([3,3],tf.int8)
print(c)

全 1 张量

tf.ones(形状,dtype=)

创建一个值全为1的张量,除了将zeros函数替换为ones函数外,创建用法与全0的张量完全一样。

具有相同元素值的张量

tf.fill(形状,值)

fill 函数没有 dtype 参数,它根据 value 参数自动确定类型。

#创建一维张量 4个元素
aa = tf.fill([4],10)
print(aa)

打印结果:

tf.([10 10 10 10], shape=(4,), dtype=int32)

#创建3行3列的二维张量
c = tf.fill([3,3],4.4)
print(c)

打印结果:

tf.(

[[4.4 4.4 4.4]

[4.4 4.4 4.4]

[4.4 4.4 4.4]],形状=(3,3),dtype=)

随机数张量正态分布

函数签名:tf..(shape,mean,,dtype)

形状:张量形状

平均值:平均值默认为 0

: 标准差默认为 1

dtype:存储类型默认为

例子:

#创建2行2列均值为0标准差为1的随机数张量
a = tf.random.normal([2, 2], mean=0, stddev=1)
print(a)

打印结果:

tf.(

[[-0. 0.]

[-2. -0. ]], 形状=(2, 2), dtype=)

截断正态分布

函数签名:tf..(shape,mean,,dtype)

此函数返回截断的正态分布。截断标准为标准差的2倍,可以保证生成的数据更集中在均值上。

例子:

b = tf.random.truncated_normal([2, 2], mean=0, stddev=1)
print(b)

打印结果:

tf.(

[[-0. 1. ]

[-0. 0.]], 形状=(2, 2), dtype=)

通俗的讲,当均值为0,标准差为1时,这个函数创建的随机张量不能有区间[-2, 2]之外的点,而使用tf..函数可能会出现区间[点除了-2,2]。

设置随机种子

如果要保证多次生成的随机数张量完全相同,可以设置相同的随机种子。

tf.random.set_seed(10)
a = tf.random.normal([1,5])
print(a)
tf.random.set_seed(10)
b = tf.random.normal([1,5])
print(b)

打印结果:

tf.([[-0. 0. -0. -0. -0.]], shape=(1, 5), dtype= )

tf.([[-0. 0. -0. -0. -0.]], shape=(1, 5), dtype= )

平均分配

函数签名:tf..(shape,,,dtype)

形状:张量形状

: 最小闭合区间

: 最大打开间隔

dtype:数据类型默认

c = tf.random.uniform([2, 2], minval=0, maxval=1)
print(c)

打印结果:

tf.(

[[0. 0.]

[0. 0. ]], 形状=(2, 2), dtype=)

序列张量

函数签名:tf.range(start, limit, delta, dtype)

start: 开始号闭合区间

限制:结束数字开区间

delta:默认步长为 1

dtype:默认自动推断数据类型

用法如下:

#创建0-9序列
a = tf.range(10)
print(a)

打印结果:

tf.([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

#创建偶数序列
b = tf.range(10,delta=2)
print(b)

打印结果:

tf.([0 2 4 6 8], shape=(5,), dtype=int32)

#创建奇数序列
c = tf.range(1,10,delta=2)
print(c)

打印结果:

tf.([1 3 5 7 9], shape=(5,), dtype=int32)

张量的四种操作

注意只有同阶张量才能做四次算术运算

加法:tf.add

a = tf.constant([1,2,3])
b = tf.constant([1,2,3])
print(tf.add(a,b))

打印结果:

tf.([2 4 6], shape=(3,), dtype=int32)

减法:tf。

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.subtract(a,b))

打印结果:

tf.([2 4 3], shape=(3,), dtype=int32)

乘法:ttf。

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.multiply(a,b))

打印结果:

tf.([ 0 0 18], shape=(3,), dtype=int32)

师:tf。

a = tf.constant([2,4,6])
b = tf.constant([0,0,3])
print(tf.divide(b,a))

打印结果:

tf.([0. 0. 0.5], shape=(3,), dtype=)

平方、幂和根

平方张量:tf.

a = tf.constant([1,2,3,4,5])
print(tf.square(a))

打印结果:

tf.([ 1 4 9 16 25], shape=(5,), dtype=int32)

为张量供电:tf.pow

a = tf.range(5) #创建一个序列张量 0 1 2 3 4
b = tf.pow(a,3) #求序列张量的3次方
print(b)

打印结果:

tf.([ 0 1 8 27 64], shape=(5,), dtype=int32)

如果说要计算a张量的b张量的幂,相当于将a张量的每个位置的元素取b张量中对应元素的幂。

a = tf.constant([[2,2],[2,2]])
b = tf.constant([[1,2],[3,4]])
print(tf.pow(a,b))

打印结果:

tf.(

[[ 24]

[ 8 16]],形状=(2,2),dtype=int32)

如果指数是分数,那么它就是根运算。例如,如果指数为 0.5,则张量的平方根是逐个元素计算的。

a = tf.constant([1,4,9,16,25],dtype=tf.float32)
print(tf.pow(a,0.5))

打印结果:

tf.([1. 2. 3. 4. 5.], shape=(5,), dtype=)

根号会涉及小数点,所以张量的元素类型必须是浮点型

张量的平方根:tf.sqrt

a = tf.constant([1,4,9,16,25],dtype=tf.float64)
print(tf.sqrt(a))

打印结果:

tf.([1. 2. 3. 4. 5.], shape=(5,), dtype=)

其他操作

除了常用的操作外,还提供了其他更广泛的操作。

tf.sign(x) 取符号

tf.abs(x) 求绝对值

tf.(x) 负数

tf.(x) 求倒数

tf.ceil(x) 向上取整

tf.floor(x) 向下舍入

tf.round(x) 四舍五入

tf.(x,y) 返回两个张量的最大值(将两个量的元素一一比较,取最大值,最后返回一个新的张量)

tf.(x,y) 返回两个张量的最小值(将两个量的元素一一比较,取最小值,最后返回一个新的张量)

重载运算符

为了方便使用,对常用的数学运算符进行了重载,让我们可以直接对张量进行运算,而无需显式调用函数进行运算。

例如,除此之外,可以直接将 tf.add(a,b) 写成 a+b

a = tf.constant([1,2,3])
b = tf.constant([1,2,3])
print(tf.add(a,b))
print(a+b)

打印结果:

tf.([2 4 6], shape=(3,), dtype=int32)

tf.([2 4 6], shape=(3,), dtype=int32)

矩阵乘法

矩阵乘法是线性代数中非常重要的内容。矩阵乘法的条件是第一个矩阵的列数等于第二个矩阵的行数,才可以进行乘法运算。

计算规则是第一个矩阵每一行的每个数对应地乘以第二个矩阵每一列的每个数再相加,计算结果是第一个矩阵的行数和列数第二个矩阵。数的对应值。

例如,矩阵 A:

23

4 5

矩阵 B:

1 2

3 4

将两个矩阵 AB 相乘得到一个新矩阵 C

C11:2*1 + 3*3 = 11

C12:2*2 + 3*4 = 16

C21:4*1 + 5*3 = 19

C22:4*2 + 5*4 = 28

那是:

11 16

19 28

可以使用 tf.(a,b) 函数计算矩阵乘积,也可以使用 @ 运算符进行运算。

a = tf.constant([[2,3],[4,5]])
print(a)
b = tf.constant([[1,2],[3,4]])
print(b)
print(tf.matmul(a,b))

打印结果:

tf.(

[[23]

[4 5]], 形状=(2, 2), dtype=int32)

tf.(

[[1 2]

[3 4]], shape=(2, 2), dtype=int32)

tf.(

[[11 16]

[19 28]],形状=(2,2),dtype=int32)

张量统计

常用的统计函数有四种:

tf.(,axis) 总和

tf.(,axis) 平均值

tf.(,axis) 找到最大值

tf.(,axis) 找到最小值

参数axis为0时,表示垂直操作,沿经度方向(即按列,跨行),当axis为1时,表示水平操作,沿纬度方向(即按行,跨列),如果未指定轴,则表示全局操作。

a = tf.constant([[5,3,1],[2,4,6]])
print(a)
print('纵向操作:{}'.format(tf.reduce_sum(a,0)))
print('横向操作:{}'.format(tf.reduce_sum(a,1)))
print('全局操作:{}'.format(tf.reduce_sum(a)))

打印结果:

tf.(

[[5 3 1]

[2 4 6]], 形状=(2, 3), dtype=int32)

纵向操作:[7 7 7]

水平操作:[9 12]

全球行动:21

需要说明的是,得到的结果的数据类型会与原始张量的数据类型一致,会出现以下现象:

a = tf.constant([[5,3,1],[2,4,6]])
print(a)
print(tf.reduce_mean(a,0))

打印结果:

[[5 3 1]

[2 4 6]], 形状=(2, 3), dtype=int32)

tf.([3 3 3], shape=(3,), dtype=int32)

垂直平均第一列5+2=7,除以2得到3.5,但受限于int数据类型,所以最终值为3

为了解决这个问题,我们可以将张量的数据类型转换为浮点数,然后进行统计。

a = tf.constant([[5,3,1],[2,4,6]])
print(a)
print(tf.reduce_mean(tf.cast(a,dtype=tf.float32),0))

打印结果:

tf.(

[[5 3 1]

[2 4 6]], 形状=(2, 3), dtype=int32)

tf.([3.5 3.5 3.5], shape=(3,), dtype=)

© 版权声明
THE END
喜欢就支持一下吧
点赞11赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容