在 Keras 中仅训练网络的一个输出 [英] Training only one output of a network in Keras

查看:29
本文介绍了在 Keras 中仅训练网络的一个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Keras 中有一个具有许多输出的网络,但是,我的训练数据一次只提供一个输出的信息.

I have a network in Keras with many outputs, however, my training data only provides information for a single output at a time.

目前我的训练方法是对有问题的输入进行预测,更改我正在训练的特定输出的值,然后进行单批更新.如果我是对的,这与将所有输出的损失设置为零相同,除了我要训练的输出.

At the moment my method for training has been to run a prediction on the input in question, change the value of the particular output that I am training and then doing a single batch update. If I'm right this is the same as setting the loss for all outputs to zero except the one that I'm trying to train.

有没有更好的办法?我已经尝试过班级权重,我将所有权重设置为零,但我正在训练的输出除外,但它没有给我预期的结果?

Is there a better way? I've tried class weights where I set a zero weight for all but the output I'm training but it doesn't give me the results I expect?

我正在使用 Theano 后端.

I'm using the Theano backend.

推荐答案

输出多个结果,只优化其中一个

假设您想从多个层返回输出,可能来自一些中间层,但您只需要优化一个目标输出.您可以这样做:

Outputting multiple results and optimizing only one of them

Let's say you want to return output from multiple layers, maybe from some intermediate layers, but you need to optimize only one target output. Here's how you can do it:

inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)

# you want to extract these values
useful_info = Dense(32, activation='relu', name='useful_info')(x)

# final output. used for loss calculation and optimization
result = Dense(1, activation='softmax', name='result')(useful_info)

编译多个输出,将额外输出的损失设置为None:

为您不想用于损失计算和优化的输出提供 None

model = Model(inputs=inputs, outputs=[result, useful_info])
model.compile(optimizer='rmsprop',
              loss=['categorical_crossentropy', None],
              metrics=['accuracy'])

在训练时仅提供目标输出.跳过额外输出:

Provide only target outputs when training. Skipping extra outputs:

model.fit(my_inputs, {'result': train_labels}, epochs=.., batch_size=...)

# this also works:
#model.fit(my_inputs, [train_labels], epochs=.., batch_size=...)

一个预测就能得到所有

拥有一个模型,您只需运行一次 predict 即可获得所需的所有输出:

One predict to get them all

Having one model you can run predict only once to get all outputs you need:

predicted_labels, useful_info = model.predict(new_x)

这篇关于在 Keras 中仅训练网络的一个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆