@@ -28,7 +28,6 @@ def fix_seed(seed=1):
28
28
29
29
def plot_his (inputs , inputs_norm ):
30
30
# plot histogram for the inputs of every layer
31
-
32
31
for j , all_inputs in enumerate ([inputs , inputs_norm ]):
33
32
for i , input in enumerate (all_inputs ):
34
33
plt .subplot (2 , len (all_inputs ), j * len (all_inputs )+ (i + 1 ))
@@ -59,6 +58,7 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
59
58
# normalize fully connected product
60
59
if norm :
61
60
# Batch Normalize
61
+ # when testing, you should fix fc_mean, fc_var instead of using tf.nn.moments!
62
62
fc_mean , fc_var = tf .nn .moments (
63
63
Wx_plus_b ,
64
64
axes = [0 ], # the dimension you wanna normalize, here [0] for batch
@@ -67,9 +67,10 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
67
67
scale = tf .Variable (tf .ones ([out_size ]))
68
68
shift = tf .Variable (tf .zeros ([out_size ]))
69
69
epsilon = 0.001
70
- # similar with this:
71
- # Wx_plus_b = (Wx_plus_b - fc_mean) / tf.sqrt(fc_var + 0.001)
72
70
Wx_plus_b = tf .nn .batch_normalization (Wx_plus_b , fc_mean , fc_var , shift , scale , epsilon )
71
+ # similar with this two steps:
72
+ # Wx_plus_b = (Wx_plus_b - fc_mean) / tf.sqrt(fc_var + 0.001)
73
+ # Wx_plus_b = Wx_plus_b * scale + shift
73
74
74
75
# activation
75
76
if activation_function is None :
@@ -111,8 +112,8 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
111
112
y_data = np .square (x_data ) - 5 + noise
112
113
113
114
# plot input data
114
- # plt.scatter(x_data, y_data)
115
- # plt.show()
115
+ plt .scatter (x_data , y_data )
116
+ plt .show ()
116
117
117
118
xs = tf .placeholder (tf .float32 , [None , 1 ]) # [num_samples, num_features]
118
119
ys = tf .placeholder (tf .float32 , [None , 1 ])
@@ -150,4 +151,6 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
150
151
plt .legend ()
151
152
plt .show ()
152
153
154
+ # when testing, you should fix fc_mean, fc_var instead of using tf.nn.moments!
155
+
153
156
0 commit comments