14
14
def add_layer (inputs , in_size , out_size , n_layer , activation_function = None ):
15
15
# add one more layer and return the output of this layer
16
16
layer_name = 'layer%s' % n_layer
17
- with tf .name_scope (layer_name ):
18
- with tf .name_scope ('weights' ):
19
- Weights = tf .Variable (tf .random_normal ([in_size , out_size ]), name = 'W' )
20
- tf .histogram_summary (layer_name + '/weights' , Weights )
21
- with tf .name_scope ('biases' ):
22
- biases = tf .Variable (tf .zeros ([1 , out_size ]) + 0.1 , name = 'b' )
23
- tf .histogram_summary (layer_name + '/biases' , biases )
24
- with tf .name_scope ('Wx_plus_b' ):
25
- Wx_plus_b = tf .add (tf .matmul (inputs , Weights ), biases )
26
- if activation_function is None :
27
- outputs = Wx_plus_b
28
- else :
29
- outputs = activation_function (Wx_plus_b , )
30
- tf .histogram_summary (layer_name + '/outputs' , outputs )
17
+
18
+ # for tensorflow version < 0.12
19
+ if int ((tf .__version__ ).split ('.' )[1 ]) < 12 :
20
+ with tf .name_scope (layer_name ):
21
+ with tf .name_scope ('weights' ):
22
+ Weights = tf .Variable (tf .random_normal ([in_size , out_size ]), name = 'W' )
23
+ tf .histogram_summary (layer_name + '/weights' , Weights )
24
+ with tf .name_scope ('biases' ):
25
+ biases = tf .Variable (tf .zeros ([1 , out_size ]) + 0.1 , name = 'b' )
26
+ tf .histogram_summary (layer_name + '/biases' , biases )
27
+ with tf .name_scope ('Wx_plus_b' ):
28
+ Wx_plus_b = tf .add (tf .matmul (inputs , Weights ), biases )
29
+ if activation_function is None :
30
+ outputs = Wx_plus_b
31
+ else :
32
+ outputs = activation_function (Wx_plus_b , )
33
+ tf .histogram_summary (layer_name + '/outputs' , outputs )
34
+ else : # tensorflow version >= 0.12
35
+ with tf .name_scope (layer_name ):
36
+ with tf .name_scope ('weights' ):
37
+ Weights = tf .Variable (tf .random_normal ([in_size , out_size ]), name = 'W' )
38
+ tf .summary .histogram (layer_name + '/weights' , Weights )
39
+ with tf .name_scope ('biases' ):
40
+ biases = tf .Variable (tf .zeros ([1 , out_size ]) + 0.1 , name = 'b' )
41
+ tf .summary .histogram (layer_name + '/biases' , biases )
42
+ with tf .name_scope ('Wx_plus_b' ):
43
+ Wx_plus_b = tf .add (tf .matmul (inputs , Weights ), biases )
44
+ if activation_function is None :
45
+ outputs = Wx_plus_b
46
+ else :
47
+ outputs = activation_function (Wx_plus_b , )
48
+ tf .summary .histogram (layer_name + '/outputs' , outputs )
31
49
return outputs
32
50
33
51
@@ -50,16 +68,25 @@ def add_layer(inputs, in_size, out_size, n_layer, activation_function=None):
50
68
with tf .name_scope ('loss' ):
51
69
loss = tf .reduce_mean (tf .reduce_sum (tf .square (ys - prediction ),
52
70
reduction_indices = [1 ]))
53
- tf .scalar_summary ('loss' , loss )
71
+ if int ((tf .__version__ ).split ('.' )[1 ]) < 12 : # tensorflow version < 0.12
72
+ tf .scalar_summary ('loss' , loss )
73
+ else : # tensorflow version >= 0.12
74
+ tf .summary .scalar ('loss' , loss )
54
75
55
76
with tf .name_scope ('train' ):
56
77
train_step = tf .train .GradientDescentOptimizer (0.1 ).minimize (loss )
57
78
58
79
sess = tf .Session ()
59
- merged = tf .merge_all_summaries ()
80
+ if int ((tf .__version__ ).split ('.' )[1 ]) < 12 : # tensorflow version < 0.12
81
+ merged = tf .merge_all_summaries ()
82
+ else : # tensorflow version >= 0.12
83
+ merged = tf .summary .merge_all ()
60
84
61
85
# tf.train.SummaryWriter soon be deprecated, use following
62
- writer = tf .summary .FileWriter ("logs/" , sess .graph )
86
+ if int ((tf .__version__ ).split ('.' )[1 ]) < 12 : # tensorflow version < 0.12
87
+ writer = tf .train .SummaryWriter ('logs/' , sess .graph )
88
+ else : # tensorflow version >= 0.12
89
+ writer = tf .summary .FileWriter ("logs/" , sess .graph )
63
90
64
91
# tf.initialize_all_variables() no long valid from
65
92
# 2017-03-02 if using tensorflow >= 0.12
0 commit comments