質問編集履歴
2
エラーが増えました あとコード全体乗せときます
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,27 +1,54 @@
|
|
1
1
|
```ここに言語を入力
|
2
2
|
コード
|
3
|
+
import numpy as np
|
4
|
+
class NN:
|
5
|
+
def __init__(self,):
|
6
|
+
self.N1=None
|
7
|
+
self.N2=None
|
8
|
+
self.N3=None
|
9
|
+
self.N4=None
|
10
|
+
n=NN()
|
11
|
+
|
12
|
+
class Relu:
|
13
|
+
def __init__(self,inp):
|
14
|
+
self.next=inp
|
15
|
+
self.N=self.next.N
|
16
|
+
self.mask = None
|
17
|
+
def f(self,x):
|
18
|
+
return np.maximum(0, x)
|
19
|
+
def b(self,dout,eta):
|
20
|
+
dout[self.mask] = 0
|
21
|
+
dx = dout
|
22
|
+
self.next.b(dx,eta)
|
23
|
+
|
24
|
+
class imput:
|
25
|
+
def __init__(self,N):
|
26
|
+
self.N=N
|
27
|
+
def start(self,inp):
|
28
|
+
return inp
|
29
|
+
def b():
|
30
|
+
print('学習完了')
|
3
31
|
class afine:
|
4
32
|
def __init__(self,N,inp):# インスタンス変数の初期化
|
5
33
|
self.N=N
|
6
34
|
self.next=inp
|
7
|
-
self.w = np.random.rand(self.next.N,N) * (2.0 /self.N)
|
35
|
+
self.w = np.random.rand(self.next.N,N) * (2.0 /self.N) # 重みは固定
|
8
|
-
self.
|
36
|
+
self.by = np.zeros(N)
|
9
37
|
self.x = None
|
10
38
|
self.dW = None
|
11
39
|
self.db = None
|
12
|
-
def f(self,
|
40
|
+
def f(self,x):
|
13
41
|
self.x = x #入力を引数で渡す。
|
14
|
-
return np.dot(
|
42
|
+
return np.dot(self.x, self.w) + self.by # バイアス+重みx入力の行列を渡す
|
15
43
|
|
16
44
|
def b(self,dout,eta):
|
17
|
-
dx = np.dot(dout, self.
|
45
|
+
dx = np.dot(dout, self.w.T)#重みの形状の転置を行なって、それをdoutでdotする。
|
18
46
|
self.dW = np.dot(self.x.T, dout)# 入力の形状の転置を行なって、それをdoutでdotする。
|
19
47
|
self.db = np.sum(dout, axis=0)#バイアスはaxis=0で微分する。
|
20
48
|
self.w = self.w - eta*self.dW1
|
21
|
-
self.
|
49
|
+
self.by = self.by - eta*self.db1
|
22
50
|
print('ddd',dx)
|
23
51
|
self.next.b(dx,eta)
|
24
|
-
|
25
52
|
class output:
|
26
53
|
def __init__(self,zz):
|
27
54
|
self.next=zz
|
@@ -31,7 +58,6 @@
|
|
31
58
|
u = np.sum(x)
|
32
59
|
return x/u
|
33
60
|
def loss(self,d,y,eta):
|
34
|
-
|
35
61
|
batch_size = d.shape[0]
|
36
62
|
if d.size == y.size: # 教師データがone-hot-vectorの場合
|
37
63
|
dx = (y - d) / batch_size
|
@@ -39,34 +65,68 @@
|
|
39
65
|
dx = y.copy()
|
40
66
|
dx[np.arange(batch_size), d] -= 1
|
41
67
|
dx = dx / batch_size
|
42
|
-
print('
|
68
|
+
print('aaaa',dx)
|
43
69
|
self.next.b(dx,eta)
|
44
70
|
|
71
|
+
"""
|
72
|
+
=====パラメーター======
|
73
|
+
epoc=エピソード
|
74
|
+
step=ステップ数
|
75
|
+
eta=学習率
|
76
|
+
"""
|
77
|
+
|
78
|
+
|
79
|
+
eta=0.01
|
80
|
+
epoc=1000
|
81
|
+
step=2000
|
82
|
+
#imput= input型
|
83
|
+
# mx= ニューロン数 前の層
|
84
|
+
#output= input
|
85
|
+
i=imput(10)
|
86
|
+
m1=afine(20,i)
|
87
|
+
r1=Relu(m1)
|
88
|
+
m2=afine(20,r1)
|
89
|
+
r2=Relu(m2)
|
45
|
-
m3=afine(10,
|
90
|
+
m3=afine(10,r2)
|
46
91
|
o=output(m3)
|
92
|
+
#start(何をインプットにするか)
|
93
|
+
#mx.f(どのニューロン群を計算するか)
|
94
|
+
#o.f(何番目のニューロン群を使う)
|
95
|
+
zz=np.array([19,2,31,4,5,12,45,89,3,134])
|
96
|
+
n.N1=i.start(zz)
|
97
|
+
n.N1=m1.f(n.N1)
|
98
|
+
n.N1=r1.f(n.N1)
|
99
|
+
n.N1=m2.f(n.N1)
|
100
|
+
n.N1=r2.f(n.N1)
|
101
|
+
n.N1=m3.f(n.N1)
|
102
|
+
n.N1=o.f(n.N1)
|
47
103
|
|
104
|
+
y=np.array([0,0,0,1,0,0,0,0,0,0])
|
105
|
+
#出力 出力 eta
|
48
106
|
o.loss(n.N1,y,0.01)
|
107
|
+
```
|
108
|
+
新しく出てきたエラー
|
109
|
+
-
|
110
|
+
ValueError Traceback (most recent call last)
|
111
|
+
<ipython-input-14-0b3cebe302aa> in <module>
|
112
|
+
102 y=np.array([0,0,0,1,0,0,0,0,0,0])
|
113
|
+
103 #出力 出力 eta
|
114
|
+
--> 104 o.loss(n.N1,y,0.01)
|
49
115
|
|
116
|
+
<ipython-input-14-0b3cebe302aa> in loss(self, d, y, eta)
|
117
|
+
65 dx = dx / batch_size
|
118
|
+
66 print('aaaa',dx)
|
119
|
+
---> 67 self.next.b(dx,eta)
|
120
|
+
68
|
121
|
+
69 """
|
50
122
|
|
51
|
-
```
|
52
|
-
出ているエラー
|
53
|
-
|
123
|
+
<ipython-input-14-0b3cebe302aa> in b(self, dout, eta)
|
124
|
+
42 def b(self,dout,eta):
|
54
|
-
|
125
|
+
43 dx = np.dot(dout, self.w.T)#重みの形状の転置を行なって、それをdoutでdotする。
|
126
|
+
---> 44 self.dW = np.dot(self.x.T, dout)# 入力の形状の転置を行なって、それをdoutでdotする。
|
127
|
+
45 self.db = np.sum(dout, axis=0)#バイアスはaxis=0で微分する。
|
55
|
-
|
128
|
+
46 self.w = self.w - eta*self.dW1
|
56
129
|
|
57
|
-
TypeError Traceback (most recent call last)
|
58
|
-
<
|
130
|
+
<__array_function__ internals> in dot(*args, **kwargs)
|
59
|
-
88 y=np.array([0,0,0,1,0,0,0,0,0,0])
|
60
|
-
89
|
61
|
-
---> 90 o.loss(n.N1,y,0.01)
|
62
|
-
91
|
63
|
-
92
|
64
131
|
|
65
|
-
<ipython-input-151-479b73aa2f95> in loss(self, d, y, eta)
|
66
|
-
59 dx = dx / batch_size
|
67
|
-
60 print('ddd',dx)
|
68
|
-
---> 61 self.next.b(dx,eta)
|
69
|
-
62 """
|
70
|
-
63 =====パラメーター======
|
71
|
-
|
72
|
-
|
132
|
+
ValueError: shapes (20,) and (10,) not aligned: 20 (dim 0) != 10 (dim 0)
|
1
bについて記述しました bは関数です
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
```ここに言語を入力
|
2
2
|
コード
|
3
|
+
class afine:
|
4
|
+
def __init__(self,N,inp):# インスタンス変数の初期化
|
5
|
+
self.N=N
|
6
|
+
self.next=inp
|
7
|
+
self.w = np.random.rand(self.next.N,N) * (2.0 /self.N)
|
8
|
+
self.b = np.zeros(N)
|
9
|
+
self.x = None
|
10
|
+
self.dW = None
|
11
|
+
self.db = None
|
12
|
+
def f(self,inp):
|
13
|
+
self.x = x #入力を引数で渡す。
|
14
|
+
return np.dot(inp, self.w) + self.b
|
15
|
+
|
16
|
+
def b(self,dout,eta):
|
17
|
+
dx = np.dot(dout, self.W.T)#重みの形状の転置を行なって、それをdoutでdotする。
|
18
|
+
self.dW = np.dot(self.x.T, dout)# 入力の形状の転置を行なって、それをdoutでdotする。
|
19
|
+
self.db = np.sum(dout, axis=0)#バイアスはaxis=0で微分する。
|
20
|
+
self.w = self.w - eta*self.dW1
|
21
|
+
self.b = self.b - eta*self.db1
|
22
|
+
print('ddd',dx)
|
23
|
+
self.next.b(dx,eta)
|
3
24
|
|
4
25
|
class output:
|
5
26
|
def __init__(self,zz):
|