简体   繁体   English

为什么不能在一维卷积中设置内核大小?

[英]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.

相关问题 Keras 中的 1D CNN:如果过滤器的数量和 kernel_size 太低,它会在序列中间停止卷积吗? - 1D CNN in Keras: if the number of filters and kernel_size are too low, will it stop convolution at the middle of a sequence? Keras 1d 卷积层如何处理词嵌入 - 文本分类问题? (过滤器、内核大小和所有超参数) - How does Keras 1d convolution layer work with word embeddings - text classification problem? (Filters, kernel size, and all hyperparameter) 当过滤器大小为1时,在TensorFlow中更有效的1d卷积 - More efficient 1d convolution in TensorFlow when filter size is 1 我们可以使用一维卷积进行图像分类吗? - Can we use 1D convolution for image classification? 张量流中的基本一维卷积 - Basic 1d convolution in tensorflow tensorflow conv1d,在这段代码中为什么我不能使用 2 或 3 的 kernel_size - tensorflow conv1d, in this code why I can not use a kernel_size of 2 or 3 在 TensorFlow 中将 2D 卷积转换为 1D 卷积 + 仿射变换? - Convert a 2D Convolution into a 1D Convolution + Affine Transformation in TensorFlow? 为什么TensorFlow在调用1D卷积时计算2D卷积? - Why does TensorFlow calculate 2D convolutions when 1D convolution is called? 1D卷积网络提供恒定输出 - 1D convolution network giving constant output keras 1D卷积输入形状 - keras 1D convolution input shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM