tf.convert_to_tensor
convert_to_tensor(value,dtype=None,name=None,preferred_dtype=None)
This function converts Python objects of various types toTensorobjects. It acceptsTensorobjects, numpy arrays, Python lists,and Python scalars.
将数组、列表、标量等转化为张量。
tf.split
split(value,num_or_size_splits,axis=0,num=None,name='split')
如果num_or_size_splits是数值,则沿着axis维度分成num份张量;
如果num_or_size_splits是张量,则沿着axis维度分成size_splits张量。
'value' is a tensor with shape [5, 30]
Split 'value' into 3 tensors with sizes [4, 15, 11] along dimension 1
split0,split1,split2=tf.split(value,[4,15,11],1)
tf.shape(split0)==>[5,4]
tf.shape(split1)==>[5,15]
tf.shape(split2)==>[5,11]
# Split 'value' into 3 tensors along dimension 1
split0,split1,split2=tf.split(value,num_or_size_splits=3,axis=1)
tf.shape(split0)==>[5,10]
tf.tile
tile(input,multiples,name=None)
将input重复multiples次
tf.zeros
import tensorflow as tf
sess=tf.Session()
x=tf.zeros([3,4],tf.float16)
sess.run(x)
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]], dtype=float16)
sess.run(tf.ones_like(x))
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]], dtype=float16)
sess.run(tf.fill([3,4],5))
array([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]], dtype=int32)
sess.run(tf.constant([[1,2,3],[4,5,6]]))
array([[1, 2, 3],
[4, 5, 6]], dtype=int32)
sess.run(tf.linspace(0.0,5,3))
array([ 0. , 2.5, 5. ], dtype=float32)
sess.run(tf.range(0,2,0.5))
array([ 0. , 0.5, 1. , 1.5], dtype=float32)
c = tf.constant([[1, 2], [3, 4], [5, 6]])
#未设置随机数种子tf.set_random_seed,tf.random每次运行会得到不同的值
sess.run(tf.random_normal([2,3],mean=1,stddev=4))
sess.run(tf.random_shuffle(c))
sess.run(tf.random_crop(c,[2,1]))#随机切割
array([[2],
[4]], dtype=int32)
网友评论