Keras 自定义指标迭代 [英] Keras custom metric iteration

查看:55
本文介绍了Keras 自定义指标迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Keras 还很陌生,我正在尝试定义自己的指标.它计算一致性指数,这是回归问题的度量.

I'm pretty new to Keras and I'm trying to define my own metric. It calculates concordance index which is a measure for regression problems.

def cindex_score(y_true, y_pred):
    sum = 0
    pair = 0    
    for i in range(1, len(y_true)):
        for j in range(0, i):
            if i is not j:
                if(y_true[i] > y_true[j]):
                  pair +=1
                  sum +=  1* (y_pred[i] > y_pred[j]) + 0.5 * (y_pred[i] == y_pred[j])
    if pair is not 0:
        return sum/pair
    else:
        return 0


def baseline_model(hidden_neurons, inputdim):
    model = Sequential()
    model.add(Dense(hidden_neurons, input_dim=inputdim, init='normal', activation='relu'))
    model.add(Dense(hidden_neurons, init='normal', activation='relu'))
    model.add(Dense(1, init='normal')) #output layer

    model.compile(loss='mean_squared_error', optimizer='adam', metrics=[cindex_score])
    return model

def run_model(P_train, Y_train, P_test, model):
    history = model.fit(numpy.array(P_train), numpy.array(Y_train), batch_size=50, nb_epoch=200)
    plotLoss(history)
    return model.predict(P_test)

baseline_model、run_model 和 cindex_score 函数在 one.py 中,以下函数在我调用模型的 two.py 中,

baseline_model, run_model and cindex_score functions are in one.py and the following function is in two.py where I called the model,

def experiment():
    hidden_neurons = 250
    dmodel=baseline_model(hidden_neurons, train_pair.shape[1])
    predicted_Y = run_model(train_pair,train_Y, test_pair, dmodel)

但我收到以下错误,'Tensor' 类型的对象没有 len()".它也不适用于 shape 属性.

But I get the following error, "object of type 'Tensor' has no len()". It does not work with shape attribute as well.

例如,y_true 表示为 Tensor("dense_4_target:0", shape=(?, ?), dtype=float32) 其形状为 Tensor("strided_slice:0", shape=(), dtype=int32).

For instance, y_true is represented as Tensor("dense_4_target:0", shape=(?, ?), dtype=float32) and its shape is Tensor("strided_slice:0", shape=(), dtype=int32).

您能帮我了解如何在 Tensor 对象中进行迭代吗?

Could you please help me about how to iterate within a Tensor object?

最好,

推荐答案

如果您习惯使用 tensorflow,那么您可以尝试使用以下代码:

If you are comfortable using tensorflow, then you can try using this code instead:

def cindex_score(y_true, y_pred):

    g = tf.subtract(tf.expand_dims(y_pred, -1), y_pred)
    g = tf.cast(g == 0.0, tf.float32) * 0.5 + tf.cast(g > 0.0, tf.float32)

    f = tf.subtract(tf.expand_dims(y_true, -1), y_true) > 0.0
    f = tf.matrix_band_part(tf.cast(f, tf.float32), -1, 0)

    g = tf.reduce_sum(tf.multiply(g, f))
    f = tf.reduce_sum(f)

    return tf.where(tf.equal(g, 0), 0.0, g/f)

以下是一些验证两种方法等效的代码:

Here is some code that verifies that both approaches are equivalent:

def _ref(J, K):
    _sum = 0
    _pair = 0
    for _i in range(1, len(J)):
        for _j in range(0, _i):
            if _i is not _j:
                if(J[_i] > J[_j]):
                  _pair +=1
                  _sum +=  1* (K[_i] > K[_j]) + 0.5 * (K[_i] == K[_j])
    return 0 if _pair == 0 else _sum / _pair

def _raw(J, K):

    g = tf.subtract(tf.expand_dims(K, -1), K)
    g = tf.cast(g == 0.0, tf.float32) * 0.5 + tf.cast(g > 0.0, tf.float32)

    f = tf.subtract(tf.expand_dims(J, -1), J) > 0.0
    f = tf.matrix_band_part(tf.cast(f, tf.float32), -1, 0)

    g = tf.reduce_sum(tf.multiply(g, f))
    f = tf.reduce_sum(f)

    return tf.where(tf.equal(g, 0), 0.0, g/f)


for _ in range(100):
    with tf.Session() as sess:
        inputs = [tf.placeholder(dtype=tf.float32),
                  tf.placeholder(dtype=tf.float32)]
        D = np.random.randint(low=10, high=1000)
        data = [np.random.rand(D), np.random.rand(D)]

        r1 = sess.run(_raw(inputs[0], inputs[1]),
                      feed_dict={x: y for x, y in zip(inputs, data)})
        r2 = _ref(data[0], data[1])

        assert np.isclose(r1, r2)

请注意,这只适用于一维张量(在 keras 中很少有这种情况).

Please note that this only works for 1D-tensors (rarely a case you will have in keras).

这篇关于Keras 自定义指标迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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