首页 文章

Keras和Conv1D问题的输入形状

提问于
浏览
0

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

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

我使用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张量的形状(批量,步骤,通道),如果我们使用1维数据,我不明白 .

您能解释每个项目的含义:批次,步骤和渠道吗?我应该如何塑造我的1D向量以允许我的网络运行?

1 回答

  • 0

    什么是(训练)样本?

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

    什么是批量大小?

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

    此外,“步骤”(也称为“序列长度”)和“通道”(也称为“特征尺寸”)分别是测量的数量和每个测量的大小 . 例如,在我们上面的天气示例中,我们有步数= 100和渠道= 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)
    

    Edit:

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

相关问题