我用 Keras 写了一个损失函数。它有两个参数,y_true和y_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))正如你所看到的,我想要batch变量是int这样我可以循环并做一些事情。我也用过size,shape它也行不通。
添加回答
举报
0/150
提交
取消