gpt4 book ai didi

python - 如何格式化卷积(一维)keras 神经网络的输入和输出形状? (Python)

转载 作者:行者123 更新时间:2023-11-28 17:11:01 27 4
gpt4 key购买 nike

我是深度学习、keras API 和卷积网络的新手,如果这些错误是幼稚的,请事先致歉。我正在尝试构建一个用于分类的简单卷积神经网络。输入数据 X 有 286 个样本,每个样本有 500 个时间点,4 维度。维度是分类变量的单热编码。我不确定要为 Y 做什么,所以我只是对样本进行了一些聚类,并对它们进行了单热编码,以获得用于建模实验的数据。 Y 目标数据有 286 个样本,对 6 类别进行单热编码。我的最终目标只是让它运行,这样我就可以弄清楚如何针对实际有用的学习问题更改它,并使用隐藏层进行特征提取。

我的问题是我无法在最后一层中匹配形状。

The model I made does the following:

(1) Inputs the data

(2) Convolutional layer

(3) Maxpooling layer

(4) Dropout regularization

(5) Large fully connected layer

(6) Output layer

import tensorflow as tf
import numpy as np
# Data Description
print(X[0,:])
# [[0 0 1 0]
# [0 0 1 0]
# [0 1 0 0]
# ...,
# [0 0 1 0]
# [0 0 1 0]
# [0 0 1 0]]
print(Y[0,:])
# [0 0 0 0 0 1]
X.shape, Y.shape
# ((286, 500, 4), (286, 6))

# Tensorboard callback
tensorboard= tf.keras.callbacks.TensorBoard()

# Build the model
# Input Layer taking in 500 time points with 4 dimensions
input_layer = tf.keras.layers.Input(shape=(500,4), name="sequence")
# 1 Dimensional Convolutional layer with 320 filters and a kernel size of 26
conv_layer = tf.keras.layers.Conv1D(320, 26, strides=1, activation="relu", )(input_layer)
# Maxpooling layer
maxpool_layer = tf.keras.layers.MaxPooling1D(pool_size=13, strides=13)(conv_layer)
# Dropout regularization
drop_layer = tf.keras.layers.Dropout(0.3)(maxpool_layer)
# Fully connected layer
dense_layer = tf.keras.layers.Dense(512, activation='relu')(drop_layer)
# Softmax activation to get probabilities for output layer
activation_layer = tf.keras.layers.Activation("softmax")(dense_layer)
# Output layer with probabilities
output = tf.keras.layers.Dense(num_classes)(activation_layer)
# Build model
model = tf.keras.models.Model(inputs=input_layer, outputs=output, name="conv_model")
model.compile(loss="categorical_crossentropy", optimizer="adam", callbacks=[tensorboard])
model.summary()
# _________________________________________________________________
# Layer (type) Output Shape Param #
# =================================================================
# sequence (InputLayer) (None, 500, 4) 0
# _________________________________________________________________
# conv1d_9 (Conv1D) (None, 475, 320) 33600
# _________________________________________________________________
# max_pooling1d_9 (MaxPooling1 (None, 36, 320) 0
# _________________________________________________________________
# dropout_9 (Dropout) (None, 36, 320) 0
# _________________________________________________________________
# dense_16 (Dense) (None, 36, 512) 164352
# _________________________________________________________________
# activation_7 (Activation) (None, 36, 512) 0
# _________________________________________________________________
# dense_17 (Dense) (None, 36, 6) 3078
# =================================================================
# Total params: 201,030
# Trainable params: 201,030
# Non-trainable params: 0
model.fit(X,Y, batch_size=128, epochs=100)
# ValueError: Error when checking target: expected dense_17 to have shape (None, 36, 6) but got array with shape (286, 6, 1)

最佳答案

Conv1D 的输出形状是一个 3 阶张量(batch, observations, kernels):

> x = Input(shape=(500, 4))
> y = Conv1D(320, 26, strides=1, activation="relu")(x)
> y = MaxPooling1D(pool_size=13, strides=13)(y)
> print(K.int_shape(y))
(None, 36, 320)

但是,Dense 层需要 2 阶张量 (batch, features)FlattenGlobalAveragePooling1DGlobalMaxPooling1D 将卷积与密集区分开就足以解决此问题:

  1. Flatten 会将 (batch, observations, kernels) 张量 reshape 为 (batch, observations * kernels) 张量:

    ....
    y = Conv1D(320, 26, strides=1, activation="relu")(x)
    y = MaxPooling1D(pool_size=13, strides=13)(y)
    y = Flatten()(y)
    y = Dropout(0.3)(y)
    y = Dense(512, activation='relu')(y)
    ....
  2. GlobalAveragePooling1D 将对(batch, observations, kernels) 张量中的所有观察值进行平均,从而生成一个(batch, kernels)一:

    ....
    y = Conv1D(320, 26, strides=1, activation="relu")(x)
    y = GlobalAveragePooling1D(pool_size=13, strides=13)(y)
    y = Flatten()(y)
    y = Dropout(0.3)(y)
    y = Dense(512, activation='relu')(y)
    ....

您的张量板回调初始化似乎也有问题。这个很容易修复。


对于时态数据处理,请查看 TimeDistributed wrapper .

关于python - 如何格式化卷积(一维)keras 神经网络的输入和输出形状? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47445732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com