TensorFlow创建初始数据(常量、⼀维、⼆维、三维、多维)的⽅法实例
使⽤ TensorFlow 时,经常需要创建⼀些初始数据(注意这⾥的初始数据不是初始化权重的 initializer),通常可以通过 numpy、python ⾃带的列表等⽅式初始⼀个矩阵,然后再将其转换为 tensor 使⽤,但是实际上 TensorFlow ⾃⾝就具有这些创建初始数据的⽅法,下⾯就对⼀些常⽤的⽅法进⾏总结:
⼀、环境
Python 3.7.3 (Anaconda 3)
TensorFlow 1.14.0
⼆、实例
为了简化操作和便于观察,本⽂操作全部采⽤ TensorFlow 的⽴即执⾏(eager execution)模式,⾸先在所有代码最前⾯启⽤ eager execution
>>>import tensorflow as tf
>>> tf.enable_eager_execution()
1、tf.constant 创建初始数据(元素为固定常数)
位置参数 value:可接受常数(constant)或列表(list)
(1)常数张量
>>> tensor_constant = tf.constant(1)
>>> tensor_constant
<tf.Tensor:id=10, shape=(), dtype=int32, numpy=1>
>>> tensor_constant.numpy()
1
(2)1 维张量
>>> tensor_1d = tf.constant([1,2,3,4,5,6,7])
>>> tensor_1d
<tf.Tensor:id=13, shape=(7,), dtype=int32, numpy=array([1,2,3,4,5,6,7], dtype=int32)>
(3)2 维张量(根据关键字参数 shape 将位置参数 value 中的数据⾃动切分为指定的维度)
>>> tensor_2d = tf.constant([1,2,3,4,5,6], shape=[2,3])
>>> tensor_2d
<tf.Tensor:id=18, shape=(2,3), dtype=int32, numpy=
array([[1,2,3],
[4,5,6]], dtype=int32)>
(4)3 维张量(根据关键字参数 shape 将位置参数 value 中的数据⾃动切分为指定的维度)
>>> tensor_3d = tf.constant([1,2,3,4,5,6], shape=[1,2,3])
>>> tensor_3d
<tf.Tensor:id=28, shape=(1,2,3), dtype=int32, numpy=
array([[[1,2,3],
[4,5,6]]], dtype=int32)>
(5)多维张量(以此类推)(根据关键字参数 shape 将位置参数 value 中的数据⾃动切分为指定的维度)
>>> tensor_nd = tf.constant([1,2,3,4,5,6,...,n], shape=[指定维度])
(6)多维张量(张量中的所有元素的数值相同,以 2 维张量为例,其他维度张量,可参看以上实例改变位置参数 shape 实现)
>>> tensor_2d_same_elements = tf.constant(1.5, shape=[2,3])
>>> tensor_2d_same_elements
<tf.Tensor:id=2, shape=(2,3), dtype=float32, numpy=
array([[1.5,1.5,1.5],
[1.5,1.5,1.5]], dtype=float32)>
2、tf.zeros 创建元素全为 0 的初始数据
>>> tensor_zeros = tf.zeros([2,3])
>>> tensor_zeros
<tf.Tensor:id=7, shape=(2,3), dtype=float32, numpy=
array([[0.,0.,0.],
[0.,0.,0.]], dtype=float32)>
3、tf.zeros_like 创建元素全为 0 的初始数据(维度由位置参数 tensor 的维度指定)
>>> referenced_tensor = tf.constant([[1,2,3],[4,5,6]])
>>> referenced_tensor
<tf.Tensor:id=10, shape=(2,3), dtype=int32, numpy=
array([[1,2,3],
[4,5,6]], dtype=int32)>
>>> tensor_zeros = tf.zeros_like(referenced_tensor)
>>> tensor_zeros
<tf.Tensor:id=11, shape=(2,3), dtype=int32, numpy=
array([[0,0,0],
[0,0,0]], dtype=int32)>
4、tf.ones 创建元素全为 1 的初始数据
>>> tensor_ones = tf.ones([2,3])
>>> tensor_ones
<tf.Tensor:id=17, shape=(2,3), dtype=float32, numpy=
array([[1.,1.,1.],
[1.,1.,1.]], dtype=float32)>
5、tf.ones_like 创建初始数据(元素全为 1,维度由位置参数 tensor 的维度指定)
>>> referenced_tensor = tf.constant([[1,2,3],[4,5,6]])
>>> referenced_tensor
<tf.Tensor:id=19, shape=(2,3), dtype=int32, numpy=
array([[1,2,3],
[4,5,6]], dtype=int32)>
>>> tensor_ones = tf.ones_like(referenced_tensor)
>>> tensor_ones
<tf.Tensor:id=23, shape=(2,3), dtype=int32, numpy=
array([[1,1,1],
[1,1,1]], dtype=int32)>
6、tf.eye 创建单位矩阵数据(对⾓线为 1)或⼀组矩阵
>>> identity_matrix = tf.eye(2)
>>> identity_matrix
<tf.Tensor:id=29, shape=(2,2), dtype=float32, numpy=
array([[1.,0.],
[0.,1.]], dtype=float32)>
>>> identity_matrix1 = tf.eye(2, num_columns=3)
>>> identity_matrix1
<tf.Tensor:id=52, shape=(2,3), dtype=float32, numpy=
array([[1.,0.,0.],
[0.,1.,0.]], dtype=float32)>
>>> batch_identity_matrix = tf.eye(2, batch_shape=[3])
>>> batch_identity_matrix
<tf.Tensor:id=34, shape=(3,2,2), dtype=float32, numpy=
array([[[1.,0.],
[0.,1.]],
[[1.,0.],
[0.,1.]],
[[1.,0.],
[0.,1.]]], dtype=float32)>
>>> batch_identity_matrix1 = tf.eye(2, batch_shape=[1,2])
>>> batch_identity_matrix1
<tf.Tensor:id=44, shape=(1,2,2,2), dtype=float32, numpy=
array([[[[1.,0.],
[0.,1.]],
[[1.,0.],
[0.,1.]]]], dtype=float32)>
7、tf.linalg.diag 创建对⾓初始数据
>>> tensor_diagonal = tf.linalg.diag([1,2,3])
>>> tensor_diagonal
<tf.Tensor:id=59, shape=(3,3), dtype=int32, numpy=
array([[1,0,0],
[0,2,0],
[0,0,3]], dtype=int32)>
8、tf.fill 创建初始数据(由同⼀个标量值填充)
>>> tensor_filled = tf.fill([2,3],1.5)
>>> tensor_filled
<tf.Tensor:id=56, shape=(2,3), dtype=float32, numpy=
array([[1.5,1.5,1.5],
[1.5,1.5,1.5]], dtype=float32)>
9、tf.linspace 创建等差序列的初始数据(tf.linspace最后⼀个参数指定的是数量,tf.range最后⼀个参数指定的是间隔)
>>> tensor_linspace = tf.linspace(0.0,1.0,5)
>>> tensor_linspace
<tf.Tensor:id=77, shape=(5,), dtype=float32, numpy=array([0.,0.25,0.5,0.75,1.], dtype=float32)>
10、tf.range 创建⼀个序列初始数据(tf.range最后⼀个参数指定的是间隔,tf.linspace最后⼀个参数指定的是数量)
>>> tensor_range = tf.range(3,18,3)
>>> tensor_range
<tf.Tensor:id=88, shape=(5,), dtype=int32, numpy=array([3,6,9,12,15], dtype=int32)>
以下为 tf.random 下的⽅法:
11、tf.random.uniform 或 tf.random_uniform 创建均匀分布的初始数据
array([[0.4853586,0.3163376,0.90865386],
[0.30343163,0.5796678,0.73963296]], dtype=float32)>
>>> tensor_uniform = tf.random_uniform([2,3],0,1)
>>> tensor_uniform
<tf.Tensor:id=104, shape=(2,3), dtype=float32, numpy=
array([[0.21101034,0.43106842,0.750067],
[0.28341413,0.13158596,0.7042546]], dtype=float32)>
12、al 或 tf.random_normal 创建正态分布的初始数据
>>> tensor_normal = al([2,3],mean=0.0, stddev=1.0)
>>> tensor_normal
<tf.Tensor:id=111, shape=(2,3), dtype=float32, numpy=
array([[0.543633,-1.1510744,-0.5166318],
[0.7066859,-0.18293963,-1.1034325]], dtype=float32)>
>>> tensor_normal = tf.random_normal([2,3],mean=0.0, stddev=1.0)
>>> tensor_normal
<tf.Tensor:id=118, shape=(2,3), dtype=float32, numpy=
array([[-0.09788968,2.1148856,-1.1407292],
[0.5654968,-1.6997695,-0.2044372]], dtype=float32)>
13、uncated_normal 或 tf.truncated_normal 创建截断正态分布的初始数据(超过 2 倍⽅差的数值会被丢弃然后重采样)
>>> tensor_truncated_normal = uncated_normal([2,3], mean=0.0, stddev=1.0)
>>> tensor_truncated_normal
<tf.Tensor:id=125, shape=(2,3), dtype=float32, numpy=
array([[1.4835494,-0.41608435,-0.5880263],
[0.5663572,0.8002272,-0.9048173]], dtype=float32)>
>>> tensor_truncated_normal = tf.truncated_normal([2,3], mean=0.0, stddev=1.0)
>>> tensor_truncated_normal
<tf.Tensor:id=132, shape=(2,3), dtype=float32, numpy=
array([[1.3685434,-0.44088304,-0.9568191],
[-0.5752376,-1.1033012,0.4550819]], dtype=float32)>
14、tf.random.gamma 或 tf.random_gamma 创建伽马分布的初始数据
(需要注意:位置参数 alpha 和关键词参数 beta 需要是⼀个 tensor 或 Python 数值或 n 为数组)
array([[0.57141626,1.8922596],
[0.16391411,0.91129816],
[0.00959269,0.48148772],
[0.9698401,0.34026673],
[0.4009804,2.9951208],
[0.41322464,1.970633],
linspace numpy
[1.0898147,0.7972925],
[1.3136083,1.8393835],
[0.1075677,0.4334423],
[0.01691816,1.9474756]], dtype=float32)>
>>> tensor_gamma1 = tf.random.gamma([2,3],[0.5,1.5])
>>> tensor_gamma1
<tf.Tensor:id=167, shape=(2,3,2), dtype=float32, numpy=
array([[[1.0264944e+00,6.4605632e+00],
[8.0157153e-02,1.2645140e+00],
[6.5384345e-04,1.6259441e+00]],
[[4.3854089e+00,9.8414886e-01],
[4.3271837e-01,3.8212683e+00],
[8.1095088e-01,2.5398853e+00]]], dtype=float32)>
>>> tensor_gamma2 = tf.random.gamma([3], alpha=alpha, beta=beta)
>>> tensor_gamma2
<tf.Tensor:id=195, shape=(3,3,2), dtype=float32, numpy=
array([[[0.8173688,0.23894905],
[0.5382208,0.86303145],
[2.113059,0.81887245]],
[[0.09482953,0.15563871],
[0.5781328,0.09177698],
[1.9396044,1.0764602]],
[[0.50212723,0.2805498],
[0.3675058,0.60953206],
[1.7223202,1.2798165]]], dtype=float32)>
15、tf.random.poisson 或 tf.random_poisson 创建泊松分布的初始数据(需要注意:第⼆个位置参数指定创建张量的形状)
>>> tensor_poisson = tf.random.poisson([0.5,1.5],[3])
>>> tensor_poisson
<tf.Tensor:id=199, shape=(3,2), dtype=float32, numpy=
array([[1.,1.],
[0.,2.],
[0.,0.]], dtype=float32)>
16、tf.random.shuffle 或 tf.random_shuffle 创建打乱的初始数据

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。