Keras Model производит нан на заказ - PullRequest
0 голосов
/ 20 июня 2020

У меня непонятная проблема. Прежде чем я вызову .fit(), моя модель может выдавать выходные данные из входов (см. Ниже).


>>> print(f0.predict(x))
>>> print(f1.predict([x, x]))

[[16.913166 ]
 [ 6.2363415]
 [10.625664 ]
 ...
 [13.86934  ]
 [15.4675865]
 [16.353233 ]]
[[-0.94484174]
 [-1.0965291 ]
 [-0.829823  ]
 ...
 [-1.0362396 ]
 [-0.18886864]
 [-0.3576672 ]]

Но после вызова fit все, что я получаю, это нанс

>>> print(f0.predict(x))
>>> print(f1.predict([x, x]))

[[16.913166 ]
 [ 6.2363415]
 [10.625664 ]
 ...
 [13.86934  ]
 [15.4675865]
 [16.353233 ]]
[[nan]
 [nan]
 [nan]
 ...
 [nan]
 [nan]
 [nan]]

Вот код для создание модели. Ошибка не зависит от данных, поскольку я могу воспроизвести это с любым набором данных sklearn в качестве входных данных.

def create_base_model(p, units=20):
    # Specs
    input_layer = k.layers.Input((p,))

    penultimate_layer = k.layers.Dense(units, activation='elu')(input_layer)
    penultimate_layer = k.layers.Dense(p)(penultimate_layer)

    training_output = k.layers.Dense(1)(penultimate_layer)

    # Models
    boost_model = k.Model(inputs=input_layer, outputs=penultimate_layer)
    training_model = k.Model(inputs=input_layer, outputs=training_output)

    # Compile and export
    training_model.compile(optimizer='sgd', loss='mse')
    return training_model, boost_model


def create_staged_model(p: int, model: k.Model, units=20):
    # Freeze prior model
    for layer_i in model.layers:
        layer_i.trainable = False

    # Specs
    input_layer = k.layers.Input((p,))

    penultimate_layer = k.layers.concatenate([model.output, input_layer], axis=-1)
    penultimate_layer = k.layers.Dense(units, activation='elu')(penultimate_layer)
    penultimate_layer = k.layers.Dense(p)(penultimate_layer)

    training_output = k.layers.Dense(1)(penultimate_layer)

    # Models
    boost_model = k.Model(inputs=[model.input, input_layer], outputs=penultimate_layer)
    training_model = k.Model(inputs=[model.input, input_layer], outputs=training_output)

    # Compile and export
    training_model.compile(optimizer='sgd', loss='mse')
    return training_model, boost_model


fit_kwargs = dict(
    epochs=50, 
    validation_split=0.1, 
    callbacks=[
        k.callbacks.ReduceLROnPlateau(factor=.5, patience=5, min_lr=1e-6),
        k.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
    ]
)


f0, _ = create_base_model(p)
f0.fit(x, y, **fit_kwargs)  # Works fine

f1, _ = create_staged_model(p, model0)
f1.fit([x, x], y, **fit_kwargs)  # Breaks on fit

Обратите внимание, что эта проблема с nan возникает немедленно

Epoch 1/50
282/282 [==============================] - 0s 2ms/step - loss: nan - val_loss: nan - lr: 0.0100
Epoch 2/50
282/282 [==============================] - 0s 2ms/step - loss: nan - val_loss: nan - lr: 0.0100
Epoch 3/50
282/282 [==============================] - 0s 1ms/step - loss: nan - val_loss: nan - lr: 0.0100
Epoch 4/50
282/282 [==============================] - 0s 2ms/step - loss: nan - val_loss: nan - lr: 0.0100
Epoch 5/50
282/282 [==============================] - 0s 2ms/step - loss: nan - val_loss: nan - lr: 0.0100
Epoch 6/50
282/282 [==============================] - 0s 1ms/step - loss: nan - val_loss: nan - lr: 0.0050
Epoch 7/50
282/282 [==============================] - 0s 1ms/step - loss: nan - val_loss: nan - lr: 0.0050
Epoch 8/50
282/282 [==============================] - 0s 2ms/step - loss: nan - val_loss: nan - lr: 0.0050
Epoch 9/50
282/282 [==============================] - 0s 1ms/step - loss: nan - val_loss: nan - lr: 0.0050
...