Machine learning 我如何添加我的神经网络可以预测的每个分类的具体数字?

Machine learning 我如何添加我的神经网络可以预测的每个分类的具体数字?,machine-learning,keras,classification,lstm,multilabel-classification,Machine Learning,Keras,Classification,Lstm,Multilabel Classification,我有一个np数组,类似于0-1的热编码。对于每个样本,我总是有15个0和5个1。我该怎么做才能让它只预测5个1和15个0?我使用的是keras库,有没有一个设置可以让我的模型精确预测15个零和5个一 - 输入示例=[0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0] #Building RNN from keras.models import Sequential from keras.layers import Dense from keras.lay

我有一个np数组,类似于0-1的热编码。对于每个样本,我总是有15个0和5个1。我该怎么做才能让它只预测5个1和15个0?我使用的是keras库,有没有一个设置可以让我的模型精确预测15个零和5个一

-

输入示例=[0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0]

#Building RNN
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

regressor = Sequential()

regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1],20)))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 50, return_sequences = True))

regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

regressor.add(Dense(units = 20, activation='sigmoid'))

# Compiling RNN
regressor.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

我希望我的模型总是预测15个0和5个1。

您也可以尝试定义自定义损失函数,该函数根据您的格式返回

def自定义丢失(y_真,y_pred):
#计算二进制交叉熵,并根据需要对结果进行重塑
...
返回K.variable(…)
regressor.compile(loss=custom_loss,optimizer='adam',metrics=['accurity'])

您可以通过严格的培训来做到这一点。严重惩罚任何有任何其他分发的内容。对预测值与精确的5
1
s之间的差异进行更重的惩罚

您可以通过编写适当的损失函数来完成大部分或全部操作