Keras和Conv1D问题的输入形状

首先,我是神经网络和Keras的新手。


我正在尝试使用Keras创建一个简单的神经网络,其中输入是一个时间序列,输出是另一个相同长度的时间序列(一维矢量)。


我制作了伪代码,以使用Conv1D层创建随机的输入和输出时间序列。然后,Conv1D层输出6个不同的时间序列(因为我有6个滤波器),并且我定义的下一层将这些输出的全部6个相加到一个,即整个网络的输出。


import numpy as np

import tensorflow as tf

from tensorflow.python.keras.models import Model

from tensorflow.python.keras.layers import Conv1D, Input, Lambda



def summation(x):

    y = tf.reduce_sum(x, 0)

    return y



time_len = 100  # total length of time series

num_filters = 6 # number of filters/outputs to Conv1D layer

kernel_len = 10 # length of kernel (memory size of convolution)


# create random input and output time series

X = np.random.randn(time_len)

Y = np.random.randn(time_len)


# Create neural network architecture

input_layer = Input(shape = X.shape)

conv_layer = Conv1D(filters = num_filters, kernel_size = kernel_len, padding = 'same')(input_layer)

summation_layer = Lambda(summation)(conv_layer)


model = Model(inputs = input_layer, outputs = summation_layer)


model.compile(loss = 'mse', optimizer = 'adam', metrics = ['mae'])


model.fit(X,Y,epochs = 1, metrics = ['mae'])

我得到的错误是:


ValueError: Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 100]

查看有关Conv1D的Keras文档,输入形状应该是3D张量的形状(批,阶,通道),如果我们使用一维数据,我将无法理解。


您能否解释每一项的含义:批次,步骤和渠道?我应该如何调整我的一维向量以允许网络运行?


喵喵时光机
浏览 305回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python