回答編集履歴

1

追記

2020/12/03 14:02

投稿

meg_
meg_

スコア10602

test CHANGED
@@ -69,3 +69,135 @@
69
69
 
70
70
 
71
71
  上記については``run train()``ではなく``run_train()``です。
72
+
73
+
74
+
75
+ ---
76
+
77
+ 【追記】
78
+
79
+ > なぜ、参考動画と同じなのに、エラーが出てしまっているのか。
80
+
81
+
82
+
83
+ 同じではないからです。forwardのインデントが間違っています。
84
+
85
+
86
+
87
+ <誤>
88
+
89
+ ```Python
90
+
91
+ class Model(nn.Module):
92
+
93
+
94
+
95
+ def __init__(self, input=1, h=50, output=1):
96
+
97
+ super().__init__()
98
+
99
+ self.hidden_size = h
100
+
101
+
102
+
103
+ self.lstm = nn.LSTM(input, h)
104
+
105
+ self.fc = nn.Linear(h, output)
106
+
107
+
108
+
109
+ self.hidden = (
110
+
111
+ torch.zeros(1, 1, h),
112
+
113
+ torch.zeros(1, 1, h)
114
+
115
+ )
116
+
117
+
118
+
119
+ def forward(self, seq):
120
+
121
+
122
+
123
+ out,_=self.lstm(
124
+
125
+ seq.view(len(seq), 1, -1),
126
+
127
+ self.hidden
128
+
129
+ )
130
+
131
+
132
+
133
+ out = self.fc(
134
+
135
+ out.view(len(seq), -1)
136
+
137
+ )
138
+
139
+
140
+
141
+ return out[-1]
142
+
143
+ ```
144
+
145
+
146
+
147
+ <正>
148
+
149
+ ```Python
150
+
151
+ class Model(nn.Module):
152
+
153
+
154
+
155
+ def __init__(self, input=1, h=50, output=1):
156
+
157
+ super().__init__()
158
+
159
+ self.hidden_size = h
160
+
161
+
162
+
163
+ self.lstm = nn.LSTM(input, h)
164
+
165
+ self.fc = nn.Linear(h, output)
166
+
167
+
168
+
169
+ self.hidden = (
170
+
171
+ torch.zeros(1, 1, h),
172
+
173
+ torch.zeros(1, 1, h)
174
+
175
+ )
176
+
177
+
178
+
179
+ def forward(self, seq):
180
+
181
+
182
+
183
+ out, _ = self.lstm(
184
+
185
+ seq.view(len(seq), 1, -1),
186
+
187
+ self.hidden
188
+
189
+ )
190
+
191
+
192
+
193
+ out = self.fc(
194
+
195
+ out.view(len(seq), -1)
196
+
197
+ )
198
+
199
+
200
+
201
+ return out[-1]
202
+
203
+ ```