Training only one output of a network in Keras

前端 未结 2 1256
耶瑟儿~
耶瑟儿~ 2020-12-14 23:58

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

2条回答
  •  醉话见心
    2020-12-15 00:44

    In order to achieve this I ended up using the 'Functional API'. You basically create multiple models, using the same layers input and hidden layers but different output layers.

    For example:

    https://keras.io/getting-started/functional-api-guide/

    from keras.layers import Input, Dense
    from keras.models import Model
    
    # This returns a tensor
    inputs = Input(shape=(784,))
    
    # a layer instance is callable on a tensor, and returns a tensor
    x = Dense(64, activation='relu')(inputs)
    x = Dense(64, activation='relu')(x)
    predictions_A = Dense(1, activation='softmax')(x)
    predictions_B = Dense(1, activation='softmax')(x)
    
    # This creates a model that includes
    # the Input layer and three Dense layers
    modelA = Model(inputs=inputs, outputs=predictions_A)
    modelA.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    modelB = Model(inputs=inputs, outputs=predictions_B)
    modelB.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    

提交回复
热议问题