**
一 从numpy list中得到tensor
**
—tf.convert_to_tensor()
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: tf.convert_to_tensor(np.ones([2,3]))
Out[3]:
<tf.Tensor: id=0, shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.]])>
In [4]: tf.convert_to_tensor(np.zeros([2,3]))
Out[4]:
<tf.Tensor: id=2, shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
[0., 0., 0.]])>
In [5]: tf.convert_to_tensor([1,2])
Out[5]: <tf.Tensor: id=4, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
In [6]: tf.convert_to_tensor([1,2.])
Out[6]: <tf.Tensor: id=6, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
In [7]: tf.convert_to_tensor([[1],[2.]])
Out[7]:
<tf.Tensor: id=8, shape=(2, 1), dtype=float32, numpy=
array([[1.],
[2.]], dtype=float32)>
**
二 tf.zeros( )和tf.ones( )函数创建
**
–tf.zeros( )#括号内填shape
–tf.zeros_like(a)#便捷函数,生成tensor的shape与a一致 = tf.zeros(a.shape)
–tf.ones( )#括号内填shape
–tf.ones_like(a)#便捷函数,生成tensor的shape与a一致 = tf.ones(a.shape)
In [8]: tf.zeros([]) #标量,0
Out[8]: <tf.Tensor: id=10, shape=(), dtype=float32, numpy=0.0>
In [9]: tf.zeros([1]) #shape为1
Out[9]: <tf.Tensor: id=14, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>
In [10]: tf.zeros([2,2]) #shape为2*2
Out[10]:
<tf.Tensor: id=18, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
[0., 0.]], dtype=float32)>
In [11]: tf.zeros([2,3,3]) #shape为2*3*3
Out[11]:
<tf.Tensor: id=22, shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>
In [12]: a = tf.zeros([2,3,3])
In [13]: tf.zeros_like(a)
Out[13]:
<tf.Tensor: id=27, shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>
In [14]: tf.zeros(a.shape)
Out[14]:
<tf.Tensor: id=31, shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>
In [15]: tf.ones([]) #标量
Out[15]: <tf.Tensor: id=33, shape=(), dtype=float32, numpy=1.0>
In [16]: tf.ones(1)
Out[16]: <tf.Tensor: id=37, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
In [17]: tf.ones([2])
Out[17]: <tf.Tensor: id=41, shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
In [18]: tf.ones([2,3])
Out[18]:
<tf.Tensor: id=45, shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>
In [19]: tf.ones_like(a)
Out[19]:
<tf.Tensor: id=49, shape=(2, 3, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]], dtype=float32)>
In [20]: tf.ones(a.shape)
Out[20]:
<tf.Tensor: id=53, shape=(2, 3, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]], dtype=float32)>
**
三 tf.fill( )函数创建
**
–tf.fill( )函数
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: tf.fill([2,2],0)#shape为2*2,全部填充为0,int32类型
Out[3]:
<tf.Tensor: id=2, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
[0, 0]], dtype=int32)>
In [4]: tf.fill([2,2],0.)#shape为2*2,全部填充为0,float32类型
Out[4]:
<tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
[0., 0.]], dtype=float32)>
In [5]: tf.fill([2,2],1)#shape为2*2,全部填充为1,int32类型
Out[5]:
<tf.Tensor: id=10, shape=(2, 2), dtype=int32, numpy=
array([[1, 1],
[1, 1]], dtype=int32)>
In [6]: tf.fill([2,2],9)#shape为2*2,全部填充为9,int32类型
Out[6]:
<tf.Tensor: id=14, shape=(2, 2), dtype=int32, numpy=
array([[9, 9],
[9, 9]], dtype=int32)>
**
四 随机初始化方式创建
**
–tf.random.normal([2,2], mean=0,stddev=1)#正常分布
–tf.random.truncated_normal([2,2], mean=0,stddev=1)#截断的正态分布
–tf.random.uniform([2,2],minval=0,maxval=1)#从[minval,maxval]中均匀采样回来的数据
In [7]: tf.random.normal([2,2],mean=1,stddev=1)#shape为2*2,正态分布,其均值为1,方
...: 差也为1
Out[7]:
<tf.Tensor: id=21, shape=(2, 2), dtype=float32, numpy=
array([[ 0.88008857, 1.5143192 ],
[ 1.3640549 , -0.5572401 ]], dtype=float32)>
In [8]: tf.random.normal([2,2])#shape为2*2,标准正态分布,均为值0,方差为1
Out[8]:
<tf.Tensor: id=28, shape=(2, 2), dtype=float32, numpy=
array([[ 1.1111085, -1.0393752],
[-0.5177935, -1.1112201]], dtype=float32)>
In [9]: tf.random.truncated_normal([2,2],mean=0,stddev=1)#截断的正态分布
Out[9]:
<tf.Tensor: id=35, shape=(2, 2), dtype=float32, numpy=
array([[-0.33920026, 0.10890114],
[-0.06457798, 0.19805565]], dtype=float32)>
In [10]: tf.random.uniform([2,2],minval=0,maxval=1)
Out[10]:
<tf.Tensor: id=43, shape=(2, 2), dtype=float32, numpy=
array([[0.78233325, 0.7201489 ],
[0.03125906, 0.5551493 ]], dtype=float32)>
In [11]: tf.random.uniform([2,2],minval=0,maxval=100)
Out[11]:
<tf.Tensor: id=51, shape=(2, 2), dtype=float32, numpy=
array([[38.83101 , 2.0980835],
[22.500038 , 51.655483 ]], dtype=float32)>
In [12]: tf.random.uniform([2,2],minval=-10,maxval=10,dtype=tf.int32)
Out[12]:
<tf.Tensor: id=56, shape=(2, 2), dtype=int32, numpy=
array([[-3, 2],
[ 8, -5]], dtype=int32)>
In [13]: tf.random.uniform([2,2],minval=-10,maxval=10,dtype=tf.float32)
Out[13]:
<tf.Tensor: id=64, shape=(2, 2), dtype=float32, numpy=
array([[ 4.1876316, -4.222634 ],
[-5.8720756, -4.5957875]], dtype=float32)>
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: idx = tf.range(10)
In [4]: idx = tf.random.shuffle(idx)#对idx进行随机打散
In [5]: idx
Out[5]: <tf.Tensor: id=4, shape=(10,), dtype=int32, numpy=array([5, 6, 9, 3, 7, 1, 0, 8, 2, 4], dtype=int32)>
In [7]: a = tf.random.normal([10,784])
In [8]: a = tf.gather(a,idx)#利用idx索引对a进行随机打散
In [9]: b = tf.random.uniform([10],maxval=10,dtype=tf.int32)
In [10]: b
Out[10]: <tf.Tensor: id=17, shape=(10,), dtype=int32, numpy=array([9, 7, 9, 1, 3, 0, 3, 2, 6, 4], dtype=int32)>
In [11]: b = tf.gather(b,idx)
In [12]: b
Out[12]: <tf.Tensor: id=20, shape=(10,), dtype=int32, numpy=array([0, 3, 4, 1, 2, 7, 9, 6, 9, 3], dtype=int32)>