文章目录
什么是tensor
常见的数据载体(data container):
- list(可存放不同类型数据);
- np.array(科学计算库,数值运算,但不支持自动求导);
- tf.Tensor(也可视为一个与numpy类似的科学计算库)。
TensorFlow使用张量(Tensor)作为数据的基本单位。tensor在概念上类似于多维数组,可以用它来描述数学中的标量(0维数组)、向量(一维数组)、矩阵(2维数组)等各种量。
tensor的属性
张量的三个重要属性是其形状、(存放的数据)类型和值。可以通过shape、dtype属性和 numpy() 方法获得。
属性/方法 | 描述 |
---|---|
a.shape | |
a.dtype | 存放的数据类型 |
a.numpy() | 获取a中存放的值 |
a.ndim | 获得a的维度 |
用 tf.is_tensor(a) 可以判断a是否为tensor(返回布尔型)。
创建tensor
- 用 tf.constant()、tf.Variable() 创建tensor
a = tf.constant(10) # 标量
print(a) # tf.Tensor(10, shape=(), dtype=int32)
b = tf.constant([2.], dtype=tf.double) # 加上中括号就为一维向量
print(b) # tf.Tensor([2.], shape=(1,), dtype=float64)
print(b.dtype) # <dtype: 'float64'>
c = tf.constant([True, False])
print(c) # tf.Tensor([ True False], shape=(2,), dtype=bool)
d = tf.constant("hello world")
print(d) # tf.Tensor(b'hello world', shape=(), dtype=string)
tf.Variable专为神经网络中可训练的参数设置(有trainable属性且为True)
a = tf.Variable(initial_value=1)
print(a) # <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>
print(a.dtype) # <dtype: 'int32'>
print(a.numpy()) # 1
b = tf.range(5)
print(b.trainable) # 报错,无此属性 AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'trainable'
c = tf.Variable(a)
print(c) # <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>
print(c.trainable) # True
- tf.range()、tf.linspace(start, end, number)
a = tf.range(5)
print(a) # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)
print(a.numpy()) # [0 1 2 3 4]
- 使用tf.convert_to_tensot() 将numpy或list转换为tensor
import tensorflow as tf
import numpy as np
a = np.arange(5)
b = tf.convert_to_tensor(a)
c = tf.convert_to_tensor(a, dtype=tf.int32)
print(b.dtype) # <dtype: 'int32'>
print(c.dtype) # <dtype: 'int32'>
import tensorflow as tf
a = tf.convert_to_tensor([1, 5, 8])
print(a) # tf.Tensor([1 5 8], shape=(3,), dtype=int32)
- tf.zeros()、tf.zeros_like()、tf.ones()、tf.ones_like()
注:tf.zeros()、tf.ones() 括号中接收的是数组形状
a = tf.zeros([])
print(a.shape) # () 即,创建了一个标量
b = tf.zeros([1])
print(b.shape) # (1,) 创建一维向量
c = tf.zeros([3, 6])
print(c.shape)
a = tf.range(5)
b = tf.zeros_like(a)
print(b.numpy()) # [0 0 0 0 0]
- 用 tf.fill() 创建值相同的tensor
a = tf.fill([4, 6], 8)
print(a.numpy())
6. 创建正态分布、均匀分布随机初始化的tensor
(1)正态分布
a = tf.random.normal([2, 3], mean=0, stddev=2) # stddev为标准差
print(a)
截断正态分布,避免数据落在sigmoid函数两端导致的梯度消失问题(如参数初始化时)。
a = tf.random.truncated_normal([2, 6], mean=0, stddev=2)
print(a)
函数注释:The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.
大于两倍标准差的数值被舍弃。
(2)均匀分布
a = tf.random.uniform([3, 6], minval=1, maxval=6) # 还可指定dtype
print(a)
7. Random Permutation
ind = tf.range(10)
ind = tf.random.shuffle(ind)
a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
a = tf.gather(a, ind)
b = tf.gather(b, ind)
创建方法 | 说明 |
---|---|
tf.range(n) | 创建tensor,值为从0~n-1的向量 |
tf.constant() | 创建常量tensor |
tf.Variable() | 创建变量tensor,可训练(trainable属性为True) |
tf.convert_to_tensor(numpy数组或list) | 将numpy数组或list转换为tensor |
tf.zeros( [shape] )、tf.ones( [shape] ) | 创建形状为shape的全为0/1的tensor |
tf.zeros_like(b)、tf.ones_like(b) | 创建与b形状相同的全为0/1的tensor |
tf.fill([shape], num) | 创建一个值全为num的shape形tensor |
tf.random.normal( [shape], mean=, steddv = ) | 正态分布 |
tf.random.truncated_normal([shape], mean=, steddv = ) | 截断正态分布 |
tf.random.uniform([shape],minval=, maxvla =, dtype = ) | 均匀分布 |
tensor数据类型转换
tf.int、tf.double、tf.float 之间相互转换( tf.cast() )
a = np.arange(5)
b = tf.convert_to_tensor(a, dtype=tf.int32)
c = tf.cast(b, tf.float32)
print(c.