我正在尝试在len N的多个序列上使用GRU层训练RNN。如何设置GRU层的输入以及我的训练和验证数据?
我尝试了不同的input_shape(例如(None,N,2),(N,2),(None,2)...),我的数据有2列,并且序列的大小为N。>
def get_batch(train, ind):
X = np.array([train.loc[i * N: (i+1) * N - 1][['col_1', 'col_2']].values for i in ind])
Y = np.array([train.loc[i * N: (i+1) * N - 1][['target']].values for i in ind])
return X,Y
model = Sequential()
model.add(CuDNNGRU(20, input_shape=(2, 1)))
model.add(Dropout(.4))
model.add(Dense(20, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(1))
model.compile(optimizer=adam(lr=0.005), loss="mae")
def cross_val(ind, train):
fold_size = int(len(ind) / 5)
for i in range(5):
val_ind = ind[i * fold_size: (i+1) * fold_size]
train_ind = [x for x in ind if x not in val_ind]
X_train, Y_train = get_batch(train, train_ind)
X_val, Y_val = get_batch(train, val_ind)
model.fit(X_train, Y_train, validation_data=(X_val, Y_val))
cross_val(ind, train)
我最终遇到这种错误:
ValueError: Error when checking target: expected dense_54 to have 2 dimensions, but got array with shape (8, 4096, 1)
答案 0 :(得分:0)
好吧,我想我明白了,您的模型必须是:
from keras.layers import TimeDistributed
model = Sequential()
model.add(CuDNNGRU(20, return_sequences=True, input_shape=(2, 1)))
model.add(TimeDistributed(Dropout(.4)))
model.add(TimeDistributed(Dense(20, activation='relu')))
model.add(TimeDistributed(Dropout(.5)))
model.add(TimeDistributed(Dense(1)))
由于每个时间步有一个输出,所以要返回一个序列
答案 1 :(得分:0)
我尝试了一些事情,并且设法训练了我的模型,但是一次只能训练一个序列。
def create_model():
model = Sequential()
model.add(CuDNNGRU(20, input_shape=(2, 1)))
model.add(Dense(20, activation='relu'))
model.add(Dense(1))
model.compile(optimizer=adam(lr=0.005), loss="mae")
return model
def cross_val(ind, train):
fold_size = int(len(ind) / 5)
for i in range(5):
model = create_model()
val_ind = ind[i * fold_size: (i+1) * fold_size]
train_ind = [x for x in ind if x not in val_ind]
X_train, Y_train = get_batch(train, train_ind)
X_val, Y_val = get_batch(train, val_ind)
for j in range(len(X_train)):
model.fit(X_train[j], Y_train[j], validation_data=(X_val[0], Y_val[0]))
cross_val(ind, train)
我的问题是,我可以删除for j in ...
循环吗?