TensorFlow random_crop和multinomial等方法学习

在学习斯坦福大学TensorFlow教程第二节课(https://www.youtube.com/watch?v=9kC836XhICU&list=PLQ0sVbIj3URf94DQtGPJV629ctn2c1zN-&index=2)的时候,遇到几个随机数生成的方法,这里学习一下。

第一个是tf.random_normal(),该方法就是用正态分布产生随机数,默认是标准正态分布。

第二个是tf.truncated_normal(),该方法类似上一个,就是多了店截断操作,具体说就是产生正太分布的值如果与均值的差值大于两倍的标准差,那就重新生成。

第三个是tf.random_uniform(),该方法则是用均匀分布产生随机值,默认浮点数范围[0, 1),整数的话maxval要指定。均匀分布也就是(a, b)范围内,概率密度f(x) = 1 / (b - a),其他地方则为0。

第四个是tf.random_suffle(),每一次都把其中的一些行换位置或者不换,代码如下:

import tensorflow as tf

a = tf.get_variable('a', [3, 2], initializer=tf.random_normal_initializer(mean=0, stddev=1))

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(a))
    print(sess.run(tf.random_shuffle(a)))

结果如下:

[[ 2.0261083  -0.34673768]
 [ 0.09152898  1.1487025 ]
 [ 1.2850556   0.97470516]]
[[ 2.0261083  -0.34673768]
 [ 1.2850556   0.97470516]
 [ 0.09152898  1.1487025 ]]

这里所有行都没变,再执行一次:

[[ 0.5839885  -0.11081421]
 [-0.4712714   0.40724093]
 [-0.12657043 -0.03069498]]
[[-0.12657043 -0.03069498]
 [ 0.5839885  -0.11081421]
 [-0.4712714   0.40724093]]

发现行的位置变了,该函数的操作效果就是这样。我在写代码的时候,不小心把tf.global_variables_initializer()写在了变量a前面,这个时候程序报错。虽然我觉得还没用sess.run(),所以tf.global_variables_initializer()位置应该可以放前面的,不过确实不行。

第五个是tf.random_crop(),参考https://blog.csdn.net/sinat_21585785/article/details/74144800。例如我的原图为:

裁剪为:

再运行一次,裁剪为:

代码如下:

import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt

sess = tf.InteractiveSession()
image = img.imread('MarshOrchid.jpg')

reshaped_image = tf.cast(image, tf.float32)
size = tf.cast(tf.shape(reshaped_image).eval(), tf.int32)

height = sess.run(size[0] // 2)
width = sess.run(size[1] // 2)

distored_image = tf.random_crop(reshaped_image, [height, width, 3])

print(tf.shape(reshaped_image).eval())
print(tf.shape(distored_image).eval())

fig = plt.figure()
fig1 = plt.figure()

ax = fig.add_subplot(111)
ax1 = fig1.add_subplot(111)

ax.imshow(sess.run(tf.cast(reshaped_image, tf.uint8)))
ax1.imshow(sess.run(tf.cast(distored_image, tf.uint8)))

plt.show()

这里用matplotlib.image的imread()方法读入图片,用tf.cast()方法将其数值转换为float32类型,然后打印其shape,我这里为[5528 3685    3]。接着用整除得到裁剪数值,选择的为裁剪一半。接着是figure实例,add_subplot()操作添加子图,一个的话里面是“111”,两个的话则分别add_subplot(221),add_subplot(222),add_subplot(223),add_subplot(224)。imshow()方法第一个参数X存储图像,最后用plt.show()显示。

第六个tf.multinomial(),multinomial也就是多项式。这个方法可以从多项式分布中抽取样本,就是根据概率分布的大小,返回对应维度的下标序号。测试代码如下:

import numpy as np
import tensorflow as tf

b = tf.constant(np.random.normal(size = (3, 4)))

with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(tf.multinomial(b, 5)))

结果为:

[[ 2.04100276 -1.12229608 -0.78679458 -0.16623389]
 [ 0.73953609 -0.06907413  0.38520517 -0.27433991]
 [ 0.0813765  -0.16081293 -2.02023628  0.23459176]]
[[0 0 0 0 2]
 [3 0 2 1 2]
 [0 1 0 3 3]]

b变量原来为三行四列的,经过该操作后成了三行五列的。multinomial()方法第一个参数是一个2-D Tensor,称为logits,其shape为[batch_size, num_classes],所以本例中batch_size就是3,num_classes就是4。第二个参数是num_samples,是一个0-D Tensor,也就是常量,表示从每一行切片中获取的独立样本的个数。这里我用的随机数作为初始变量,其实如果以固定的值来算,其multinomial()结果也会是变化的。

第七个是tf.random_gamma()。该方法根据gamma分布个数,每个分布给出shape参数对应个数数据。

在本部分的学习中,遇到的一些其他不清楚的问题一并记录如下。

1. numpy的eye()方法可以得到一个单位矩阵,如

import numpy as np

e = np.eye(3)

得到的结果如下:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

这也就是3行3列的方阵,单位矩阵一定是方阵。不过该方法实际上可以不产生方阵,如将其改为np.eye(3, 4),那么就得到3行4列的如下输出:

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]

其类型是numpy.ndarray,可以用e.shape属性获取其shape信息。

2. TensorFlow的变量也是可以用shape直接获取其shape属性的,如:

import tensorflow as tf

a = tf.get_variable('a', [2, 3], initializer=tf.random_normal_initializer(mean=0, stddev=1))

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)

    print('a:\n', sess.run(a))
    print(type(a))
    print(a.shape)

这里面变量a是2行3列的矩阵,其类型是tensorflow.python.ops.variables.Variable,虽然不是numpy.ndarray,但是用a.shape是没问题的。接着,TensorFlow有tf.shape(x)和x.get_shape()两个方法,和以上有什么不同呢?加上print(tf.shape(a)),得到的是:

Tensor("Shape:0", shape=(2,), dtype=int32)

所以对于tf.shape(x),x可以是tensor,也可以不是,其返回值是一个tensor。shape=(2,)也就是个二维矩阵了,接着 print(sess.run(tf.shape(a)))确定运行后其二维各自大小,得到:

[2 3]

也就是该二维矩阵第一个维度(行)是2,第二个维度(列)是3。

猜你喜欢

转载自blog.csdn.net/u012911347/article/details/81477244