質問編集履歴

5

文法修正

2020/11/30 08:19

投稿

PPAP_AWS
PPAP_AWS

スコア105

test CHANGED
File without changes
test CHANGED
@@ -372,7 +372,7 @@
372
372
 
373
373
 
374
374
 
375
- ```エラーコード
375
+ ```Error
376
376
 
377
377
  Epoch: 1
378
378
 

4

変更点追加

2020/11/30 08:19

投稿

PPAP_AWS
PPAP_AWS

スコア105

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,11 @@
20
20
 
21
21
  import matplotlib.pyplot as plt
22
22
 
23
-
23
+ ```
24
+
25
+
26
+
27
+ ```python
24
28
 
25
29
  stock_data = pd.read_csv(
26
30
 
@@ -318,6 +322,10 @@
318
322
 
319
323
 
320
324
 
325
+ ```
326
+
327
+ ```python
328
+
321
329
  for epoch in range(epochs):
322
330
 
323
331
 
@@ -360,13 +368,11 @@
360
368
 
361
369
  plt.show()
362
370
 
363
-
371
+ ```
364
-
372
+
373
+
374
+
365
- エラー内容
375
+ ```エラーコード
366
-
367
-
368
-
369
-
370
376
 
371
377
  Epoch: 1
372
378
 
@@ -406,6 +412,4 @@
406
412
 
407
413
  AttributeError: 'Tensor' object has no attribute 'vierq'
408
414
 
409
- コード
410
-
411
415
  ```

3

コード追加

2020/11/30 08:18

投稿

PPAP_AWS
PPAP_AWS

スコア105

test CHANGED
File without changes
test CHANGED
@@ -6,43 +6,349 @@
6
6
 
7
7
 
8
8
 
9
+ 一連の流れ。
10
+
9
11
  ```python
10
12
 
11
-
13
+ import torch
14
+
15
+ import torch.nn as nn
16
+
17
+ import numpy as np
18
+
19
+ import pandas as pd
20
+
21
+ import matplotlib.pyplot as plt
22
+
23
+
24
+
25
+ stock_data = pd.read_csv(
26
+
27
+ "/content/drive/MyDrive/^GSPC.csv",
28
+
29
+ index_col = 0,
30
+
31
+ parse_dates=True
32
+
33
+ )
34
+
35
+
36
+
37
+ stock_data
38
+
39
+
40
+
41
+ stock_data.drop(
42
+
43
+ ["Open", "High", "Low", "Close", "Volume"],
44
+
45
+ axis="columns",
46
+
47
+ inplace=True
48
+
49
+ )
50
+
51
+
52
+
53
+ stock_data
54
+
55
+
56
+
57
+ stock_data.plot(figsize=(12, 4))
58
+
59
+
60
+
61
+ y = stock_data["Adj Close"].values
62
+
63
+ y
64
+
65
+
66
+
67
+ from sklearn.preprocessing import MinMaxScaler
68
+
69
+
70
+
71
+ scaler = MinMaxScaler(feature_range=(-1, 1))
72
+
73
+ scaler.fit(y.reshape(-1, 1))
74
+
75
+ y =scaler.transform(y.reshape(-1, 1))
76
+
77
+ y
78
+
79
+
80
+
81
+ y = torch.FloatTensor(y).view(-1)
82
+
83
+ y
84
+
85
+
86
+
87
+ test_size = 24
88
+
89
+
90
+
91
+ train_seq = y[:-test_size]
92
+
93
+ test_seq = y[-test_size:]
94
+
95
+
96
+
97
+ plt.figure(figsize=(12, 4))
98
+
99
+ plt.xlim(-20, len(train_seq)+20)
100
+
101
+ plt.grid(True)
102
+
103
+ plt.plot(train_seq)
104
+
105
+
106
+
107
+ train_window_size = 12
108
+
109
+
110
+
111
+ def input_data(seq, ws):
112
+
113
+ out = []
114
+
115
+ L = len(seq)
116
+
117
+
118
+
119
+ for i in range(L-ws):
120
+
121
+ window = seq[i:i+ws]
122
+
123
+ label = seq[i+ws:i+ws+1]
124
+
125
+ out.append((window, label))
126
+
127
+
128
+
129
+ return out
130
+
131
+
132
+
133
+ train_data = input_data(train_seq, train_window_size)
134
+
135
+
136
+
137
+ print("The NUmber of Training Data: ",len(train_data))
138
+
139
+
140
+
141
+ class Model(nn.Module):
142
+
143
+
144
+
145
+ def __init__(self, input=1, h=50, output=1):
146
+
147
+
148
+
149
+ super().__init__()
150
+
151
+ self.hidden_size = h
152
+
153
+
154
+
155
+ self.lstm = nn.LSTM(input, h)
156
+
157
+ self.fc = nn.Linear(h, output)
158
+
159
+
160
+
161
+ self.hidden = (
162
+
163
+ torch.zeros(1, 1, h),
164
+
165
+ torch.zeros(1, 1, h)
166
+
167
+ )
168
+
169
+
170
+
171
+
172
+
173
+ def forward(self, seq):
174
+
175
+
176
+
177
+ out, _=self.lstm(
178
+
179
+ seq.vierq(len(seq), 1, -1),
180
+
181
+ self.hidden
182
+
183
+ )
184
+
185
+
186
+
187
+ out = self.fc(
188
+
189
+ out.view(len(seq), -1)
190
+
191
+ )
192
+
193
+
194
+
195
+ return out[-1]
196
+
197
+
198
+
199
+ torch.manual_seed(123)
200
+
201
+ model = Model()
202
+
203
+ criterion = nn.MSELoss()
204
+
205
+ optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
206
+
207
+
208
+
209
+ epochs = 10
210
+
211
+ train_losses = []
212
+
213
+ test_losses = []
214
+
215
+
216
+
217
+ def run_train():
218
+
219
+ model.train()
220
+
221
+
222
+
223
+ for train_window, correct_label in train_data:
224
+
225
+ optimizer.zero_grad()
226
+
227
+
228
+
229
+ model.hidden = (
230
+
231
+ torch.zeros(1, 1, model.hidden_size),
232
+
233
+ torch.zeros(1, 1, model.hidden_size),
234
+
235
+
236
+
237
+ )
238
+
239
+
240
+
241
+ train_predicred_label = model.forward(train_window)
242
+
243
+ train_loss = criterion(train_predicted_label, correct_label)
244
+
245
+
246
+
247
+ train_loss.backward()
248
+
249
+ optimizer.step()
250
+
251
+
252
+
253
+ train_losses.append(train_loss)
254
+
255
+
256
+
257
+ a = torch.tensor([3])
258
+
259
+ a.item()
260
+
261
+
262
+
263
+ def run_test():
264
+
265
+ model.eval()
266
+
267
+
268
+
269
+ for i in range(test_size):
270
+
271
+
272
+
273
+ test_window = torch.FloatTensor(extending_seq[-test_size:])
274
+
275
+
276
+
277
+ with torch.no_grad():
278
+
279
+
280
+
281
+ model.hidden = (
282
+
283
+ torch.zeros(1, 1, model.hidden_size),
284
+
285
+ torch.zeros(1, 1, model.hidden_size),
286
+
287
+
288
+
289
+ )
290
+
291
+
292
+
293
+ test_predicted_label = mode.forward(test_window)
294
+
295
+ extending_seq.append(test_predicted_label())
296
+
297
+
298
+
299
+ test_loss = criterion(
300
+
301
+ torch.FloatTensor(extending_seq[-test_size:]),
302
+
303
+ y[len(y)-test_size:]
304
+
305
+ )
306
+
307
+
308
+
309
+ test_losses.append(test_loss)
310
+
311
+
312
+
313
+ train_seq[-test_size:]
314
+
315
+
316
+
317
+ train_seq[-test_size:].tolist()
318
+
319
+
12
320
 
13
321
  for epoch in range(epochs):
14
322
 
323
+
324
+
15
325
  print()
16
326
 
17
327
  print(f'Epoch: {epoch+1}')
18
328
 
19
329
 
20
330
 
21
- run_train()
331
+ run_train()
22
-
23
-
24
-
332
+
333
+
334
+
25
- extending_seq = train_seq[-test_size:].to;ost()
335
+ extending_seq = train_seq[-test_size:].tolist()
26
-
27
-
28
-
336
+
337
+
338
+
29
- run_test()
339
+ run_test()
30
-
31
-
32
-
340
+
341
+
342
+
33
- plt.figure(figsize=(12, 4))
343
+ plt.figure(figsize=(12, 4))
34
-
344
+
35
- plt.xlim(-20, len(y)+20)
345
+ plt.xlim(-21, len(y)+20)
36
-
346
+
37
- plt.grid(True)
347
+ plt.grid(True)
38
-
39
-
40
-
41
- plt.plot(y.numpy())
348
+
42
-
43
-
44
-
349
+
350
+
45
- plt.plot(
351
+ plt.plot(
46
352
 
47
353
  range(len(y)-test_size, len(y)),
48
354
 
@@ -52,30 +358,54 @@
52
358
 
53
359
 
54
360
 
55
- plt.show()
361
+ plt.show()
362
+
363
+
364
+
365
+ ↓エラー内容
366
+
367
+
368
+
369
+
370
+
371
+ Epoch: 1
372
+
373
+ ---------------------------------------------------------------------------
374
+
375
+ AttributeError Traceback (most recent call last)
376
+
377
+ <ipython-input-85-98e8d85b09e9> in <module>()
378
+
379
+ 4 print(f'Epoch: {epoch+1}')
380
+
381
+ 5
382
+
383
+ ----> 6 run_train()
384
+
385
+ 7
386
+
387
+ 8 extending_seq = train_seq[-test_size:].tolist()
388
+
389
+
390
+
391
+ 1 frames
392
+
393
+ <ipython-input-77-b3af88443259> in forward(self, seq)
394
+
395
+ 18
396
+
397
+ 19 out, _=self.lstm(
398
+
399
+ ---> 20 seq.vierq(len(seq), 1, -1),
400
+
401
+ 21 self.hidden
402
+
403
+ 22 )
404
+
405
+
406
+
407
+ AttributeError: 'Tensor' object has no attribute 'vierq'
408
+
409
+ コード
56
410
 
57
411
  ```
58
-
59
- ↓エラーコード
60
-
61
- ```error
62
-
63
- NameError Traceback (most recent call last)
64
-
65
- <ipython-input-11-24515b2a1814> in <module>()
66
-
67
- ----> 1 for epoch in range(epochs):
68
-
69
- 2 print()
70
-
71
- 3 print(f'Epoch: {epoch+1}')
72
-
73
- 4
74
-
75
- 5 run_train()
76
-
77
-
78
-
79
- NameError: name 'epochs' is not defined
80
-
81
- ```

2

変更点追加

2020/11/30 06:28

投稿

PPAP_AWS
PPAP_AWS

スコア105

test CHANGED
@@ -1 +1 @@
1
- python 株分析 練習 エラーコード
1
+ NameError: name 'epochs' is not defined python エラーコード
test CHANGED
File without changes

1

文法修正

2020/11/30 05:39

投稿

PPAP_AWS
PPAP_AWS

スコア105

test CHANGED
@@ -1 +1 @@
1
- 価の分析実装中 エラーコード
1
+ python 株分析 練習 エラーコード
test CHANGED
File without changes