質問編集履歴
1
写真の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -195,3 +195,145 @@
|
|
195
195
|
|
196
196
|
|
197
197
|
BEST REGARDS.
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
![イメージ説明](606b3e862f8144ad2723f6d6bfc5974a.png)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
変更後
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
import torch
|
214
|
+
|
215
|
+
import torch.nn as nn
|
216
|
+
|
217
|
+
import torch.nn.functional as F
|
218
|
+
|
219
|
+
from torch.utils.data import Dataset, DataLoader
|
220
|
+
|
221
|
+
from sklearn.model_selection import train_test_split
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
import pandas as pd
|
226
|
+
|
227
|
+
import matplotlib.pyplot as plt
|
228
|
+
|
229
|
+
%matplotlib inline
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
class Model(nn.Module):
|
234
|
+
|
235
|
+
def __init__(self, in_features=4, h1=8, h2=9,out_features=3):
|
236
|
+
|
237
|
+
super().__init__()
|
238
|
+
|
239
|
+
self.fc1 = nn.Linear(in_features, h1)
|
240
|
+
|
241
|
+
self.fc2 = nn.Linear(h1,h2)
|
242
|
+
|
243
|
+
self.out = nn.Linear(h2,in_features)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def forward(self,x):
|
248
|
+
|
249
|
+
x = F.relu(self.fc1(x))
|
250
|
+
|
251
|
+
x = F.relu(self.fc2(x))
|
252
|
+
|
253
|
+
x = self.out(x)
|
254
|
+
|
255
|
+
return x
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
torch.manual_seed(32)
|
262
|
+
|
263
|
+
model=Model()
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
df = pd.read_csv('../Data/iris.csv')
|
268
|
+
|
269
|
+
df.head()
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
X = X = df.drop('target',axis=1).values
|
274
|
+
|
275
|
+
y = df['target'].values
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=33)
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
X_train = torch.FloatTensor(X_train)
|
284
|
+
|
285
|
+
X_test = torch.FloatTensor(X_test)
|
286
|
+
|
287
|
+
# y_train = F.one_hot(torch.LongTensor(y_train)) # not needed with Cross Entropy Loss
|
288
|
+
|
289
|
+
# y_test = F.one_hot(torch.LongTensor(y_test))
|
290
|
+
|
291
|
+
y_train = torch.LongTensor(y_train)
|
292
|
+
|
293
|
+
y_test = torch.LongTensor(y_test)
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
criterion = nn.CrossEntropyLoss()
|
298
|
+
|
299
|
+
optimizer = torch.optim.Adam(model.parameters(),lr=0.01)
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
model.parameters()
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
epochs = 100
|
310
|
+
|
311
|
+
lossses = []
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
for i in range(epochs):
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
i+=1
|
320
|
+
|
321
|
+
y_pred = model.forward(X_train)
|
322
|
+
|
323
|
+
loss = criterion(y_test, y_pred)
|
324
|
+
|
325
|
+
losses.append(loss)
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
if i%10 == 1:
|
330
|
+
|
331
|
+
print(f'epoch: {i:2} loss:{loss.item():10.8f}')
|
332
|
+
|
333
|
+
optimizer.zero_grad()
|
334
|
+
|
335
|
+
loss.backward()
|
336
|
+
|
337
|
+
optimizer.step()
|
338
|
+
|
339
|
+
```
|