tensorflow学习6

1 g_w1 = tf.get_variable('g_w1', [z_dim, 3136], dtype=tf.float32,
2                             initializer=tf.truncated_normal_initializer(stddev=0.02))
3         g_b1 = tf.get_variable('g_b1', [3136], initializer=tf.truncated_normal_initializer(stddev=0.02))
4         g1 = tf.matmul(z, g_w1) + g_b1
5         g1 = tf.reshape(g1, [-1, 56, 56, 1])
#reshape为什么会有-1???

reshape即把矩阵的形状变一下,这跟matlab一样的,但如果参数是-1的话是什么意思呢?

 1 In [21]:
 2  
 3  
 4 tensor = tf.constant([1, 2, 3, 4, 5, 6, 7,8])
 5  
 6  
 7 In [22]:
 8  
 9  
10 sess.run(tf.initialize_all_variables())
11  
12  
13 In [23]:
14  
15  
16 print(sess.run(tensor))
17  
18  
19 [1 2 3 4 5 6 7 8]
20 In [24]:
21  
22  
23 tensorReshape = tf.reshape(tensor,[2,4])
24  
25  
26 In [25]:
27  
28  
29 print(sess.run(tensorReshape))
30  
31  
32 [[1 2 3 4]
33  [5 6 7 8]]
34 In [26]:
35  
36  
37 tensorReshape = tf.reshape(tensor,[1,2,4])
38  
39  
40 In [27]:
41  
42  
43 print(sess.run(tensorReshape))
44  
45  
46 [[[1 2 3 4]
47   [5 6 7 8]]]
48 In [28]:
49  
50  
51 tensorReshape = tf.reshape(tensor,[-1,2,2])
52  
53  
54 In [29]:
55  
56  
57 print(sess.run(tensorReshape))
58  
59  
60 [[[1 2]
61   [3 4]]
62 
63  [[5 6]
64   [7 8]]]
65 所以-1,就是缺省值,就是先以你们合适,到时总数除以你们几个的乘积,我该是几就是几。

 批正则化:

1 #批正则化
2 g1 = tf.contrib.layers.batch_norm(g1, epsilon=1e-5, scope='bn1')

 

 共享变量的应用之处:

之前看文档时体会不深,现在大体明白共享变量的存在意义了,它是在设计计算图时考虑的,同一个变量如果有不同的数据流(计算图中不同的节点在不同的时刻去给同一个节点的同一个输入位置提供数据),Variable变量会之间创建两个不同的变量节点去接收不同的数据流,get_variable变量在reuse为True时会使用同一个变量应付不同的数据流,这也就是共享变量的应用之处。这在上面的程序中体现在判别器的任务,如果接收到的是生成器生成的图像,判别器就尝试优化自己的网络结构来使自己输出0,如果接收到的是来自真实数据的图像,那么就尝试优化自己的网络结构来使自己输出1。也就是说,fake图像和real图像经过判别器的时候,要共享同一套变量,所以TensorFlow引入了变量共享机制,而和正常的卷积网络不同的是这里的fake和real变量并不在同一个计算图节点位置(real图片在x节点处输入,而fake图则在生成器输出节点位置计入计算图)。

posted @ 2018-05-06 22:09  陈柯成  阅读(195)  评论(0编辑  收藏  举报