简体   繁体   English

有没有办法使用多标签分类但当 model 预测 keras 中只有一个 label 时认为是正确的?

[英]Is there a way to use multilabel classification but take as correct when the model predicts only one label in keras?

I have a dataset of weather forecasts and am trying to make a model that predicts which forecast will be more accurate the next day.我有一个天气预报数据集,正在尝试制作一个 model 来预测第二天哪个预测会更准确。

In order to do so, my y output is of the form y=[1,0,1,0] because I have the forecasts of 4 different organizations.为此,我的 y output 的形式为 y=[1,0,1,0],因为我有 4 个不同组织的预测。 1 represents that this is the best forecast for the current record and more 'ones' means that multiple forecasts had the same best prediction. 1 表示这是当前记录的最佳预测,更多的“1”表示多个预测具有相同的最佳预测。

My problem is that I want to create a model that trains on these data but also learns that only predicting 1 value correctly is 100% correct answer as I only need to get as a result one of the best and equal forecasts.我的问题是我想创建一个 model 来训练这些数据,但也知道只有正确预测 1 个值是 100% 正确的答案,因为我只需要因此得到最好和平等的预测之一。 I believe that the way I am doing this 'shaves' accuracy from my evaluation.我相信我这样做的方式会从我的评估中“减少”准确性。 Is there a way to implement this in keras?有没有办法在 keras 中实现这个? The architecture of the neural network is totally experimental and there is no specific reason why I chose it.神经网络的架构完全是实验性的,我选择它没有具体原因。 This is the code I wrote.这是我写的代码。 My train dataset consists of 6463 rows × 505 columns.我的火车数据集由 6463 行 × 505 列组成。

model = Sequential()

model.add(LSTM(150, activation='relu',activity_regularizer=regularizers.l2(l=0.0001)))
model.add(Dense(100,  activation='relu'))
model.add(Dense(100,  activation='relu'))
model.add(Dense(100,  activation='relu'))
model.add(Dense(50,  activation='relu'))
model.add(Dense(50,  activation='relu'))
model.add(Dense(50,  activation='relu'))

model.add(Dense(24,  activation='relu'))
model.add(Dense(4, activation='softmax')) 


#LSTM
# reshape input to be 3D [samples, timesteps, features]
X_train_sc =X_train_sc.reshape((X_train_sc.shape[0], 1, X_train_sc.shape[1]))
X_test_sc = X_test_sc.reshape((X_test_sc.shape[0], 1,X_test_sc.shape[1]))
#validation set
x_val=X_train.iloc[-2000:-1300,0:505]
y_val=y_train[-2000:-1300]

x_val_sc=scaler.transform(x_val)

# reshape input to be 3D for LSTM[samples, timesteps, features]
x_val_sc =x_val_sc.reshape((x_val_sc.shape[0], 1, x_val_sc.shape[1]))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
history= model.fit(x=X_train_sc, y=y_train ,validation_data=(x_val_sc,y_val), epochs=300, batch_size=24)
print(model.evaluate(X_test_sc,y_test))
yhat= model.predict(X_test_sc)

My accuracy is ~44%我的准确率约为 44%

If you want to make prediction of form [1,0,1,0] ie.如果你想预测形式[1,0,1,0]即。 the model should predict the probabiliyt of belong to each of the 4 classes then it is called multi-label classification. model 应该预测属于 4 个类别中的每个类别的概率,则称为多标签分类。 What you have coded for is multi-class classification.您编码的是多类分类。

Muti-label classification多标签分类

Your last layer will be a dense layers of size 4 for each class, with sigmod activation.您的最后一层将是每个 class 大小为 4 的密集层,并激活 sigmod。 You will use a binary_crossentropy loss.您将使用binary_crossentropy损失。

x = np.random.randn(100,10,1)
y = np.random.randint(0,2,(100,4))

model = keras.models.Sequential()

model.add(keras.layers.LSTM(16, activation='relu', input_shape=(10,1), return_sequences=False))
model.add(keras.layers.Dense(8,  activation='relu'))
model.add(keras.layers.Dense(4, activation='sigmoid')) 

model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(x,y)

Check查看

print (model.predict(x))

Output Output

array([[0.5196002 , 0.52978194, 0.5009601 , 0.5036485 ],
       [0.508756  , 0.5189857 , 0.5022978 , 0.50169533],
       [0.5213044 , 0.5254892 , 0.51159555, 0.49724004],
       [0.5144601 , 0.5264933 , 0.505496  , 0.5008205 ],
       [0.50524575, 0.5147699 , 0.50287664, 0.5021702 ],
       [0.521035  , 0.53326863, 0.49642274, 0.50102305],
.........

As you can see the probabilities for each prediction do not sum up to one, rather each value is a probability of it belonging to the corresponding class.如您所见,每个预测的概率总和不等于 1,而是每个值是它属于相应 class 的概率。 So if the probability > 0.5 you can say that it belong to the class.所以如果概率 > 0.5 你可以说它属于 class。

On the other hand if you use softmax , the probabilies sum up to 1 ie.另一方面,如果您使用softmax ,则概率总和为 1,即。 it belongs to the single class for which it has value > 0.5.它属于单个 class,其值 > 0.5。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM