【发布时间】:2018-08-14 20:31:30
【问题描述】:
首先,我对神经网络和 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 张量,如果我们使用一维数据,我不明白。
您能解释一下每一项的含义:批次、步骤和通道吗?我应该如何塑造我的一维向量以允许我的网络运行?
【问题讨论】:
标签: python tensorflow keras time-series conv-neural-network