【问题标题】:Keras and input shape to Conv1D issuesKeras 和 Conv1D 问题的输入形状
【发布时间】: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


    【解决方案1】:

    什么是(训练)样本?

    (训练)数据可能包含数十、数百或数千个样本。例如,像 Cifar-10 或 ImageNet 这样的图像数据集中的每个图像都是一个样本。再举一个例子,对于一个时序数据集,该数据集由 10 年多天的天气统计数据组成,每个训练样本可能是每天的时间序列。如果我们在一天中记录了 100 次测量,并且每次测量都包含温度和湿度(即每次测量有两个特征),那么我们的数据集的形状大致为 (10x365, 100, 2)

    什么是批量大小?

    批量大小只是模型一次可以处理的样本数。我们可以使用 Keras 中fit 方法的batch_size 参数设置批量大小。常见的值为 16、32、64、128、256 等(尽管您必须选择一个数字,以便您的机器有足够的 RAM 来分配所需的资源)。

    此外,“步数”(也称为“序列长度”)和“通道”(也称为“特征大小”)分别是测量次数和每次测量的大小。例如,在上面的天气示例中,我们有 steps=100 和 channels=2。

    要解决代码问题,您需要定义训练数据(即X),使其形状为(num_samples, steps or time_len, channels or feat_size)

    n_samples = 1000   # we have 1000 samples in our training data
    n_channels = 1     # each measurement has one feature
    X = np.random.randn(n_samples, time_len, n_channels)
    
    # if you want to predict one value for each measurement
    Y = np.random.randn(n_samples, time_len)
    
    # or if you want to predict one value for each sample
    Y = np.random.randn(n_samples)
    

    编辑:

    还有一件事是您应该将一个样本的形状作为模型的输入形状传递。因此,Input层的输入形状必须像shape=X.shape[1:]一样传递。

    【讨论】:

    • 感谢您的帮助和解释!我按照您所说的方式重新调整了我的数据,但我仍然收到一个错误:ValueError:Conv1d_1 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:[None, 1000, 100, 1] 我不确定为什么要在我的网络中添加“无”维度。
    • @gamma701 您是否将batch_size 参数传递给fit 方法?
    • @gamma701 此外,您是否正确传递了输入层的输入形状?必须是shape=X.shape[1:];它不应该包括第一个维度,即样本数。
    • 我没有为 fit 传递 batch_size 参数。但我确实使用了shape=X.shape[1:],这似乎解决了我的问题。干杯!
    猜你喜欢
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    相关资源
    最近更新 更多