@@ -32,10 +32,14 @@ def plot_his(inputs, inputs_norm):
32
32
for i , input in enumerate (all_inputs ):
33
33
plt .subplot (2 , len (all_inputs ), j * len (all_inputs )+ (i + 1 ))
34
34
plt .cla ()
35
- plt .hist (input .ravel (), bins = 15 , range = (- 1 , 1 ), color = '#FF5733' )
35
+ if i == 0 :
36
+ the_range = (- 7 , 10 )
37
+ else :
38
+ the_range = (- 1 , 1 )
39
+ plt .hist (input .ravel (), bins = 15 , range = the_range , color = '#FF5733' )
36
40
plt .yticks (())
37
41
if j == 1 :
38
- plt .xticks (( - 1 , 0 , 1 ) )
42
+ plt .xticks (the_range )
39
43
else :
40
44
plt .xticks (())
41
45
ax = plt .gca ()
@@ -81,6 +85,18 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
81
85
return outputs
82
86
83
87
fix_seed (1 )
88
+
89
+ if norm :
90
+ # BN for the first input
91
+ fc_mean , fc_var = tf .nn .moments (
92
+ xs ,
93
+ axes = [0 ],
94
+ )
95
+ scale = tf .Variable (tf .ones ([1 ]))
96
+ shift = tf .Variable (tf .zeros ([1 ]))
97
+ epsilon = 0.001
98
+ xs = tf .nn .batch_normalization (xs , fc_mean , fc_var , shift , scale , epsilon )
99
+
84
100
# record inputs for every layer
85
101
layers_inputs = [xs ]
86
102
@@ -137,8 +153,8 @@ def add_layer(inputs, in_size, out_size, activation_function=None, norm=False):
137
153
all_inputs , all_inputs_norm = sess .run ([layers_inputs , layers_inputs_norm ], feed_dict = {xs : x_data , ys : y_data })
138
154
plot_his (all_inputs , all_inputs_norm )
139
155
140
- sess .run (train_op , feed_dict = {xs : x_data , ys : y_data })
141
- sess . run ( train_op_norm , feed_dict = { xs : x_data , ys : y_data })
156
+ sess .run ([ train_op , train_op_norm ] , feed_dict = {xs : x_data , ys : y_data })
157
+
142
158
if i % record_step == 0 :
143
159
# record cost
144
160
cost_his .append (sess .run (cost , feed_dict = {xs : x_data , ys : y_data }))
0 commit comments