我用 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
提交
取消