python如何自定义权重损失函数? 80C

我利用keras的神经网络训练一个模型,被训练的数据是一个很大的二维数组,每一行是一个类别,总共有3种类别。被训练出的模型中包括3种类别(暂且称为A,B,C)。现在B类的预测准确率太高了,而A和C类的预测准确率较低,我想在把B类准确率适当减低的情况下来提高A和C类的预测准确率。请问该怎么操作?
代码如下,我从网上查了一些代码,自己不是特别明白,尝试后,出现了错误。请问该如何修改?下面添加的图片中被划红线圈住的代码是添加上去的,最终运行出错了,请问怎么修改,或者重新帮我写一个权重损失代码代码,跪谢
def custom_loss_4(y_true, y_pred, weights):
return K.mean(K.abs(y_true - y_pred) * weights)
model = models.Sequential()
model.add(layers.Dense(200, activation = "relu", input_shape = (1175, )))
weights = np.random.randn(*X_train.shape)
weights_tensor = Input(shape=(3,))
cl4 = partial(custom_loss_4,weights=weights_tensor)
model.add(layers.Dropout(0.7))
model.add(layers.Dense(100, activation = "relu"))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(3, activation = "softmax"))
model.compile(optimizer = "rmsprop", loss = cl4, metrics = ["accuracy"])
model.summary()
图片说明
图片说明
图片说明
补充一下:我在前面对数据做过了不平衡调整,定义的函数如下:
def calc_class_weight(total_y):
my_class_weight = class_weight.compute_class_weight("balanced", np.unique(total_y), total_y)
return my_class_weight

1个回答

from keras.layers import Input,Embedding,LSTM,Dense,Lambda
from keras.layers.merge import dot
from keras.models import Model
from keras import backend as K

word_size = 128
nb_features = 10000
nb_classes = 10
encode_size = 64
margin = 0.1

embedding = Embedding(nb_features,word_size)
lstm_encoder = LSTM(encode_size)

def encode(input):
return lstm_encoder(embedding(input))

q_input = Input(shape=(None,))
a_right = Input(shape=(None,))
a_wrong = Input(shape=(None,))
q_encoded = encode(q_input)
a_right_encoded = encode(a_right)
a_wrong_encoded = encode(a_wrong)

q_encoded = Dense(encode_size)(q_encoded) #一般的做法是,直接讲问题和答案用同样的方法encode成向量后直接匹配,但我认为这是不合理的,我认为至少经过某个变换。

right_cos = dot([q_encoded,a_right_encoded], -1, normalize=True)
wrong_cos = dot([q_encoded,a_wrong_encoded], -1, normalize=True)

loss = Lambda(lambda x: K.relu(margin+x[0]-x[1]))([wrong_cos,right_cos])

model_train = Model(inputs=[q_input,a_right,a_wrong], outputs=loss)
model_q_encoder = Model(inputs=q_input, outputs=q_encoded)
model_a_encoder = Model(inputs=a_right, outputs=a_right_encoded)

model_train.compile(optimizer='adam', loss=lambda y_true,y_pred: y_pred)
model_q_encoder.compile(optimizer='adam', loss='mse')
model_a_encoder.compile(optimizer='adam', loss='mse')

model_train.fit([q,a1,a2], y, epochs=10)
#其中q,a1,a2分别是问题、正确答案、错误答案的batch,y是任意形状为(len(q),1)的矩阵

https://blog.csdn.net/TTdreamloong/article/details/80434331

weixin_45099845
weixin_45099845 这个你自己运行了吗?能否解决我的问题?
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐