提取张量的一维而不使用get_shape,size和shape函数?

时间:2018-09-03 11:11:22

标签: python tensorflow neural-network keras

我在Keras中编写了损失函数。它具有两个参数,y_truey_pred。我的第一行代码是:batch = y_pred.get_shape()[0]。然后在我的batch变量中,我的第一个维度为y_pred,因此我遍历了range(batch)并写下了自己写的内容。没关系问题是,当我编译所有内容时,我收到一条错误消息,告诉我批处理不是整数,而是张量。然后,作为Tensorflow的初学者,我开始考虑如何从batch中获取一个整数,该整数应该是整数,但是是张量。我尝试做sess.run(batch),但这根本没有帮助。所以,我的问题是如何从表示整数变量的张量中获取整数。我想使用一些确实给我一个整数而不是张量的函数。请帮忙。这是我的代码:

def custom_loss(y_true, y_pred):

    batch = y_pred.get_shape()[0]

    list_ones = returnListOnes(batch)
    tensor_ones = tf.convert_to_tensor(list_ones)

    loss = 0
    for i in range(batch):
      for j in range(S):
        for k in range(S):
            lista = returnListOnesIndex(batch, [j,k,0])

            lista_bx = returnListOnesIndex(batch, [j,k,1])
            lista_by = returnListOnesIndex(batch, [j,k,2])
            lista_bw = returnListOnesIndex(batch, [j,k,3])
            lista_bh = returnListOnesIndex(batch, [j,k,4])

            lista_to_tensor = tf.convert_to_tensor(lista)

            lista_bx_to_tensor = tf.convert_to_tensor(lista_bx)
            lista_by_to_tensor = tf.convert_to_tensor(lista_by)
            lista_bw_to_tensor = tf.convert_to_tensor(lista_bw)
            lista_bh_to_tensor = tf.convert_to_tensor(lista_bh)

            element = tf.reduce_sum(tf.multiply(lista_to_tensor,y_pred))
            element_true = tf.reduce_sum(tf.multiply(lista_to_tensor, y_true))

            element_bx = tf.reduce_sum(tf.multiply(lista_bx_to_tensor, y_pred))
            element_bx_true = tf.reduce_sum(tf.multiply(lista_bx_to_tensor, y_true))

            element_by = tf.reduce_sum(tf.multiply(lista_by_to_tensor, y_pred))
            element_by_true = tf.reduce_sum(tf.multiply(lista_by_to_tensor, y_true))

            element_bw = tf.reduce_sum(tf.multiply(lista_bw_to_tensor, y_pred))
            element_bw_true = tf.reduce_sum(tf.multiply(lista_bw_to_tensor, y_true))

            element_bh = tf.reduce_sum(tf.multiply(lista_bh_to_tensor, y_pred))
            element_bh_true = tf.reduce_sum(tf.multiply(lista_bh_to_tensor, y_true))

            distance = tf.square(tf.subtract(element, element_true))
            distance_bx = tf.square(tf.subtract(element_bx, element_bx_true))
            distance_by = tf.square(tf.subtract(element_by, element_by_true))
            distance_bw = tf.square(tf.subtract(element_bw, element_bw_true))
            distance_bh = tf.square(tf.subtract(element_bh, element_bh_true))

            suma = tf.add(distance, distance_bx)
            suma = tf.add(suma, distance_by)
            suma = tf.add(suma, distance_bw)
            suma = tf.add(suma, distance_bh)



            loss += tf.cond(tf.greater(element_true,0.),
                            lambda: suma,
                            lambda: distance)
    return loss

如您所见,我希望batch变量为int,以便我可以循环执行某些操作。我还使用了sizeshape,但也无法使用。

1 个答案:

答案 0 :(得分:0)

矢量化的代码肯定会更有效率,我会强烈鼓励您尝试以不需要循环的方式编写代码。

但是,如果无法这样做,可以求助于tf.map_fn

从您的代码中,我看不到循环中i的位置。我猜这是一个错误(可能是循环内的batch应该是i)或我自己的盲目性-否则,您可以将结果乘以批处理大小...