[英]Error when checking target: expected activation_6 to have 3 dimensions, but got array with shape (70612, 1)
I Keep getting the error: Error when checking target: expected activation_6 to have 3 dimensions, but got array with shape (70612, 1).我不断收到错误:检查目标时出错:预期 activation_6 具有 3 个维度,但得到了形状为 (70612, 1) 的数组。 What could be the issue?
可能是什么问题?
see below code:见下面的代码:
# CNN Model
model = Sequential()
model.add(Embedding(256, 8))
model.add(Conv1D(32, 3, activation="relu"))
model.add(MaxPooling1D(pool_size = (2)))
model.add(Conv1D(32, 3, activation="relu"))
model.add(MaxPooling1D(pool_size =(2)))
model.add(Dense(64)
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy", optimizer ="adam", metrics = ["accuracy"])
**Model Summary:**
Model: "sequential_37"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_37 (Embedding) (None, None, 8) 2048
_________________________________________________________________
conv1d_47 (Conv1D) (None, None, 32) 800
_________________________________________________________________
max_pooling1d_43 (MaxPooling (None, None, 32) 0
_________________________________________________________________
conv1d_48 (Conv1D) (None, None, 32) 3104
_________________________________________________________________
max_pooling1d_44 (MaxPooling (None, None, 32) 0
_________________________________________________________________
dense_34 (Dense) (None, None, 64) 2112
_________________________________________________________________
dropout_18 (Dropout) (None, None, 64) 0
_________________________________________________________________
dense_35 (Dense) (None, None, 2) 130
_________________________________________________________________
activation_12 (Activation) (None, None, 2) 0
=================================================================
Your last layer has 3 dimension, it should have been 2 (batch, n_class)
.你的最后一层有 3 维,它应该是 2
(batch, n_class)
。
activation_12 (Activation) (None, None, 2) 0
You need to add a Flatten()
layer before您需要先添加一个
Flatten()
层
model.add(Dense(64)
Another issue is you have your labels with shape (batch, 1)
.另一个问题是您的标签带有形状
(batch, 1)
。
You need to use sigmoid
instead of softmax
(assuming binary classification).您需要使用
sigmoid
而不是softmax
(假设二进制分类)。
Also, change the shape of x and y.此外,更改 x 和 y 的形状。
x = x.reshape((-1, 533, 1))
y = y.reshape(-1,1)
x = x.reshape((-1, 533, 1))
y = y.reshape(-1,1)
model = Sequential()
model.add(Embedding(256, 8, input_length = 533))
model.add(Conv1D(32, 3, activation="relu"))
model.add(MaxPooling1D(pool_size = (2)))
model.add(Conv1D(32, 3, activation="relu"))
model.add(MaxPooling1D(pool_size =(2)))
model.add(Flatten())
model.add(Dense(64)
model.add(Dropout(0.5))
model.add(Dense(1)) # 1
model.add(Activation("sigmoid"))
model.compile(loss="binary_crossentropy", optimizer ="adam", metrics = ["accuracy"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.