Tensorflow数据类型
-
Data Container
- list 列表
- np.array 同类型数据运算,存储大的数据组,运算(转制, 加减, 乘除)
- tf.Tensor
what is tensor
- scalar :标量
- vector :矢量; 向量
- matrix :矩阵
- tensor :rank > 2
TF is computing lib
-
int float double
-
bool
-
string
tf.constant
:tensor常量
constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
with tf.device("cpu):
a= tf.constant([1])
#a在cpu的环境下创建
with tf.device('gpu') :
b = tf.range(4)
#b在gpu的环境下创建
aa = a.gpu()
#将cpu数据变量变为gpu的数据变量,同理cpu也是如此
b.ndim :查看数据的维度
tf.rank(b):返回b的秩
check Tensor Type
isinstance(a, tf.tensor)
判断一个对象是否是一个已知的类型isinstance(object, classinfo)
tf.is_tensor(b)
3.a.dtype
- tf.is_tensor(b):判断b是不是tensor数据类型
- np.arange:创建一个等差数组
np.arange([start, ]stop, [step, ]dtype=None)
convert
aa = tf.convert_to_tensor(a)
numpy数据类型转换成tensortf.cast(aa, dtype = tf.float32)
数据类型转换tf.Variable(a)
变量a经过tf.Variable后自动增加了可求导的属性即trainable=True
,反向梯度传播时模型参数可以训练
##创建一个Tensor
- from numpy, list
- zeros, ones
- fill
- random
- constant
- Application
tf.convert_to_tensor(np.ones([2,3]))
Out[37]:
<tf.Tensor: id=35, shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.]])>
tf.convert_to_tensor(np.zeros([2,3]))
#传入一个shape为[2,3]的martix
Out[38]:
<tf.Tensor: id=39, shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
[0., 0., 0.]])>
tf.zero([])
#传入的是一个shape不是data
tf.convert_to_tensor([1,2])
tf.ones_like(a)#等同于tf.ones(a.shape)
tf.fill(shape(),num)
tf.fill([2,2],0)
Out[42]:
<tf.Tensor: id=57, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
[0, 0]])>
tf.random.normal([2,2],mean=1,stddev=1)
从服从指定正太分布的数值中取出随机数
参数 :
- shape: 输出张量的形状,必选
- mean: 正态分布的均值,默认为0
- stddev: 正态分布的标准差,默认为1.0
- dtype: 输出的类型,默认为tf.float32
- seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
- name: 操作的名称
tf.random.truncated_normal([2,2],mean=0, stddev=1)
截断正态分布
truncated_normal
jhn9-0[pp仅仅只是截取了正太分布某一个范围的数据并不是全部数据。
用法
tf.random.truncated_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.dtypes.float32,
seed=None,
name=None
)
tf.random.Uniform([2,2],minval=0,maxval=1)
均匀分布
random_uniform(
shape,
minval=0,
maxval=None,
dtype=tf.float32,
seed=None,
name=None
)
-
shape:一维整数张量或 Python 数组.输出张量的形状.
-
minval:dtype 类型的 0-D 张量或 Python 值;生成的随机值范围的下限;默认为0.
-
maxval:dtype 类型的 0-D 张量或 Python 值.要生成的随机值范围的上限.如果 dtype 是浮点,则默认为1 .
-
dtype:输出的类型:float16、float32、float64、int32、orint64.
-
seed:一个 Python 整数.用于为分布创建一个随机种子.查看 tf.set_random_seed 行为.
-
name:操作的名称(可选).
Random Permutation
tf.gather
:用一个一维的索引数组,将张量中对应索引的向量提取出来
tf.gather()
首先来看官方的API说明
tf.gather(
params,
indices,
validate_indices=None,
axis=None,
batch_dims=0,
name=None
)
其中我们比较关心的是前两个参数,params和indices
params :代表需要切片的张量;
indices :切片的索引;
axis: 取哪个维度
idx = tf.range(10)
idx = tf.random.shuffle(idx)
a = tf.random.normal([10,784])
b = tf.random.uniform([10],maxval = 10, dtype = tf.int32)
a = tf.gather(a,idx)
b = tf.gather(b,idx)
tf.constant
创建常量
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
Typical Dim data
Scale的典型应用
- loss = mse(out,y)
- accuracy
Vector的典型应用 - Bias
- [out_dim]
Matrix的典型应用
- [out_dim]
- input x :[b,vec_dim]
- weight: [input_dim, output_dim]
Dim = 3的典型应用自然语言处理 - x = [b, seq_len,word_dim]
Dim = 4 的典型应用 - image:[b ,h, w, 3]
- feature maps:[b, h, w, c]
Dim = 5 Tensor - Sigle task:[b,h,w,3]
- meta-learning:[task_b, b, h, w, 3]
Tensorflow 索引与切片
Indexing
-
Basic indexing
- [idx][idx][idx]
-Same with numpy
- [idx][idx][idx]
-
[idx, idx, …]
-
[start:end]
[start:end)- a[-1:]
- a[-2:]
- a[:2]
- a[:-1]
切片返回的shape
为Vector
-
[::]
a = tf.random.uniform([4,28,28,3])
#a.shape = [4,28,28,3]
a[0].shape
# Out[23]: TensorShape([28, 28, 3])
a[0].shape
# Out[24]: TensorShape([28, 28, 3])
a[0,:,:,:].shape
# Out[25]: TensorShape([28, 28, 3])
a[0,1,:,:].shape
# Out[26]: TensorShape([28, 3])
# ","分隔维度
- start🔚step
- ::step
a.shape
# Out[28]: TensorShape([4, 28, 28, 3])
a[0:2, :, :, :].shape
# Out[29]: TensorShape([2, 28, 28, 3])
a[:, 0:28:2, :, :].shape
# Out[30]: TensorShape([4, 14, 28, 3])
a[:, 0:28:2, 0:28:2, :].shape
# Out[31]: TensorShape([4, 14, 14, 3])
a[:, ::2, 0:28:2, :].shape
# Out[32]: TensorShape([4, 14, 14, 3])
a[:, ::2, ::4, :].shape
# Out[33]: TensorShape([4, 14, 7, 3])
倒采数据
a = tf.range(4)
a[::-1]
Out[35]: <tf.Tensor: id=158, shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0])>
a[::-2]
Out[36]: <tf.Tensor: id=166, shape=(2,), dtype=int32, numpy=array([3, 1])>
...
代表任意长的:
使选择元组的长度与数组的维度相同。
Selective Indexing
- tf.gather
- tf.gather_nd
_ tf.boolean_mask
tf.gather_nd
联合索引号对应的个体,或者是联合索引号集合对应个体的集合
tf.gather_nd(
params, //数据
indices, //联合索引号对应的个体,或者是联合索引号集合对应个体的集合
batch_dims=0,
name=None
# 把最内层作为索引indexing
)
tf.boolean_mask
通过布尔值 过滤元素
boolean_mask(tensor, mask, name="boolean_mask", axis=None):
-
axis
所需过滤的维度 -
tensor:被过滤的元素
-
mask:一堆 bool 值,它的维度不一定等于 tensor
-
return: mask 为 true 对应的 tensor 的元素
-
当 tensor 与 mask 维度一致时,return 一维