教科書のコードと入力したところ表題のようなエラーがでます.
ご教授お願いできますでしょうか?
In [29]:
1
2
%matplotlib inline
3
4
import numpy as np
5
import matplotlib.pyplot as plt
6
7
input_data = np.arange(0,np.pi*2,0.1)
8
correct_data = np.sin(input_data)
9
input_data = (input_data-np.pi)/np.pi
10
n_data = len(correct_data)
11
12
n_in = 1
13
n_mid = 3
14
n_out = 1
15
16
wb_width = 0.01
17
eta = 0.1
18
epoch = 2001
19
interval = 200
20
21
class MiddleLayer:
22
def int(self,n_upper,n):
23
self.w = wb_width*np.random.randn(n_upper,n)
24
self.b = wb_width*np.random.randn(n)
25
def forward(self,x):
26
self.x = x
27
u = np.dot(x,self.w) + self.b
28
self.y = 1/(1+np.exp(-u))
29
def backward(self,grad_y):
30
delta = grad_y * (1-self.y)*self.y
31
self.grad_w=np.dot(self.x.T,delta)
32
self.grad_b=np.sum(delta,axis=0)
33
34
self.grad_x=np.dot(delta,self.w.T)
35
def updata(self,eta):
36
self -=eta*self.grad_w
37
self.b -=eta*self.grad.b
38
39
class OutputLayer:
40
def int(self,n_upper,n):
41
self.w = wb_width*np.random.randn(n_upper,n)
42
self.b = wb_width*np.random.rand(n)
43
44
def foward(self,x):
45
self.x=x
46
u=np.dot(x,self.w)+self.b
47
self.y=u
48
49
def backward(self,t):
50
delta=self.y-t
51
52
self.grad_w=np.dot(self.x.T,delta)
53
self.grad_b=np.sum(delta,self.w.T)
54
55
def updata(self,eta):
56
self.w-=eta*self.grad_w
57
self.b-=eta*self.grad_b
58
59
middle_layer = MiddleLayer(n_in,n_mid)
60
output_layer = OutputLayer(n_mid,n_out)
61
62
for i in range(epoch):
63
64
index_random = np.arange(n_data)
65
np.random.shuffle(index_random)
66
67
total_error = 0
68
plot_x=[]
69
plot_y=[]
70
71
for idx in index_random:
72
73
x=input_data[idx:idx+1]
74
t=correct_data[idx:idx+1]
75
76
middle_layer.forward(x.reshape(1,1))
77
output_layer.forward(middle_layer.y)
78
79
output_layer.backward(t.reshape(1,1))
80
middle_layer.backward(output_layer.grad_x)
81
82
middle_layer.updata(eta)
83
output_layer.updata(eta)
84
85
if i%interval==0:
86
y=output_layer.y.shape(-1)
87
88
total_error += 1.0/2.0*np.sum(np.square(y-t))
89
90
plot_x.append(x)
91
plot_y.append(y)
92
93
if i%interval==0:
94
95
plt.plot(input_data,correct_data,limestyle="dashed")
96
plt.scatter(plot_x,plot_y,marker="+")
97
pltshow()
98
print("Epoch:"+str(i)+"/"+str(epoch),"Error:"+str(total_error/n_data))
TypeError Traceback (most recent call last)
<ipython-input-29-d29584c2fd2f> in <module>
57 self.b-=eta*self.grad_b
58
---> 59 middle_layer = MiddleLayer(n_in,n_mid)
60 output_layer = OutputLayer(n_mid,n_out)
61
TypeError: MiddleLayer() takes no arguments
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 14:08
2019/05/08 14:11