KerasのModelクラスを使用した下記の2種類のコードがあります。
前者(fit)は学習が進む(training lossが徐々に低下する)のに対し、
後者(train_on_batch)は、学習が進まず、training lossが大きく上下にぶれてしまいます。
下記に記載のコード部分を除く、データのロードやモデルの定義部分はすべて同じコードになっています。
両者のlossが乖離した決定的な原因をご教示ください。
train_x: 学習データ shape = (データ数, 224, 224, 3)
train_y: 教師ラベル shape = (データ数, 1)
BATCH_SIZE: バッチサイズ(16)
python
1... 2 3BATCH_SIZE = 16 4EPOCH = 30 5 6model.compile(loss='mse', optimizer='Adam') 7 8history = model.fit(train_x, train_y, batch_size=BATCH_SIZE , epochs=EPOCH , verbose=1)
python
1... 2 3BATCH_SIZE = 16 4EPOCH = 30 5 6model.compile(loss='mse', optimizer='Adam') 7 8from sklearn.utils import shuffle 9 10history = [] 11 12for e in range(EPOCH): 13 train_x, train_y = shuffle(train_x, train_y) 14 15 for it in (range(train_x.shape[0]//BATCH_SIZE): 16 17 batch_x = train_x[it*BATCH_SIZE: (it+1)*BATCH_SIZE] 18 batch_y = train_y[it*BATCH_SIZE: (it+1)*BATCH_SIZE] 19 20 train_loss = model.train_on_batch(batch_x, batch_y) 21 history.append(train_loss)
あなたの回答
tips
プレビュー