预测和拟合之间的角膜形状不匹配

时间:2019-12-04 20:35:53

标签: python tensorflow keras

我有一个简单的模型,将序列嵌入变平,然后求和。当我进行预测时,没有任何错误,也没有期望的输出形状,但是当我尝试训练时,却出现了形状不匹配错误

这是模型:

import numpy as np
from keras import backend as K
from keras.models import Model
from keras.layers.embeddings import Embedding
from keras.layers import Reshape, Lambda

inputs = Input(shape=(20,), name="inputs")
embedding = Embedding(69, 100, name="embeddings")(inputs)
out = Reshape((2000,), name='reshape_embeddings')(embedding)
out = Lambda(lambda x: K.sum(x, axis=1), name='sum_embeddings')(out)
model = Model(inputs, out)
model.compile('adam', 'mean_squared_error')
print(model.summary())

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inputs (InputLayer)          (None, 20)                0         
_________________________________________________________________
embeddings (Embedding)       (None, 20, 100)           6900      
_________________________________________________________________
reshape_embeddings (Reshape) (None, 2000)              0         
_________________________________________________________________
sum_embeddings (Lambda)      (None,)                   0         
=================================================================
Total params: 6,900
Trainable params: 6,900
Non-trainable params: 0
_________________________________________________________________

在这里,我建立一个随机的x,y样本:

x = np.random.randint(69, size=(500,20))
y = np.random.uniform(0, 1, size=(500,))

当我预测x时,我会得到正确的输出形状

preds = model.predict(x)
print(preds.shape == y.shape)

当我拟合模型时,出现以下错误:

model.fit(x, y, batch_size=50, verbose=1)
  

ValueError:检查目标时出错:预期的sum_embeddings   有1个维度,但数组的形状为(500,1)

感觉好像我缺少一些非常简单的东西。任何建议将不胜感激

1 个答案:

答案 0 :(得分:1)

是的,这是您的代码中的几个简单问题。模型的输出至少必须具有2级(在这种情况下为(None,1))(我的2美分是优化器抱怨的不是2分)。这是使用keepdims=True完成的。然后,您也必须向y添加一个维度。

inputs = Input(shape=(20,), name="inputs")
embedding = Embedding(69, 100, name="embeddings")(inputs)
out = Reshape((2000,), name='reshape_embeddings')(embedding)
out = Lambda(lambda x: K.sum(x, axis=1, keepdims=True), name='sum_embeddings')(out)
model = Model(inputs, out)
model.compile('adam', 'mean_squared_error')
print(model.summary())

x = np.random.randint(69, size=(500,20))
y = np.random.uniform(0, 1, size=(500,1))

preds = model.predict(x)
print(preds.shape == y.shape)

model.fit(x, y, batch_size=50, verbose=1)