如果这些错误很幼稚的话,我是深度学习,keras
API和卷积网络的新手。我正在尝试构建一个简单的卷积神经网络进行分类。输入数据X具有286
个样本,每个样本具有500
个4
维度的时间点。维度是分类变量的一种热编码。我不知道该为Y
做些什么,所以我只对样本进行了一些聚类,然后对它们进行了一次热编码,以便对数据进行实验以进行建模。 Y
目标数据包含286
个样本,其中包含6
个类别的热编码。我的最终目标只是让它运行,这样我就可以弄清楚如何根据实际有用的学习问题进行更改,并使用隐藏的图层进行特征提取。
我的问题是我无法在最后一层获得匹配的形状。
我制作的模型执行以下操作:
(1)输入数据
(2)卷积层
(3)Maxpooling layer
(4)辍学正规化
(5)大型全连通层
(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)
答案 0 :(得分:2)
(batch, observations, kernels)
的输出形状为3级张量> 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
但是,(batch, features)
图层需要2级张量Flatten
。将卷积与裂缝分开的GlobalAveragePooling1D
,GlobalMaxPooling1D
或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
(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)
....
个:{/ p>
{{1}}
您的tensorboard回调初始化似乎也存在问题。这个很容易解决。
对于时态数据处理,请查看TimeDistributed wrapper。