[英]How to format input and output shapes for convolutional (1D) keras neural network? (Python)
I am new to deep learning, the keras
API, and convolutional networks so apologies before-hand if these mistakes are naive. 我是新手,深度学习, keras
API和卷积网络,如果这些错误是天真的,那么我们前面道歉。 I am trying to build a simple convolutional neural network for classification. 我正在尝试构建一个简单的卷积神经网络进行分类。 The input data X has 286
samples each with 500
timepoints of 4
dimensions. 输入数据X具有286
样本,每个样本具有4
维度的500
时间点。 The dimensions are one-hot-encodings of categorical variables. 维度是分类变量的一种热编码。 I wasn't sure what to do for Y
so I just did some clustering of the samples and the one-hot-encoded them to have data to experiment with for the modeling. 我不知道该怎么做Y
所以我只是对样本进行了一些聚类,然后对它们进行了一次热编码,以便有数据来进行建模。 The Y
target data has 286
samples with one-hot-encodings for 6
categories. Y
目标数据具有286
样本,具有针对6
类别的单热编码。 My ultimate goal is just to get it to run so I can figure out how to change it for actually useful learning problems and use the hidden layers for feature extraction. 我的最终目标只是让它运行,这样我就可以弄清楚如何根据实际有用的学习问题进行更改,并使用隐藏的图层进行特征提取。
My problem is that I can't get the shapes to match up in the final layer. 我的问题是我无法在最后一层获得匹配的形状。
The model I made does the following: 我制作的模型执行以下操作:
(1) Inputs the data (1)输入数据
(2) Convolutional layer (2)卷积层
(3) Maxpooling layer (3)最大化层
(4) Dropout regularization (4)辍学正规化
(5) Large fully connected layer (5)大型全连接层
(6) Output layer (6)输出层
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
's output's shape is a 3-rank tensor (batch, observations, kernels)
: 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)
However, Dense
layers expects a 2-rank tensor (batch, features)
. 但是, Dense
图层需要2级张量(batch, features)
。 A Flatten
, GlobalAveragePooling1D
or GlobalMaxPooling1D
separating the convolutions from the denses is sufficient to fix this: Flatten
, GlobalAveragePooling1D
或GlobalMaxPooling1D
将卷积与GlobalAveragePooling1D
分开,足以解决这个问题:
Flatten
will reshape a (batch, observations, kernels)
tensor into a (batch, observations * kernels)
one: 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) ....
GlobalAveragePooling1D
will average all observations in (batch, observations, kernels)
tensor, resulting in a (batch, kernels)
one: 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) ....
There seems to be a problem with your tensorboard callback initialization also. 您的tensorboard回调初始化似乎也存在问题。 This one is easy to fix. 这个很容易修复。
For temporal data processing, take a look at the TimeDistributed wrapper . 对于时态数据处理,请查看TimeDistributed包装器 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.