Fork me on GitHub

深度学习-TensorFlow张量和常用函数

北京大学深度学习1:TensorFlow张量和常用函数

本文记录的是TensorFlow2.0中的张量基础知识和常用函数,主要内容包含:

  • 张量类型
  • 张量数据类型
  • 张量创建
  • 常用函数

张量类型

维数 名字 例子
0-D 0 标量scalar s = 1,2,3
1-D 1 vector v = [1,2,3]
2-D 2 matrix m = [[1,2,3],
[4,5,6]]
n-D n tensor t = [[[ (有n个括号)

张量可以表示0-n阶的数组(列表)。判断张量是几阶,就看有几个[]

TensorFlow数据类型

  1. tf.int, tf.float:tf.int32、tf.float32、tf.float64
  2. tf.bool:tf.constant([True,False])
  3. tf.string:tf.constant(“hello world!”)

创建张量Tensor

创建张量的一般方式:

1
tf.constant(张量内容, dtype=数据类型[可选])

直接生成

1
2
3
4
5
import tensorflow as tf
import numpy as np

a = tf.constant([1,2,3], dtype=tf.int64)
a
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 2, 3])>
1
a.dtype
tf.int64
1
a.shape
TensorShape([3])
1
print(a.shape)
(3,)
1
2
b = tf.constant([[1,2,3],[4,5,6]], dtype=tf.int64)
b
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 2, 3],
       [4, 5, 6]])>
1
b.dtype
tf.int64
1
b.shape
TensorShape([2, 3])
1
print(b.shape)
(2, 3)

基于numpy数组

方式1:通过numpy数组来创建张量:

1
2
array = np.eye(4,3)
array
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])
1
2
c = tf.constant(array, dtype=tf.int64)
c
<tf.Tensor: shape=(4, 3), dtype=int64, numpy=
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1],
       [0, 0, 0]])>
1
c.dtype
tf.int64
1
print(c.shape)
(4, 3)

方式2:将numpy的数据类型转换为Tensor数据类型

1
tf.convert_to_tensor(数据名,dtype=数据类型[可选])
1
2
3
4
arr1 = np.arange(5)

arr_to_tf = tf.convert_to_tensor(arr1, dtype=tf.int64)
arr_to_tf
<tf.Tensor: shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>
1
arr_to_tf.shape
TensorShape([5])
1
type(arr_to_tf)
tensorflow.python.framework.ops.EagerTensor

创建特殊张量

维度的记忆方式:

  1. 一维:直接写个数
  2. 二维:用[行, 列]表示
  3. 多维:用[n,m,j,k…]表示

全0张量

1
tf.zeros(3)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>
1
tf.zeros([2,5])  # 默认数据类型是float32
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]], dtype=float32)>
1
tf.zeros([4,3],dtype=tf.int64)  # 指定类型
<tf.Tensor: shape=(4, 3), dtype=int64, numpy=
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])>

全1张量

1
tf.ones(3)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
1
tf.ones([3,5])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]], dtype=float32)>
1
tf.ones([3,5],dtype=tf.int32)
<tf.Tensor: shape=(3, 5), dtype=int32, numpy=
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]], dtype=int32)>

指定值的张量

1
tf.fill([2,3],8)  # 指定shape和填充的数值
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[8, 8, 8],
       [8, 8, 8]], dtype=int32)>
1
tf.fill([2,3],8)  # 指定shape和填充的数值
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[8, 8, 8],
       [8, 8, 8]], dtype=int32)>
1
tf.fill([2,3],5.5)  # 填充浮点数
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[5.5, 5.5, 5.5],
       [5.5, 5.5, 5.5]], dtype=float32)>

符合正态分布的张量

生成符合正态分布的随机数,默认均值是0,标准差是1

1
tf.random.normal(维度, mean=均值, stddev=标准差)

生成截断式正态分布的随机数

1
tf.random.truncated_normal(维度, mean=均值, stddev=标准差)

在tf.random.truncated_normal中如果随机数的取值在$(u-2\sigma, u+2\sigma)$之外,则重新生成,保证值在均值附近

  • $\u$:均值
  • $\sigma$:标准差

标准差计算公式:

$$\sigma=\sqrt\frac{\sum^{n}_{i=1}(x_i-\hat x)^2}{n} $$

1
tf.random.normal([2,2],mean=0.5,stddev=1)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.4578526, 1.7715421],
       [0.7767614, 0.9287627]], dtype=float32)>
1
tf.random.truncated_normal([2,2],mean=0.5,stddev=1)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0.58692557, 0.42861888],
       [1.0456834 , 0.16730729]], dtype=float32)>

生成均匀分布的张量

1
tf.random.uniform(维度,minval=最小值,maxval=最大值)

区间是前闭后开:[minval,maxval)

1
tf.random.uniform([3,3],minval=1,maxval=3)
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1.112915 , 2.621307 , 2.4389098],
       [1.9054191, 1.19591  , 2.1409607],
       [1.9407322, 1.2102165, 2.0343587]], dtype=float32)>

Tensoflow常用函数

本文记录的是TensorFlow中常用的函数

  • tf.cast:强制数据类型转换
  • tf.reduct_mean/sum:求和或均值
  • tf.reduce_max/min:求最值
  • tf.Variable:标记变量
  • 四则运算
  • tf.data.Dataset.from_tensor_slices:特征和标签配对

1
2
import tensorflow as tf
import numpy as np

理解axis

在一个二维张量或者数组中,通过改变axis=0或1来控制执行的维度

  • 0:表示经度,跨行,down
  • 1:表示纬度,跨列,across

如果不指定的话,则全员参与计算

tf.cast

强制tensor转换为该数据类型

1
tf.cast(张量名, dtype=数据类型)

In [2]:

1
2
x1 = tf.constant([1,2,3],dtype=tf.float64)
x1

Out[2]:

1
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 2., 3.])>

In [3]:

1
2
x2 = tf.cast(x1, dtype=tf.int64)  # 转换数据类型
x2

Out[3]:

1
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 2, 3])>

tf.reduce_max/min

计算张量维度上的最值

In [4]:

1
2
print(tf.reduce_max(x2))
tf.Tensor(3, shape=(), dtype=int64)

In [5]:

1
2
print(tf.reduce_min(x2))
tf.Tensor(1, shape=(), dtype=int64)

tf.reduct_mean/sum

计算张量沿着指定维度的平均值或者和

In [6]:

1
2
3
4
i = tf.constant([[1,2,3],
[4,5,6]
],dtype=tf.float64)
i

Out[6]:

1
2
3
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[1., 2., 3.],
[4., 5., 6.]])>

In [7]:

1
tf.reduce_mean(i)  # 默认是全部数值的均值

Out[7]:

1
<tf.Tensor: shape=(), dtype=float64, numpy=3.5>

In [8]:

1
tf.reduce_mean(i, axis=0)

Out[8]:

1
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([2.5, 3.5, 4.5])>

In [9]:

1
tf.reduce_mean(i, axis=1)

Out[9]:

1
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([2., 5.])>

In [10]:

1
tf.reduce_sum(i)  # 1+2+3+4+5+6

Out[10]:

1
<tf.Tensor: shape=(), dtype=float64, numpy=21.0>

In [11]:

1
tf.reduce_sum(i, axis=0)

Out[11]:

1
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([5., 7., 9.])>

In [12]:

1
tf.reduce_sum(i, axis=1)

Out[12]:

1
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([ 6., 15.])>

tf.Variable

tf.Variable()将函数标记为可训练,被标记的变量会在反向传播中记录梯度信息。

神经网络中常用该函数来标记待训练的参数。

In [13]:

1
2
w = tf.Variable(tf.random.normal([2,2], mean=0, stddev=1))
w

Out[13]:

1
2
3
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[-1.3200597 , 1.123157 ],
[ 0.4855043 , -0.06241844]], dtype=float32)>

上面变量w的解释:

  1. 先生成正态分布的随机数
  2. 再将随机数标记为可训练,这样在神经网络的反向传播中就可以通过梯度下降更新参数w了

数学运算

  • 四则运算:tf.add(t1,t2)、tf.subtract、tf.multiply、tf.divide
  • 平方、次方与开方:tf.square、tf.pow(t,n次方)、tf.sqrt
  • 矩阵乘:tf.matmul

注意:只有维度相同的两个张量才能进行四则运算

tf.data.Dataset.from_tensor_slices

切分传入张量的第一维度,生成输入特征和标签对,构建数据集:特征和标签配对

Numpy和Tensor格式都可以使用该语句读入数据

1
data = tf.data.Dataset.from_tensor_slices((输入特征,标签))

In [14]:

1
2
3
4
5
6
features = tf.constant([12,15,20,27])
labels = tf.constant([0,1,0,1])

dataset = tf.data.Dataset.from_tensor_slices((features,labels))
print(dataset)
<TensorSliceDataset element_spec=(TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None))>

In [15]:

1
2
3
4
5
6
for element in dataset:
print(element)
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=15>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=20>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=27>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)

本文标题:深度学习-TensorFlow张量和常用函数

发布时间:2022年10月18日 - 23:10

原始链接:http://www.renpeter.cn/2022/10/18/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0-TensorFlow%E5%BC%A0%E9%87%8F%E5%92%8C%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea