我用 Keras 寫了一個(gè)損失函數(shù)。它有兩個(gè)參數(shù),y_true和y_pred。我的第一行代碼是:batch = y_pred.get_shape()[0]. 然后在我的batch變量中我有第一個(gè)維度y_pred,所以我循環(huán)range(batch)并寫下我寫的內(nèi)容。那沒(méi)關(guān)系。問(wèn)題是當(dāng)我編譯所有內(nèi)容時(shí),我收到一條錯(cuò)誤消息,告訴我批處理不是整數(shù),而是張量。然后,作為 Tensorflow 的初學(xué)者,我開(kāi)始思考如何從 中獲取一個(gè)整數(shù)batch,它應(yīng)該是一個(gè)整數(shù),但是一個(gè)張量。我試圖這樣做,sess.run(batch)但這根本沒(méi)有幫助。所以,我的問(wèn)題是如何從表示整數(shù)變量的張量中獲取整數(shù)。我想使用一些真正給我一個(gè)整數(shù)而不是張量的函數(shù)。請(qǐng)幫忙。這是我的代碼: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這樣我可以循環(huán)并做一些事情。我也用過(guò)size,shape它也行不通。
添加回答
舉報(bào)
0/150
提交
取消