Skip to content

Commit 3a621e2

Browse files
author
824zzy
committed
fix($*.ipynb, $RNN/*.npy): Change some codes need to be modified result of Tensorflow's version
1 parent 22d25b9 commit 3a621e2

17 files changed

+3114
-265
lines changed

.DS_Store

6 KB
Binary file not shown.

.ipynb_checkpoints/LV3-checkpoint.ipynb

+467
Large diffs are not rendered by default.

.ipynb_checkpoints/LV3_0.12-checkpoint.ipynb

+526
Large diffs are not rendered by default.

.ipynb_checkpoints/XOR_gate-checkpoint.ipynb

+647
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 17,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import tensorflow as tf\n",
10+
"import numpy as np\n",
11+
"tf.set_random_seed(55)"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 18,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"#构建网络\n",
21+
"D_input = 2\n",
22+
"D_label = 1\n",
23+
"D_hidden = 2\n",
24+
"lr=1e-4\n",
25+
"\n",
26+
"x = tf.placeholder(tf.float32, [None, D_input], name=\"x\")\n",
27+
"t = tf.placeholder(tf.float32, [None, D_label], name=\"t\")\n",
28+
" \n",
29+
"W_h1 = tf.Variable(tf.truncated_normal([D_input, D_hidden], stddev=0.1), name=\"W_h\")\n",
30+
"b_h1 = tf.Variable(tf.constant(0.1, shape=[D_hidden]), name=\"b_h\")\n",
31+
"pre_act_h1 = tf.matmul(x, W_h1) + b_h1\n",
32+
"act_h1 = tf.nn.relu(pre_act_h1, name='act_h')\n",
33+
" \n",
34+
"W_o = tf.Variable(tf.truncated_normal([D_hidden, D_label], stddev=0.1), name=\"W_o\")\n",
35+
"b_o = tf.Variable(tf.constant(0.1, shape=[D_label]), name=\"b_o\")\n",
36+
"pre_act_o = tf.matmul(act_h1, W_o) + b_o\n",
37+
"y = tf.nn.relu(pre_act_o, name='act_y')\n",
38+
" \n",
39+
"loss=tf.reduce_mean((y-t)**2)\n",
40+
" \n",
41+
"train_step = tf.train.AdamOptimizer(lr).minimize(loss)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 19,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"#生成数据\n",
51+
"X=[[0,0],[0,1],[1,0],[1,1]]\n",
52+
"Y=[[0],[1],[1],[0]]\n",
53+
"X=np.array(X).astype('int16')\n",
54+
"Y=np.array(Y).astype('int16')"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 20,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"#加载\n",
64+
"sess = tf.InteractiveSession()\n",
65+
"sess.run(tf.global_variables_initializer())"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 21,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"#训练\n",
75+
"for i in range(20000):\n",
76+
" sess.run(train_step,feed_dict={x:X,t:Y} )"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 22,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/plain": [
87+
"array([[ 0.01419991],\n",
88+
" [ 0.97204113],\n",
89+
" [ 0.97204131],\n",
90+
" [ 0.04118853]], dtype=float32)"
91+
]
92+
},
93+
"execution_count": 22,
94+
"metadata": {},
95+
"output_type": "execute_result"
96+
}
97+
],
98+
"source": [
99+
"#计算预测值\n",
100+
"sess.run(y,feed_dict={x:X})"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 23,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"data": {
110+
"text/plain": [
111+
"array([[ 0.96414495, 1.01730847],\n",
112+
" [ 0. , 0.50864619],\n",
113+
" [ 0. , 0.50864631],\n",
114+
" [ 0. , 0. ]], dtype=float32)"
115+
]
116+
},
117+
"execution_count": 23,
118+
"metadata": {},
119+
"output_type": "execute_result"
120+
}
121+
],
122+
"source": [
123+
"#查看隐藏层的输出\n",
124+
"sess.run(act_h1,feed_dict={x:X})"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 24,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"#用完关闭session\n",
134+
"sess.close()"
135+
]
136+
}
137+
],
138+
"metadata": {
139+
"kernelspec": {
140+
"display_name": "Python 2",
141+
"language": "python",
142+
"name": "python2"
143+
},
144+
"language_info": {
145+
"codemirror_mode": {
146+
"name": "ipython",
147+
"version": 2
148+
},
149+
"file_extension": ".py",
150+
"mimetype": "text/x-python",
151+
"name": "python",
152+
"nbconvert_exporter": "python",
153+
"pygments_lexer": "ipython2",
154+
"version": "2.7.14"
155+
}
156+
},
157+
"nbformat": 4,
158+
"nbformat_minor": 1
159+
}

FNN.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ def bias_init(self,shape):
6666
def variable_summaries(self, var, name):
6767
with tf.name_scope(name+'_summaries'):
6868
mean = tf.reduce_mean(var)
69-
tf.scalar_summary('mean/' + name, mean)
69+
tf.summary.scalar('mean/' + name, mean)
7070
with tf.name_scope(name+'_stddev'):
7171
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
72-
tf.scalar_summary('_stddev/' + name, stddev)
73-
tf.scalar_summary('_max/' + name, tf.reduce_max(var))
74-
tf.scalar_summary('_min/' + name, tf.reduce_min(var))
75-
tf.histogram_summary(name, var)
72+
tf.summary.scalar('_stddev/' + name, stddev)
73+
tf.summary.scalar('_max/' + name, tf.reduce_max(var))
74+
tf.summary.scalar('_min/' + name, tf.reduce_min(var))
75+
tf.summary.histogram(name, var)
7676

7777
def layer(self,in_tensor, in_dim, out_dim, layer_name, act=tf.nn.relu):
7878
with tf.name_scope(layer_name):
@@ -85,9 +85,9 @@ def layer(self,in_tensor, in_dim, out_dim, layer_name, act=tf.nn.relu):
8585
self.variable_summaries(biases, layer_name + '/biases')
8686
with tf.name_scope(layer_name+'_Wx_plus_b'):
8787
pre_activate = tf.matmul(in_tensor, weights) + biases
88-
tf.histogram_summary(layer_name + '/pre_activations', pre_activate)
88+
tf.summary.histogram(layer_name + '/pre_activations', pre_activate)
8989
activations = act(pre_activate, name='activation')
90-
tf.histogram_summary(layer_name + '/activations', activations)
90+
tf.summary.histogram(layer_name + '/activations', activations)
9191
return activations, tf.nn.l2_loss(weights)
9292

9393
def drop_layer(self,in_tensor):
@@ -129,25 +129,25 @@ def build(self, prefix):
129129
with tf.name_scope('total_l2'):
130130
for l2 in self.total_l2:
131131
self.l2_penalty+=l2
132-
tf.scalar_summary('l2_penalty', self.l2_penalty)
132+
tf.summary.scalar_('l2_penalty', self.l2_penalty)
133133

134134
if self.Task_type=='regression':
135135
with tf.name_scope('SSE'):
136136
self.loss=tf.reduce_mean(tf.nn.l2_loss((self.output - self.labels)))
137-
tf.scalar_summary('loss', self.loss)
137+
tf.summary.scalar('loss', self.loss)
138138
else:
139139
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=self.output, labels=self.labels)
140140
with tf.name_scope('cross entropy'):
141141
self.loss = tf.reduce_mean(entropy)
142-
tf.scalar_summary('loss', self.loss)
142+
tf.summary.scalar('loss', self.loss)
143143
with tf.name_scope('accuracy'):
144144
correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.labels, 1))
145145
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
146-
tf.scalar_summary('accuracy', self.accuracy)
146+
tf.summary.scalar('accuracy', self.accuracy)
147147

148148
with tf.name_scope('total_loss'):
149149
self.total_loss=self.loss + self.l2_penalty*self.L2_lambda
150-
tf.scalar_summary('total_loss', self.total_loss)
150+
tf.summary.scalar_('total_loss', self.total_loss)
151151

152152
#train
153153
with tf.name_scope('train'):

LV3.ipynb

+33-31
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
"import tensorflow as tf\n",
1212
"import numpy as np\n",
1313
"import matplotlib.pyplot as plt\n",
14-
"%matplotlib inline"
14+
"%matplotlib inline\n",
15+
"tf.reset_default_graph()"
1516
]
1617
},
1718
{
1819
"cell_type": "code",
1920
"execution_count": 2,
20-
"metadata": {
21-
"collapsed": false
22-
},
21+
"metadata": {},
2322
"outputs": [],
2423
"source": [
2524
"class FNN(object):\n",
@@ -86,14 +85,14 @@
8685
" def variable_summaries(self, var, name):\n",
8786
" with tf.name_scope(name+'_summaries'):\n",
8887
" mean = tf.reduce_mean(var)\n",
89-
" tf.scalar_summary('mean/' + name, mean)\n",
88+
" tf.summary.scalar('mean/' + name, mean)\n",
9089
" with tf.name_scope(name+'_stddev'):\n",
9190
" stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))\n",
9291
" # 记录每次训练后变量的数值变化\n",
93-
" tf.scalar_summary('_stddev/' + name, stddev)\n",
94-
" tf.scalar_summary('_max/' + name, tf.reduce_max(var))\n",
95-
" tf.scalar_summary('_min/' + name, tf.reduce_min(var))\n",
96-
" tf.histogram_summary(name, var)\n",
92+
" tf.summary.scalar('_stddev/' + name, stddev)\n",
93+
" tf.summary.scalar_('_max/' + name, tf.reduce_max(var))\n",
94+
" tf.summary.scalar_('_min/' + name, tf.reduce_min(var))\n",
95+
" tf.summary.histogram(name, var)\n",
9796
"\n",
9897
" def layer(self,in_tensor, in_dim, out_dim, layer_name, act=tf.nn.relu):\n",
9998
" with tf.name_scope(layer_name):\n",
@@ -113,10 +112,10 @@
113112
" # 计算Wx+b\n",
114113
" pre_activate = tf.matmul(in_tensor, weights) + biases\n",
115114
" # 记录直方图\n",
116-
" tf.histogram_summary(layer_name + '/pre_activations', pre_activate)\n",
115+
" tf.summary.histogram(layer_name + '/pre_activations', pre_activate)\n",
117116
" # 计算a(Wx+b)\n",
118117
" activations = act(pre_activate, name='activation')\n",
119-
" tf.histogram_summary(layer_name + '/activations', activations)\n",
118+
" tf.summary.histogram(layer_name + '/activations', activations)\n",
120119
" # 最终返回该层的输出,以及权重W的L2\n",
121120
" return activations, tf.nn.l2_loss(weights)\n",
122121
"\n",
@@ -174,7 +173,7 @@
174173
" with tf.name_scope('total_l2'):\n",
175174
" for l2 in self.total_l2:\n",
176175
" self.l2_penalty+=l2\n",
177-
" tf.scalar_summary('l2_penalty', self.l2_penalty)\n",
176+
" tf.summary.scalar('l2_penalty', self.l2_penalty)\n",
178177
" \n",
179178
" # 不同任务的loss\n",
180179
" # 若为回归,则loss是用于判断所有预测值和实际值差别的函数。\n",
@@ -183,22 +182,22 @@
183182
" self.loss=tf.reduce_mean((self.output-self.labels)**2)\n",
184183
" self.loss2=tf.nn.l2_loss(self.output-self.labels)\n",
185184
" \n",
186-
" tf.scalar_summary('loss', self.loss)\n",
185+
" tf.summary.scalar('loss', self.loss)\n",
187186
" else:\n",
188187
" # 若为分类,cross entropy的loss function\n",
189188
" entropy = tf.nn.softmax_cross_entropy_with_logits(self.output, self.labels)\n",
190189
" with tf.name_scope('cross entropy'):\n",
191190
" self.loss = tf.reduce_mean(entropy)\n",
192-
" tf.scalar_summary('loss', self.loss)\n",
191+
" tf.summary.scalar('loss', self.loss)\n",
193192
" with tf.name_scope('accuracy'):\n",
194193
" correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.labels, 1))\n",
195194
" self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
196-
" tf.scalar_summary('accuracy', self.accuracy)\n",
195+
" tf.summary.scalar('accuracy', self.accuracy)\n",
197196
" \n",
198197
" # 整合所有loss,形成最终loss\n",
199198
" with tf.name_scope('total_loss'):\n",
200199
" self.total_loss=self.loss + self.l2_penalty*self.L2_lambda\n",
201-
" tf.scalar_summary('total_loss', self.total_loss)\n",
200+
" tf.summary.scalar('total_loss', self.total_loss)\n",
202201
" \n",
203202
" # 训练操作\n",
204203
" with tf.name_scope('train'):\n",
@@ -263,9 +262,7 @@
263262
{
264263
"cell_type": "code",
265264
"execution_count": 5,
266-
"metadata": {
267-
"collapsed": false
268-
},
265+
"metadata": {},
269266
"outputs": [
270267
{
271268
"name": "stdout",
@@ -282,9 +279,7 @@
282279
{
283280
"cell_type": "code",
284281
"execution_count": 6,
285-
"metadata": {
286-
"collapsed": false
287-
},
282+
"metadata": {},
288283
"outputs": [
289284
{
290285
"name": "stdout",
@@ -319,18 +314,16 @@
319314
"outputs": [],
320315
"source": [
321316
"sess = tf.InteractiveSession()\n",
322-
"tf.initialize_all_variables().run()\n",
323-
"merged = tf.merge_all_summaries()\n",
324-
"train_writer = tf.train.SummaryWriter('log3' + '/train',sess.graph)\n",
325-
"test_writer = tf.train.SummaryWriter('log3' + '/test')"
317+
"sess.run(tf.global_variables_initializer())\n",
318+
"merged = tf.summary.merge_all()\n",
319+
"train_writer = tf.summary.FileWriter('log3' + '/train',sess.graph)\n",
320+
"test_writer = tf.summary.FileWriter('log3' + '/test')"
326321
]
327322
},
328323
{
329324
"cell_type": "code",
330325
"execution_count": 8,
331-
"metadata": {
332-
"collapsed": false
333-
},
326+
"metadata": {},
334327
"outputs": [],
335328
"source": [
336329
"def plots(T,P,i, n=21,length=400):\n",
@@ -360,7 +353,6 @@
360353
"cell_type": "code",
361354
"execution_count": 9,
362355
"metadata": {
363-
"collapsed": false,
364356
"scrolled": true
365357
},
366358
"outputs": [
@@ -439,6 +431,16 @@
439431
" test_writer.add_summary(summary, k)\n",
440432
" print('epoch%s | train_loss:%s |test_loss:%s' %(i,sess.run(ff.loss,feed_dict={ff.inputs:X0,ff.labels:Y0,ff.drop_keep_rate:1.0}),sess.run(ff.loss,feed_dict={ff.inputs:X_test,ff.labels:Y_test,ff.drop_keep_rate:1.0})))"
441433
]
434+
},
435+
{
436+
"cell_type": "code",
437+
"execution_count": null,
438+
"metadata": {},
439+
"outputs": [],
440+
"source": [
441+
"#用完关闭session\n",
442+
"sess.close()"
443+
]
442444
}
443445
],
444446
"metadata": {
@@ -457,7 +459,7 @@
457459
"name": "python",
458460
"nbconvert_exporter": "python",
459461
"pygments_lexer": "ipython2",
460-
"version": "2.7.12"
462+
"version": "2.7.14"
461463
}
462464
},
463465
"nbformat": 4,

0 commit comments

Comments
 (0)