Reputation: 1049
I use keras build 1D Convolution + LSTM. I try to set kernel size = 5 like this image 1D Convoluton. I have data all 72 value and separate to test set 6 value. It can set kernel to 1. If I set kernel to another size it show error. This my data.csv file.
This is my code.
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)
When I run it show error like this.
InvalidArgumentError (see above for traceback): computed output size would be negative
Upvotes: 0
Views: 342
Reputation: 4868
This line:
model.add(Conv1D(12, 5, activation='relu', input_shape=(None,1)))
says that the input is of unknown batch size, and within a batch, it's 1 long, that is exactly one number. And you're trying to fit a 5-kernel convolution over it. If you use padding "same" this would just yield an output of one number (the input number multiplied by the middle number of your kernel), but with the default "valid" padding, this would make the output size negative.
With input_shape = ( None, 5 )
you would get exactly one number (per filter) as output, if you have only 1 number that would make the size -4 theoretically but that has no practical meaning.
Upvotes: 2