在Keras中以自定义指标定义采样权重

时间:2019-05-09 01:34:46

标签: python tensorflow keras

我想通过实现tensorflow roc_auc来使用sklearn py_func函数,并尝试合并采样权重。我的目标数组有两列:第一列是标签,第二列是权重。这是我的代码:

def custom_loss(y_true, y_pred):
    return binary_crossentropy(K.reshape(y_true[:,0],(-1,1)), y_pred) * y_true[:,1]

def auroc(y_true, y_pred):
    w = K.get_session().run(y_true[:, 1])
    return tf.py_func(partial(roc_auc_score, sample_weight=w), (y_true[:,0], y_pred), tf.double)

def build_model(embedding_matrix):

    words = Input(shape=(220,))
    x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False)(words)
    x = SpatialDropout1D(0.3)(x)
    x = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)
    x = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)

    hidden = concatenate([GlobalMaxPooling1D()(x), GlobalAveragePooling1D()(x), ])
    hidden = add([hidden, Dense(512, activation='relu')(hidden)])
    hidden = add([hidden, Dense(512, activation='relu')(hidden)])
    result = Dense(1, activation='sigmoid')(hidden)

    model = Model(inputs=words, outputs=result)
    model.compile(loss=custom_loss, 
                  metrics=[auroc],
                  optimizer='adam')

    return model
def run_model(X_train, y_train, embedding_matrix):
    checkpoint_predictions = []
    weights = []
    for model_idx in range(NUM_MODELS):
        model = build_model(embedding_matrix)
        for global_epoch in range(NUM_EPOCHS):
            model.fit(
                X_train, y_train,
                batch_size=BATCH_SIZE, epochs=1, verbose=1,
                validation_data=(X_val, y_val),
                callbacks=[LearningRateScheduler(lambda epoch: 1e-3 * (0.6 ** global_epoch))]
            )
            checkpoint_predictions.append(model.predict(X_test, batch_size=2048).flatten())
            weights.append(2 ** global_epoch)
        del model
        gc.collect()

    preds = np.average(checkpoint_predictions, weights=weights, axis=0)
    return preds

我在编译时使用metrics=[auroc]对此进行了调用,并在fit命令中使用了validation_data选项以在每个时期结束时进行测试。但是,我不断收到错误消息:

InvalidArgumentError: You must feed a value for placeholder tensor 'dense_12_target' with dtype float and shape [?,?]
     [[{{node dense_12_target}}]]

0 个答案:

没有答案