質問編集履歴

2

追加の修正

2023/05/23 10:20

投稿

sasa__tomo
sasa__tomo

スコア1

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,6 @@
60
60
  ```
61
61
  ### 発生している問題・エラーメッセージ
62
62
 
63
- モデル訓練時for文でdataloaderの辞書を作成したときのみエラーを吐きます。
64
63
  145の数値から辞書型train_loader_dictから取り出したtrain_loaderがfor文で最後に格納したtrain_loaderに置き換わっているのかなと思います。
65
64
 
66
65
  ```

1

質問全体の修正

2023/05/23 10:19

投稿

sasa__tomo
sasa__tomo

スコア1

test CHANGED
File without changes
test CHANGED
@@ -5,33 +5,11 @@
5
5
 
6
6
  pytorchでLSTMモデルの訓練をしているとき、気になったことがあったので質問させていただきます。
7
7
 
8
- 4つのcsvデータを読み込みそれぞれのデータセット用のデータローダを作る過程でfor文を使うとうまくかずfor文使わずべた書きするうまくいきました
8
+ 4つのcsvデータを読み込みそれぞれのデータセット用のデータローダを格納した辞書型配列を使い、モデルの訓練行うエラーが起きま
9
9
 
10
- 以下の二つのコードは同じことをしてるつもりで作成しています(でもモデル訓練時片方だけエラーが起きる)
11
- ```python
12
- #うまくいったコード(べた書き)
13
-
14
- df1 = pd.read_csv("./outlier_detection_xy/data/"+"IMG_0569_20_1840"+"/position_data_labeled.csv")
15
- df2 = pd.read_csv("./outlier_detection_xy/data/"+"IMG_0569_3140_4120"+"/position_data_labeled.csv")
16
- df3 = pd.read_csv("./outlier_detection_xy/data/"+"IMG_0893_1940_2060"+"/position_data_labeled.csv")
17
- df4 = pd.read_csv("./outlier_detection_xy/data/"+"IMG_0893_7080_7260"+"/position_data_labeled.csv")
18
- df1=df[df['label']=='normal']
19
- df2=df[df['label']=='normal']
20
- df3=df[df['label']=='normal']
21
- df4=df[df['label']=='normal']
22
- y1=df1['RIGHT_ANKLE_x'].values-df1['LEFT_ANKLE_x'].values
23
- y2=df2['RIGHT_ANKLE_x'].values-df2['LEFT_ANKLE_x'].values
24
- y3=df1['RIGHT_ANKLE_x'].values-df1['LEFT_ANKLE_x'].values
25
- y4=df2['RIGHT_ANKLE_x'].values-df2['LEFT_ANKLE_x'].values
26
- train_loader1=DataLoader(Dataset(y1),batch_size=4, shuffle=False, drop_last=True)
27
- train_loader2=DataLoader(Dataset(y2),batch_size=4, shuffle=False, drop_last=True)
28
- train_loader3=DataLoader(Dataset(y1),batch_size=4, shuffle=False, drop_last=True)
29
- train_loader4=DataLoader(Dataset(y2),batch_size=4, shuffle=False, drop_last=True)
30
- train_loader_dict={"IMG_0569_20_1840":train_loader1,"IMG_0569_3140_4120":train_loader2,"IMG_0893_1940_2060":train_loader3,"IMG_0893_7080_7260":train_loader4}
31
- ```
32
10
 
33
11
  ```python
34
- #うまくいかなかっコード
12
+ #Dataloaderを格納し辞書型作成
35
13
 
36
14
  train_loader_dict={}
37
15
 
@@ -46,7 +24,7 @@
46
24
  y=df['RIGHT_ANKLE_x'].values-df['LEFT_ANKLE_x'].values
47
25
 
48
26
  train_dataset=Dataset(y)
49
- print(train_dataset.__len__())
27
+
50
28
  train_loader=DataLoader(train_dataset,batch_size=4, shuffle=False, drop_last=True)
51
29
  train_loader_dict[video]=train_loader
52
30
 
@@ -139,6 +117,43 @@
139
117
  print('%d loss: %.3f, training_accuracy: %.5f' % (epoch + 1, running_loss, training_accuracy))
140
118
  ```
141
119
 
120
+ ### 実践したこと
121
+ dataloader一つを使った場合うまくいった。
122
+
123
+ ```python
124
+ #データローダ作成
125
+ df = pd.read_csv("./outlier_detection_xy/data/"+"IMG_0893_7080_7260"+"/position_data_labeled.csv")
126
+
127
+ df=df[df['label']=='normal']
128
+
129
+ y=df['RIGHT_ANKLE_x'].values-df['LEFT_ANKLE_x'].values
130
+
131
+ train_loader=DataLoader(Dataset(y),batch_size=4, shuffle=False, drop_last=True)
132
+
133
+
134
+ #モデル訓練
135
+ from pandas.core.groupby.groupby import OutputFrameOrSeries
136
+ for epoch in range(epochs_num):
137
+ # training
138
+ running_loss = 0.0
139
+ training_accuracy = 0.0
140
+
141
+ for train_data, train_label in train_loader:
142
+
143
+   optimizer.zero_grad()
144
+ output = model(train_data)
145
+
146
+ loss = criterion(output, train_label.reshape(-1,1))
147
+ loss.backward()
148
+ optimizer.step()
149
+
150
+ running_loss += loss.item()
151
+
152
+
153
+ print('%d loss: %.3f, training_accuracy: %.5f' % (epoch + 1, running_loss, training_accuracy))
154
+
155
+ ```
156
+
142
157
 
143
158
  ### 補足情報(FW/ツールのバージョンなど)
144
159