我已经制作了一个KERAS LSTM模型,该模型在二进制目标值中读取,并且应该输出二进制预测。但是,预测不是二进制的。我的x和y值的样本如下:
X Y
5.06 0
4.09 1
4.72 0
4.57 0
4.44 1
6.98 1
我要预测的是XT 1是否将高于或低于XT。如果XT 1大于XT,则XT的Y值为1。我的训练X值是932个样本的形状(932、100、1),"回头"序列为100,特征为1。我看起来像:
Predictions
.512
.514
.513
我认为这些可能是概率,因为我的模型准确性约为51%。关于如何使它们成为二进制的想法?完整的型号代码如下:
# Defining network architecture
def build_model(layers):
model = Sequential()
model.add(LSTM(
input_dim=layers[0],
output_dim=layers[1],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
layers[2],
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[3]))
model.add(Activation("sigmoid"))
start = time.time()
model.compile(loss="binary_crossentropy", optimizer="rmsprop",metrics=['accuracy'],class_mode="binary")
print("> Compilation Time : ", time.time() - start)
return model
# Compiling model
model = build_model([1, 100, 500, 1])
这是正常行为。
神经网络中没有"二进制",但是在范围内连续函数。
仅使用连续功能,模型才能使用"随机梯度下降"来训练和学习。
为了实现二进制结果,我们使用了从0到1的Sigmoid函数,但是最初,您的模型未经训练,其所有"权重"都是随机的初始化。结果确实是结果趋于平均值,在Sigmoid函数中为0.5。
您只需要用足够的数据来训练模型以进行足够的时代,因此结果将逐渐接近(但永远不会达到)0或1(或您在培训数据中拥有的任何目标" y")
我遇到了相同的问题,并解决为:
seq_predictions=model.predict(test_sequences)
print('Outputs shape')
print(seq_predictions.shape) # prints (n,1) but need (n,)
seq_predictions=np.transpose(seq_predictions)[0] # transformation to get (n,)
print(seq_predictions.shape) # now the shape is (n,)
# Applying transformation to get binary values predictions with 0.5 as thresold
seq_predictions = list(map(lambda x: 0 if x<0.5 else 1, seq_predictions))