申明:本文非笔者原创,原文转载自:https://blog.csdn.net/vagrantabc2017/article/details/78062218
zeros_tsr = tf.zeros([2, 3])
show("tf.zeros:",zeros_tsr)
ones_tsr = tf.ones([1, 3])
show("tf.ones:",ones_tsr)
filled_tsr = tf.fill([2, 4], 8) #创建一个常量
show('tf.fill:',filled_tsr)
constant_tsr = tf.constant([1, 2, 3]) #创建一个自定义的常量
show('tf.constant:',constant_tsr)
constant2_str = tf.constant(8, tf.float32, [2,4]) #等价于fill
show('tf.constant:',constant2_str)
zeros_similar = tf.zeros_like(zeros_tsr)
show('tf.zeros_like:',zeros_similar)
ones_similar = tf.ones_like(ones_tsr)
show('tf.ones_like:',ones_similar)
linear_tsr = tf.linspace(start=0.0, stop=1, num=11) #使用样本个数,常用
show("tf.linspace:",linear_tsr)
range_tsr = tf.range(0, 1, 0.1) #等价于linspace,但使用步长,有时不方便
show('tf.range:',range_tsr)
rand_unif_tsr = tf.random_uniform([2,3], 0, 10) #[0,10)的均匀分布
show('tf.random_uniform:',rand_unif_tsr)
randnorm = tf.random_normal([2,3], 0, 0.1) #均值0,标准差 0.1
show('tf.random_normal:' , randnorm)
trunc_norm = tf.truncated_normal([2,3], 0, 0.1) #同上,但2个标准差之外的截掉
show('tf.truncated_normal',trunc_norm)
value = tf.range(1,10)
show('tf.range:', value)
shuffle_output = tf.random_shuffle(value) #洗牌
show('tf.random_shuffle:',shuffle_output)
rand_int_tsr = tf.random_uniform([3,4], 0, 100)
cropped_output = tf.random_crop(rand_int_tsr, [2,2])
show('tf.random_crop:', rand_int_tsr, cropped_output) #在3*4的矩阵上,随机取一个 2*2的子矩阵,常用于彩色图片 (高,宽,3颜色通道)
print('--------------------------------')
cropped_output_var = tf.Variable(cropped_output)
sess.run(cropped_output_var.initializer)
print(sess.run(cropped_output_var))
cropped_output_var2 = tf.Variable(tf.lin_space(7.0, 9, 3))
sess.run(cropped_output_var2.initializer)
print(sess.run(cropped_output_var2))
var1 = tf.Variable(tf.range(1,3))
var2 = tf.Variable(tf.range(4,8))
init_allvars_op = tf.global_variables_initializer() #不用每个变量都去取initializer
sess.run(init_allvars_op)
print(sess.run([var1,var2]))
a = [1, 2, 3]
show("const to tensor:" , tf.convert_to_tensor(a))
输出结果:
-------------------
tf.zeros:
[[ 0. 0. 0.]
[ 0. 0. 0.]]
-------------------
tf.ones:
[[ 1. 1. 1.]]
-------------------
tf.fill:
[[8 8 8 8]
[8 8 8 8]]
-------------------
tf.constant:
[1 2 3]
-------------------
tf.constant:
[[ 8. 8. 8. 8.]
[ 8. 8. 8. 8.]]
-------------------
tf.zeros_like:
[[ 0. 0. 0.]
[ 0. 0. 0.]]
-------------------
tf.ones_like:
[[ 1. 1. 1.]]
-------------------
tf.linspace:
[ 0. 0.1 0.2 0.30000001 0.40000001 0.5
0.60000002 0.69999999 0.80000001 0.90000004 1. ]
-------------------
tf.range:
[ 0. 0.1 0.2 0.30000001 0.40000001 0.5
0.60000002 0.70000005 0.80000007 0.9000001 ]
-------------------
tf.random_uniform:
[[ 7.20409775 6.92542934 4.91668463]
[ 2.51387954 4.47322464 0.28337955]]
-------------------
tf.random_normal:
[[ 0.05500451 -0.01932122 -0.00656942]
[ 0.27151474 0.05169746 0.08516382]]
-------------------
tf.truncated_normal
[[-0.09705421 0.18184513 -0.09811895]
[-0.00603499 0.11441358 -0.19262247]]
-------------------
tf.range:
[1 2 3 4 5 6 7 8 9]
-------------------
tf.random_shuffle:
[2 9 8 3 1 5 7 4 6]
-------------------
tf.random_crop:
[[ 99.53952026 1.27675533 68.68402863 85.94589233]
[ 21.94381905 83.0783844 88.68248749 51.50793839]
[ 16.83168411 37.43605804 72.67305756 7.39020109]]
[[ 68.68402863 85.94589233]
[ 88.68248749 51.50793839]]
--------------------------------
[[ 17.86952019 46.57141113]
[ 64.63282013 68.54146576]]
[ 7. 8. 9.]
[array([1, 2]), array([4, 5, 6, 7])]
-------------------
const to tensor:
[1 2 3]