keras-检查带有嵌入层的目标时出错

问题描述 投票:0回答:1

我正在尝试如下运行keras模型:

model = Sequential()
model.add(Dense(10, activation='relu',input_shape=(286,)))
model.add(Dense(1, activation='softmax',input_shape=(324827, 286)))

此代码有效,但是如果我要添加嵌入层:

model = Sequential()
model.add(Embedding(286,64, input_shape=(286,)))
model.add(Dense(10, activation='relu',input_shape=(286,)))
model.add(Dense(1, activation='softmax',input_shape=(324827, 286)))

我遇到以下错误:

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (324827, 1)

我的数据有286个要素和324827行。我可能对形状定义做错了,您能告诉我它是什么吗?谢谢

python tensorflow keras embedding
1个回答
0
投票

您不需要在第二个Dense层中提供input_shape:

from tensorflow.keras.layers import Embedding, Dense
from tensorflow.keras.models import Sequential

# 286 features and 324827 rows (324827, 286)

model = Sequential()
model.add(Embedding(286,64, input_shape=(286,)))
model.add(Dense(10, activation='relu',input_shape=(286,)))
model.add(Dense(1, activation='softmax'))
model.compile(loss='mse', optimizer='adam')
model.summary()

返回:

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_2 (Embedding)      (None, 286, 64)           18304     
_________________________________________________________________
dense_2 (Dense)              (None, 286, 10)           650       
_________________________________________________________________
dense_3 (Dense)              (None, 286, 1)            11        
=================================================================
Total params: 18,965
Trainable params: 18,965
Non-trainable params: 0
_________________________________________________________________

我希望这是您想要的

© www.soinside.com 2019 - 2024. All rights reserved.