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

我正在尝试如下运行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,)))
model.add(Dense(10,286)))

我遇到以下错误:

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

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

yzf710905 回答:keras-检查具有嵌入层的目标时出错

您不需要在第二个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'))
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,10)           650       
_________________________________________________________________
dense_3 (Dense)              (None,1)            11        
=================================================================
Total params: 18,965
Trainable params: 18,965
Non-trainable params: 0
_________________________________________________________________

我希望这就是您要寻找的

本文链接:https://www.f2er.com/2704158.html

大家都在问