TensorFlow张量
本文记录的是TensorFlow中的张量基础知识,包含:
- 张量类型
- 张量数据类型
- 张量创建
张量类型
维数 | 阶 | 名字 | 例子 |
---|---|---|---|
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数据类型
- tf.int, tf.float:tf.int32、tf.float32、tf.float64
- tf.bool:tf.constant([True,False])
- tf.string:tf.constant(“hello world!”)
创建张量Tensor
创建张量的一般方式:
1 | tf.constant(张量内容, dtype=数据类型[可选]) |
直接生成
1 | import tensorflow as tf |
<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 | b = tf.constant([[1,2,3],[4,5,6]], dtype=tf.int64) |
<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 | array = np.eye(4,3) |
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
1 | c = tf.constant(array, dtype=tf.int64) |
<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 | arr1 = np.arange(5) |
<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
创建特殊张量
维度的记忆方式:
- 一维:直接写个数
- 二维:用[行, 列]表示
- 多维:用[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)>