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

查看:276
本文介绍了在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)

编译具有多个输出,将 extra 输出的损耗设置为None:

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

Compile with multiple outputs, set loss as None for extra outputs:

Give None for outputs that you don't want to use for loss calculation and optimization

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

训练时仅提供 target 输出.跳过 extra 输出:

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天全站免登陆