[英]why I can't set kernel size in 1d convolution?
I use keras build 1D Convolution + LSTM. 我使用keras构建1D卷积+ LSTM。 I try to set kernel size = 5 like this image 1D Convoluton . 我尝试将内核大小= 5设置为该图像1D Convoluton 。 I have data all 72 value and separate to test set 6 value. 我有所有72值的数据,并分别测试集6值。 It can set kernel to 1. If I set kernel to another size it show error. 它可以将内核设置为1。如果将内核设置为其他大小,则显示错误。 This my data.csv file. 这是我的data.csv文件。
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
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. 表示输入的批次大小未知,并且在一个批次中,它的长度为1,正好是一个数字。 And you're trying to fit a 5-kernel convolution over it. 您正在尝试在其上进行5核卷积。 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. 如果input_shape = ( None, 5 )
,则只有一个数字(每个过滤器),如果您只有1个数字,其理论上的大小为-4,但没有实际意义,则将得到一个数字(每个过滤器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.