我有這個代碼,它接受一個形狀為 的張量(3, 3)并將其重塑為 (9,). 之后它應用了一個one_hot函式,但它拋出了一個錯誤。
這是代碼:
import tensorflow as tf
t1 = tf.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=tf.float32)
t2 = tf.constant([[1], [-1], [1]], dtype=tf.float32)
print(tf.one_hot(tf.reshape(t1, -1), depth=2))
錯誤是:
InvalidArgumentError: Value for attr 'TI' of float is not in the list of allowed values: uint8, int32, int64
; NodeDef: {{node OneHot}}; Op<name=OneHot; signature=indices:TI, depth:int32, on_value:T, off_value:T -> output:T; attr=axis:int,default=-1; attr=T:type; attr=TI:type,default=DT_INT64,allowed=[DT_UINT8, DT_INT32, DT_INT64]> [Op:OneHot]
我在 GoogleColab 筆記本上作業,所以我認為問題可能出在 TensorFlow 的版本或張量的資料型別上,但任何其他解決方案將不勝感激。
uj5u.com熱心網友回復:
你可以簡單地將你的張量轉換為tf.int32或類似的,因為tf.one_hot期望整數indices:
import tensorflow as tf
t1 = tf.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=tf.float32)
t2 = tf.constant([[1], [-1], [1]], dtype=tf.float32)
print(tf.one_hot(tf.cast(tf.reshape(t1, -1), dtype=tf.int32), depth=3))
tf.Tensor(
[[0. 1. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]], shape=(9, 3), dtype=float32)
或與depth=2:
tf.Tensor(
[[0. 1.]
[1. 0.]
[1. 0.]
[1. 0.]
[0. 1.]
[1. 0.]
[1. 0.]
[1. 0.]
[0. 1.]], shape=(9, 2), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/357744.html
上一篇:根據“權重”計算“排名”-給定不同范圍的值,公式是什么
下一篇:找到產生給定值的最小硬幣集
