質問するログイン新規登録

質問編集履歴

2

このような形でよろしいでしょうか?

2019/05/05 13:06

投稿

iLia
iLia

スコア14

title CHANGED
File without changes
body CHANGED
@@ -135,6 +135,10 @@
135
135
 
136
136
  というコードに対し、
137
137
  ```
138
+ Using TensorFlow backend.
139
+ Traceback (most recent call last):
140
+ File "test2.py", line 102, in <module>
141
+ t_batch = train_T[batch_choice]
138
142
  IndexError: index 44061 is out of bounds for axis 0 with size 10000
139
143
 
140
144
  ```

1

お願いします

2019/05/05 13:06

投稿

iLia
iLia

スコア14

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,102 @@
1
1
  ```
2
+ # -*- coding: utf-8 -*-
3
+ import keras
4
+ from keras.datasets import mnist
5
+ import matplotlib
6
+ from matplotlib import pyplot
7
+ import numpy as np
8
+ from sklearn import datasets
9
+ from sklearn.model_selection import cross_val_score as crv
10
+
11
+ def sigmoid(x):
12
+ return 1 / (1 + np.exp(-x))
13
+
14
+ def softmax(x):
15
+ expX = np.exp(x)
16
+ return expX / np.sum(expX)
17
+
18
+ def cross_entropy_error(y,t):
19
+ delta = 1e-7
20
+ batch_size = y.shape[0]
21
+ idx= np.arange(batch_size)
22
+ return -np.sum(np.log(y[idx,t]+delta)) / batch_size
23
+
24
+ def gradient(f,x):
25
+ if x.ndim == 1:
26
+ return gradient_sub(f,x)
27
+ else:
28
+ grad = np.zeros_like(x)
29
+ for index, xx in enumerate(x):
30
+ grad[index] = gradient_sub(f,xx)
31
+ return grad
32
+
33
+ def gradient_sub(f,x):
34
+ h = 1e-4
35
+ grad = np.zeros_like(x)
36
+ for i in range(x.size):
37
+ val = x[i]
38
+ x[i] = val + h
39
+ fx1 = f(x)
40
+ x[i] = val - h
41
+ fx2 = f(x)
42
+ grad[i] = (fx1 - fx2) / (2*h)
43
+ x[i] = val
44
+ return grad
45
+
46
+
47
+ class SampleNetwork:
48
+ def __init__(self):
49
+ input_size = 64
50
+ hidden_size = 50
51
+ output_size = 10
52
+ self.params = {}
53
+ self.params["w0"] = 0.01 * np.random.randn(input_size,hidden_size)
54
+ self.params["w1"] = 0.01 * np.random.randn(hidden_size,output_size)
55
+ self.params["b0"] = np.zeros(hidden_size)
56
+ self.params["b1"] = np.zeros(output_size)
57
+
58
+ self.learning_rate = 0.1
59
+
60
+ def predict(self,x):
61
+ a0 = np.dot(x,self.params["w0"]) + self.params["b0"]
62
+ z0 = sigmoid(a0)
63
+ a1 = np.dot(z0,self.params["w1"]) + self.params["b1"]
64
+ y = softmax(a1)
65
+ return y
66
+
67
+ def update_params(self,x,t):
68
+ loss_W = lambda W: self.loss(x,t)
69
+ for key in self.params.keys():
70
+ grad = gradient(loss_W, self.params[key])
71
+ self.params[key] -= self.learning_rate*grad
72
+
73
+
74
+ def loss(self,x,t):
75
+ y = self.predict(x)
76
+ return cross_entropy_error(y,t)
77
+
78
+ def accurary(self,x,t):
79
+ y = self.predict(x)
80
+ y = np.argmax(y,axis=1)
81
+ acc = np.sum(y==t) / float(x.shape[0])
82
+ return acc
83
+
84
+ digits = datasets.load_digits()
85
+ X = digits.data
86
+ T = digits.target
87
+
88
+ (train_X, test_X), (train_T,test_T ) = mnist.load_data()
89
+
90
+ network = SampleNetwork()
91
+
92
+ batch_size = 100
93
+ iter_num = 300
94
+ train_size = train_X.shape[0]
95
+ erpoch_size = max(train_size//batch_size,1)
96
+ loss_list = []
97
+ train_accurary_list = []
98
+ test_accurary_list = []
99
+
2
100
  for index in range(iter_num):
3
101
  batch_choice = np.random.choice(train_size,batch_size)
4
102
  x_batch = train_X[batch_choice]
@@ -6,6 +104,33 @@
6
104
  network.update_params(x_batch,t_batch)
7
105
  loss = network.loss(x_batch,t_batch)
8
106
  loss_list.append(loss)
107
+ if (index % erpoch_size == 0):
108
+ train_accurary = network.accurary(train_X,train_T)
109
+ test_accurary = network.accurary(test_X,test_T)
110
+ train_accurary_list.append(train_accurary)
111
+ test_accurary_list.append(test_accurary)
112
+
113
+
114
+ pyplot.figure(figsize=(10,7))
115
+ pyplot.subplot(2,2,1)
116
+ pyplot.plot(np.arange(len(loss_list)),loss_list)
117
+ pyplot.xlabel("iteration")
118
+ pyplot.title("Cross Entropy Error")
119
+ pyplot.subplot(2,2,2)
120
+ pyplot.plot(np.arange(0,len(train_accurary_list),1),train_accurary_list,"b")
121
+ pyplot.plot(np.arange(0,len(test_accurary_list),1),test_accurary_list,"ro")
122
+ pyplot.xlabel("iteration(epoch)")
123
+ pyplot.title("Accuary")
124
+ pyplot.legend(("train","test"),loc = "lowrer right")
125
+ pyplot.tight_layout()
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
9
134
  ```
10
135
 
11
136
  というコードに対し、