如何格式化卷积(1D)keras神经网络的输入和输出形状?Python

2024-05-19 16:24:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我对深度学习、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)

Tags: thenonelayer编码modellayerstfwith
1条回答
网友
1楼 · 发布于 2024-05-19 16:24:27

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)。一个FlattenGlobalAveragePooling1D或{}将卷积与密集区分开就足以解决这个问题:

  1. Flatten将把(batch, observations, kernels)张量重塑为(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)
    ....
    

您的tensorboard回调初始化似乎也有问题。这个很容易修好。在


对于时态数据处理,请看一下TimeDistributed wrapper。在

相关问题 更多 >