简体   繁体   English

Keras-检查目标时出错:预期activation_5具有形状(2,),但数组的形状为(1,)

[英]Keras - Error when checking target: expected activation_5 to have shape (2,) but got array with shape (1,)

In an LSTM network I'm passing as feature an array of the form 在LSTM网络中,我将以下形式的数组作为特征传递

X
array([[1],[2],...,[12]],
      [[2],[3],...,[13]],...
      [[999],[1000],...,[1011]]
      [[1000],[1001],...,[1012]])

So it's shape is (1000, 12, 1) 所以它的形状是(1000,12,1)

And the target is an array with two possible values 0 and 1 of the form 目标是一个具有两个可能值0和1的数组

y 
array([[1], [0], [0], [1], ..., [0]])

So it's shape is (1000, 1) 所以它的形状是(1000,1)

What I'm I doing wrong taking into account that I have to use a softmax activation and a Dense 2? 考虑到必须使用softmax激活和Dense 2,我在做错什么?

Here's the construction of the network. 这是网络的构建。

model = Sequential()
model.add(LSTM(25, input_shape=(12, 1)))
model.add(Dropout(0.1))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(X, y, epochs=1000, batch_size=80, verbose=1, shuffle=False, callbacks=[EarlyStopping(patience=10)])

My guess is that it has to do with the shape of the target but I'm not sure how to fix it. 我的猜测是,它与目标的形状有关,但我不确定如何修复它。

Thanks! 谢谢!

Converting target to one hot encoding with two classes can solve this problem. 将目标转换为具有两个类的一种热编码可以解决此问题。 To convert y into one hot encoding do the following 要将y转换为一种热编码,请执行以下操作

y = numpy.eye(2)[y]

Edit: 编辑:

The other solution can be changing the output layer to contain only single node with sigmoid activation. 另一种解决方案是将输出层更改为仅包含具有S型激活的单个节点。 If the objective is to output values between [0, 1] sigmoid activation is best fit. 如果目标是输出[0,1]之间的值,则最适合S型激活。 I also recommend changing the loss function from 'mse' to 'binary_crossentropy', because assumption of 'mse' loss is the data is from normal distribution rather than binomial distribution. 我还建议将损失函数从“ mse”更改为“ binary_crossentropy”,因为“ mse”损失的假设是数据来自正态分布而不是二项分布。 In your case the output class distribution is binomial({0, 1}). 在您的情况下,输出类的分布为二项式({0,1})。 So using 'binary_crossentropy' is logical choice. 因此,使用“ binary_crossentropy”是合理的选择。

...
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss="binary_crossentropy", optimizer="rmsprop")
... 

I think Mitiku is right on the need to one hot encode your categorical Y. This can also be done with keras.utils.to_categorical . 我认为Mitiku正确地对您的分类Y进行热编码是正确的。这也可以通过keras.utils.to_categorical完成。

one_hot_y = keras.utils.to_categorical(y)

I'm also wondering whether your model should be... 我也想知道您的模型是否应该...

model = Sequential()
model.add(LSTM(25, input_shape=(12, 1)))
model.add(Dropout(0.1))
model.add(Dense(2, activation='softmax') # For a categorical output this has worked for me
model.compile(loss="binary_crossentropy", optimizer="rmsprop") # AFAIK 'mse' loss is not suitable for binary classification.
model.fit(X, y, epochs=1000, batch_size=80, verbose=1, shuffle=False, callbacks=[EarlyStopping(patience=10)])

I'm relatively new to Neural Networks myself so this may not fit with LSTM. 我本人对神经网络比较陌生,因此可能不适合LSTM。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查目标时出错:预期activation_5 的形状为(1,),但数组的形状为(100,) - ValueError: Error when checking target: expected activation_5 to have shape (1,) but got array with shape (100,) 检查目标时出错:预期 activation_5 的形状为 (1,) 但得到的数组的形状为 (2,) - Error when checking target: expected activation_5 to have shape (1,) but got array with shape (2,) 如何在检查目标时修复错误:预期 activation_5 有 2 个维度,但得到了形状为 (24943, 50, 50, 1) 的数组 - How to fix Error when checking target: expected activation_5 to have 2 dimensions, but got array with shape (24943, 50, 50, 1) Keras - “ValueError:检查目标时出错:预期activation_1具有形状(无,9)但得到的数组具有形状(9,1) - Keras - "ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1) keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100) ValueError:检查目标时出错:预期激活具有形状 (1,) 但得到形状为 (2,) 的数组 - ValueError: Error when checking target: expected activation to have shape (1,) but got array with shape (2,) ValuError:检查目标时出错:预期activation_6具有形状(70,)但得到形状为(71,)的数组 - ValuError: Error when checking target: expected activation_6 to have shape (70,) but got array with shape (71,) ValueError:检查目标时出错:预期 activation_9 具有形状 (74, 6) 但得到形状为 (75, 6) 的数组 - ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6) ValueError:检查目标时出错:预期activation_1的形状为(158,),但数组的形状为(121,) - ValueError: Error when checking target: expected activation_1 to have shape (158,) but got array with shape (121,) ValueError:检查目标时出错:预期 activation_6 具有形状(70,)但得到形状为(71,)的数组 - ValueError: Error when checking target: expected activation_6 to have shape (70,) but got array with shape (71,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM