我使用 keras 构建一维卷积 + LSTM。我尝试设置 kernel size = 5 像这个 图像 1D Convoluton。我有所有 72 个值的数据,并分别用于测试集 6 值。它可以将内核设置为 1。如果我将内核设置为另一个大小,则会显示错误。这是我的data.csv文件。
这是我的代码。
import pandas as pd
import numpy as np
from keras.layers import LSTM
from keras.layers import Conv1D
from pandas.tseries.offsets import MonthEnd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
from keras.layers import Embedding
from keras.layers import GRU
df = pd.read_csv('D://data.csv',
engine='python')
df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
df = df.set_index('DATE_')
df.head()
split_date = pd.Timestamp('03-01-2015')
########## Separate train and test data ##########
train = df.loc[:split_date, ['COLUMN1']]
test = df.loc[split_date:, ['COLUMN1']]
sc = MinMaxScaler()
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
################### Convolution #######################
X_train_t = X_train[:, None]
X_test_t = X_test[:, None]
K.clear_session()
model = Sequential()
model.add(Conv1D(12, 5, activation='relu', input_shape=(None,1)))
model.add(LSTM(5,return_sequences=True))
model.add(LSTM(3))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam' )
model.fit(X_train_t, y_train, epochs=400, batch_size=10, verbose=1)
y_pred = model.predict(X_test_t)
print(y_pred)
print(y_test)
当我运行它时显示这样的错误。
InvalidArgumentError (see above for traceback): computed output size would be negative