本文记录的是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阶的数组(列表)。判断张量是几阶,就看有几个[]
创建张量的一般方式:
tf.constant(张量内容, dtype=数据类型[可选])
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])>
a.dtype
tf.int64
a.shape
TensorShape([3])
print(a.shape)
(3,)
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]])>
b.dtype
tf.int64
b.shape
TensorShape([2, 3])
print(b.shape)
(2, 3)
方式1:通过numpy数组来创建张量:
array = np.eye(4,3)
array
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
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]])>
c.dtype
tf.int64
print(c.shape)
(4, 3)
方式2:将numpy的数据类型转换为Tensor数据类型
tf.convert_to_tensor(数据名,dtype=数据类型[可选])
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])>
arr_to_tf.shape
TensorShape([5])
type(arr_to_tf)
tensorflow.python.framework.ops.EagerTensor
维度的记忆方式:
tf.zeros(3)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>
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)>
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]])>
tf.ones(3)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
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)>
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)>
tf.fill([2,3],8) # 指定shape和填充的数值
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[8, 8, 8],
[8, 8, 8]], dtype=int32)>
tf.fill([2,3],8) # 指定shape和填充的数值
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[8, 8, 8],
[8, 8, 8]], dtype=int32)>
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
tf.random.normal(维度, mean=均值, stddev=标准差)
生成截断式正态分布的随机数
tf.random.truncated_normal(维度, mean=均值, stddev=标准差)
在tf.random.truncated_normal中如果随机数的取值在
之外,则重新生成,保证值在均值附近
标准差计算公式:
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)>
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)>
tf.random.uniform(维度,minval=最小值,maxval=最大值)
区间是前闭后开:[minval,maxval)
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)>