【发布时间】:2020-12-25 10:03:48
【问题描述】:
我有 2 个数据集和一个权重数组。
(train_X, validation_X, train_Y, validation_Y 和 sampleW)
X 集是 3 维的,而 Y 集是 2 维 numpy 数组。
sampleW 是一维 numpy 数组。
如何从fit_generator()成功迁移到fit()函数?
就:
- 是“
fit(x=None, y=None”,代表train_X, train_Y? - 如何分别传递验证数据? (
validation_X, validation_Y) - 我可以像以前一样通过
sampleW吗? - 如何在
fit()上训练分段数据? - 最重要的是:如何在没有生成器的情况下做到这一点?
这是一个最小的可重现性(我目前正在努力找出为什么除 1 之外的任何其他批量大小都会出错,但 >1 也应该可用)
# -*- coding: utf-8 -*-
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,LSTM,BatchNormalization
import tensorflow as tf, numpy as np; from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
tensorboard_path= r"C:\Users\user\documents\session" # <--- your path
checkpoint_path = tensorboard_path
BATCH_SIZE = 1
EPOCHS, Input_shape, labels = 3, (20,4),6
train_X,train_Y = np.asarray([np.random.random(Input_shape) for x in range(100)]), np.random.random((100,labels))
validation_X,validation_Y = np.asarray([np.random.random(Input_shape) for x in range(50)]), np.random.random((50,labels))
sampleW = np.random.random((100,1))
class CustomGenerator_SampleW(tf.keras.utils.Sequence) :
def __init__(self, list_x, labels, batch_size, sample_weights=None) :
self.labels = labels
self.batch_size = batch_size
self.list_x = list_x
self.sample_weights = sample_weights
def __len__(self) :
return (np.ceil(len(self.list_x) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx) :
batch_x = self.list_x[idx * self.batch_size : (idx+1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
batch_weight = self.sample_weights[idx * self.batch_size : (idx+1) * self.batch_size]
return np.array(batch_x),np.array(batch_y), np.array(batch_weight)
class CustomGenerator(tf.keras.utils.Sequence) :
def __init__(self, list_x, labels, batch_size) :
self.labels = labels
self.batch_size = batch_size
self.list_x = list_x
def __len__(self) :
return (np.ceil(len(self.list_x) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx) :
batch_x = self.list_x[idx * self.batch_size : (idx+1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
return np.array(batch_x),np.array(batch_y)
model = Sequential()
model.add(LSTM(242, input_shape=Input_shape, return_sequences=True))
model.add(Dropout(0.3)); model.add(BatchNormalization())
model.add(LSTM(242, return_sequences=True))
model.add(Dropout(0.3)); model.add(BatchNormalization())
model.add(Dense(labels, activation='tanh')); model.add(Dropout(0.3))
opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)
model.compile(loss='mean_absolute_error',optimizer=opt,metrics=['mse'])
if sampleW is not None:
train_batch_gen = CustomGenerator_SampleW(train_X, train_Y, BATCH_SIZE, sample_weights=sampleW)
else: train_batch_gen = CustomGenerator(train_X, train_Y, BATCH_SIZE)
validation_batch_gen = CustomGenerator(validation_X, validation_Y, BATCH_SIZE)
tensorboard = TensorBoard(tensorboard_path)
checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
model.fit_generator(train_batch_gen, steps_per_epoch=None, epochs=EPOCHS,
validation_data = validation_batch_gen, callbacks=[tensorboard,checkpoint])
【问题讨论】:
标签: python machine-learning keras data-science lstm