質問編集履歴

3

少なかったので書き足しました

2019/05/03 23:58

投稿

iLia
iLia

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,201 @@
1
1
  ```ここに言語を入力
2
2
 
3
+ # -*- coding: utf-8 -*-
4
+
5
+ import keras
6
+
7
+ from keras.datasets import mnist
8
+
9
+ import matplotlib
10
+
11
+ from matplotlib import pyplot
12
+
13
+ import numpy as np
14
+
15
+ from sklearn import datasets
16
+
17
+ from sklearn.model_selection import cross_val_score as crv
18
+
19
+
20
+
21
+ def sigmoid(x):
22
+
23
+ return 1 / (1 + np.exp(-x))
24
+
25
+
26
+
27
+ def softmax(x):
28
+
29
+ expX = np.exp(x)
30
+
31
+ return expX / np.sum(expX)
32
+
33
+
34
+
35
+ def cross_entropy_error(y,t):
36
+
37
+ delta = 1e-7
38
+
39
+ batch_size = y.shape[0]
40
+
41
+ idx= np.arange(batch_size)
42
+
43
+ return -np.sum(np.log(y[idx,t]+delta)) / batch_size
44
+
45
+
46
+
47
+ def gradient(f,x):
48
+
49
+ if x.ndim == 1:
50
+
51
+ return gradient_sub(f,x)
52
+
53
+ else:
54
+
55
+ grad = np.zeros_like(x)
56
+
57
+ for index, xx in enumerate(x):
58
+
59
+ grad[index] = gradient_sub(f,xx)
60
+
61
+ return grad
62
+
63
+
64
+
65
+ def gradient_sub(f,x):
66
+
67
+ h = 1e-4
68
+
69
+ grad = np.zeros_like(x)
70
+
71
+ for i in range(x.size):
72
+
73
+ val = x[i]
74
+
75
+ x[i] = val + h
76
+
77
+ fx1 = f(x)
78
+
79
+ x[i] = val - h
80
+
81
+ fx2 = f(x)
82
+
83
+ grad[i] = (fx1 - fx2) / (2*h)
84
+
85
+ x[i] = val
86
+
87
+ return grad
88
+
89
+
90
+
91
+
92
+
93
+ class SampleNetwork:
94
+
95
+ def __init__(self):
96
+
97
+ input_size = 64
98
+
99
+ hidden_size = 50
100
+
101
+ output_size = 10
102
+
103
+ self.params = {}
104
+
105
+ self.params["w0"] = 0.01 * np.random.randn(input_size,hidden_size)
106
+
107
+ self.params["w1"] = 0.01 * np.random.randn(hidden_size,output_size)
108
+
109
+ self.params["b0"] = np.zeros(hidden_size)
110
+
111
+ self.params["b1"] = np.zeros(output_size)
112
+
113
+ self.learning_rate = 0.1
114
+
115
+
116
+
117
+ def predict(self,x):
118
+
119
+ a0 = np.dot(x,self.params["w0"]) + self.params["b0"]
120
+
121
+ z0 = sigmoid(a0)
122
+
123
+ a1 = np.dot(z0,self.params["w1"]) + self.params["b1"]
124
+
125
+ y = softmax(a1)
126
+
127
+ return y
128
+
129
+
130
+
131
+ def updata_params(self,x,t):
132
+
133
+ loss_W = lambda W: self.loss(x,t)
134
+
135
+ for key in self.params.keys():
136
+
137
+ grad = gradient(loss_W, self.params[key])
138
+
139
+ self.params[key] -= self.learning_rate*grad
140
+
141
+
142
+
143
+
144
+
145
+ def loss(self,x,t):
146
+
147
+ y = self.predict(x)
148
+
149
+ return cross_entropy_error(y,t)
150
+
151
+
152
+
153
+ def accurary(self,x,t):
154
+
155
+ y = self.predict(x)
156
+
157
+ y = np.argmax(y,axis=1)
158
+
159
+ acc = np.sum(y==t) / float(x.shape[0])
160
+
161
+ return acc
162
+
163
+
164
+
165
+ digits = datasets.load_digits()
166
+
167
+ X = digits.data
168
+
169
+ T = digits.target
170
+
171
+
172
+
173
+ (train_X, train_T), (test_X, test_T) = mnist.load_data()
174
+
175
+
176
+
177
+ network = SampleNetwork()
178
+
179
+
180
+
181
+ batch_size = 100
182
+
183
+ iter_num = 300
184
+
185
+ train_size = train_X.shape[0]
186
+
187
+ erpoch_size = max(train_size//batch_size,1)
188
+
189
+ loss_list = []
190
+
191
+ train_accurary_list = []
192
+
193
+ test_accurary_list = []
194
+
195
+
196
+
197
+
198
+
3
199
  for index in range(iter_num):
4
200
 
5
201
  batch_choice = np.random.choice(train_size,batch_size)
@@ -16,12 +212,50 @@
16
212
 
17
213
  train_accurary = network.accurary(train_X,train_T)
18
214
 
215
+ test_accurary = network.accurary(test_X,test_T)
216
+
19
217
  train_accurary_list.append(train_accurary)
20
218
 
21
- test_accurary = network.accurary(test_X,test_T)
22
-
23
219
  test_accurary_list.append(test_accurary)
24
220
 
221
+
222
+
223
+ pyplot.figure(figsize=(10,7))
224
+
225
+ pyplot.subplot(2,2,1)
226
+
227
+ pyplot.plot(np.arange(len(loss_list)),loss_list)
228
+
229
+ pyplot.xlabel("iteration")
230
+
231
+ pyplot.title("Cross Entropy Error")
232
+
233
+ pyplot.subplot(2,2,2)
234
+
235
+ pyplot.plot(np.arange(0,len(train_accurary_list),1),train_accurary_list,"b")
236
+
237
+ pyplot.plot(np.arange(0,len(test_accurary_list),1),test_accurary_list,"ro")
238
+
239
+ pyplot.xlabel("iteration(epoch)")
240
+
241
+ pyplot.title("Accuary")
242
+
243
+ pyplot.legend(("train","test"),loc = "lowrer right")
244
+
245
+ pyplot.tight_layout()
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
25
259
  ```
26
260
 
27
261
 

2

このような感じですか?

2019/05/03 23:57

投稿

iLia
iLia

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,220 +1,4 @@
1
- ### 前提・実現したいこと
2
-
3
-
4
-
5
- エラー解決したいです
1
+ ```ここに言語入力
6
-
7
-
8
-
9
- ### 発生している問題・エラーメッセージ
10
-
11
-
12
-
13
- update_params
14
-
15
-
16
-
17
- ### 該当のソースコード
18
-
19
-
20
-
21
-
22
-
23
- # -*- coding: utf-8 -*-
24
-
25
- import keras
26
-
27
- from keras.datasets import mnist
28
-
29
- import matplotlib
30
-
31
- from matplotlib import pyplot
32
-
33
- import numpy as np
34
-
35
- from sklearn import datasets
36
-
37
- from sklearn.model_selection import cross_val_score as crv
38
-
39
-
40
-
41
- def sigmoid(x):
42
-
43
- return 1 / (1 + np.exp(-x))
44
-
45
-
46
-
47
- def softmax(x):
48
-
49
- expX = np.exp(x)
50
-
51
- return expX / np.sum(expX)
52
-
53
-
54
-
55
- def cross_entropy_error(y,t):
56
-
57
- delta = 1e-7
58
-
59
- batch_size = y.shape[0]
60
-
61
- idx= np.arange(batch_size)
62
-
63
- return -np.sum(np.log(y[idx,t]+delta)) / batch_size
64
-
65
-
66
-
67
- def gradient(f,x):
68
-
69
- if x.ndim == 1:
70
-
71
- return gradient_sub(f,x)
72
-
73
- else:
74
-
75
- grad = np.zeros_like(x)
76
-
77
- for index, xx in enumerate(x):
78
-
79
- grad[index] = gradient_sub(f,xx)
80
-
81
- return grad
82
-
83
-
84
-
85
- def gradient_sub(f,x):
86
-
87
- h = 1e-4
88
-
89
- grad = np.zeros_like(x)
90
-
91
- for i in range(x.size):
92
-
93
- val = x[i]
94
-
95
- x[i] = val + h
96
-
97
- fx1 = f(x)
98
-
99
- x[i] = val - h
100
-
101
- fx2 = f(x)
102
-
103
- grad[i] = (fx1 - fx2) / (2*h)
104
-
105
- x[i] = val
106
-
107
- return grad
108
-
109
-
110
-
111
-
112
-
113
- class SampleNetwork:
114
-
115
- def __init__(self):
116
-
117
- input_size = 64
118
-
119
- hidden_size = 50
120
-
121
- output_size = 10
122
-
123
- self.params = {}
124
-
125
- self.params["w0"] = 0.01 * np.random.randn(input_size,hidden_size)
126
-
127
- self.params["w1"] = 0.01 * np.random.randn(hidden_size,output_size)
128
-
129
- self.params["b0"] = np.zeros(hidden_size)
130
-
131
- self.params["b1"] = np.zeros(output_size)
132
-
133
- self.learning_rate = 0.1
134
-
135
-
136
-
137
- def predict(self,x):
138
-
139
- a0 = np.dot(x,self.params["w0"]) + self.params["b0"]
140
-
141
- z0 = sigmoid(a0)
142
-
143
- a1 = np.dot(z0,self.params["w1"]) + self.params["b1"]
144
-
145
- y = softmax(a1)
146
-
147
- return y
148
-
149
-
150
-
151
- def updata_params(self,x,t):
152
-
153
- loss_W = lambda W: self.loss(x,t)
154
-
155
- for key in self.params.keys():
156
-
157
- grad = gradient(loss_W, self.params[key])
158
-
159
- self.params[key] -= self.learning_rate*grad
160
-
161
-
162
-
163
-
164
-
165
- def loss(self,x,t):
166
-
167
- y = self.predict(x)
168
-
169
- return cross_entropy_error(y,t)
170
-
171
-
172
-
173
- def accurary(self,x,t):
174
-
175
- y = self.predict(x)
176
-
177
- y = np.argmax(y,axis=1)
178
-
179
- acc = np.sum(y==t) / float(x.shape[0])
180
-
181
- return acc
182
-
183
-
184
-
185
- digits = datasets.load_digits()
186
-
187
- X = digits.data
188
-
189
- T = digits.target
190
-
191
-
192
-
193
- (train_X, train_T), (test_X, test_T) = mnist.load_data()
194
-
195
-
196
-
197
- network = SampleNetwork()
198
-
199
-
200
-
201
- batch_size = 100
202
-
203
- iter_num = 300
204
-
205
- train_size = train_X.shape[0]
206
-
207
- erpoch_size = max(train_size//batch_size,1)
208
-
209
- loss_list = []
210
-
211
- train_accurary_list = []
212
-
213
- test_accurary_list = []
214
-
215
-
216
-
217
-
218
2
 
219
3
  for index in range(iter_num):
220
4
 
@@ -238,56 +22,20 @@
238
22
 
239
23
  test_accurary_list.append(test_accurary)
240
24
 
241
-
242
-
243
- pyplot.figure(figsize=(10,7))
244
-
245
- pyplot.subplot(2,2,1)
25
+ ```
246
-
247
- pyplot.plot(np.arange(len(loss_list)),loss_list)
248
-
249
- pyplot.xlabel("iteration")
250
-
251
- pyplot.title("Cross Entropy Error")
252
-
253
- pyplot.subplot(2,2,2)
254
-
255
- pyplot.plot(np.arange(0,len(train_accurary_list),1),train_accurary_list,"b")
256
-
257
- pyplot.plot(np.arange(0,len(test_accurary_list),1),test_accurary_list,"ro")
258
-
259
- pyplot.xlabel("iteration(epoch)")
260
-
261
- pyplot.title("Accuary")
262
-
263
- pyplot.legend(("train","test"),loc = "lowrer right")
264
-
265
- pyplot.tight_layout()
266
26
 
267
27
 
268
28
 
269
29
 
270
30
 
31
+ #エラー
271
32
 
33
+ Using TensorFlow backend.
272
34
 
35
+ Traceback (most recent call last):
273
36
 
37
+ File "number.py", line 103, in <module>
274
38
 
39
+ network.update_params(x_batch,t_batch)
275
40
 
276
-
277
-
278
-
279
-
280
-
281
- ### 試したこと
282
-
283
-
284
-
285
- エラーコードがどういう意味なのかを調べましたが、解決方法がわかりませんでした。
286
-
287
-
288
-
289
- ### 補足情報(FW/ツールのバージョンなど)
41
+ AttributeError: 'SampleNetwork' object has no attribute 'update_params'
290
-
291
-
292
-
293
- MacBook Pro Python VScode

1

修正しました

2019/05/03 22:33

投稿

iLia
iLia

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,26 @@
1
+ ### 前提・実現したいこと
2
+
3
+
4
+
1
- ###コです
5
+ エラを解決したいです
6
+
7
+
8
+
2
-
9
+ ### 発生している問題・エラーメッセージ
10
+
11
+
12
+
3
-
13
+ update_params
14
+
15
+
16
+
17
+ ### 該当のソースコード
18
+
19
+
20
+
21
+
22
+
23
+ # -*- coding: utf-8 -*-
4
24
 
5
25
  import keras
6
26
 
@@ -246,12 +266,28 @@
246
266
 
247
267
 
248
268
 
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
249
- ###エラー
281
+ ### 試したこと
250
-
251
- AttributeError: 'SampleNetwork' object has no attribute 'update_params'
282
+
252
-
253
-
254
-
255
-
256
-
283
+
284
+
257
- エラー意味調べたのですが、SampleNetworkの中にどうやって入れればいいのかがわかりません
285
+ エラーコードがどういう意味なのかを調べましたが、解決方法がわかりませんでした。
286
+
287
+
288
+
289
+ ### 補足情報(FW/ツールのバージョンなど)
290
+
291
+
292
+
293
+ MacBook Pro Python VScode