我有一個張量串列,但我需要使用每個張量中保存的整數值。有沒有辦法在不需要改變急切模式的情況下檢索它?
示例測驗:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
if __name__ == '__main__':
test = tf.constant([1,4,5])
np_array = test.numpy()
integer_value = np_array[0]
print(type(integer_value))
結果: AttributeError: 'Tensor' object has no attribute 'numpy'
我需要將值 1,4,5 作為整數
uj5u.com熱心網友回復:
您可以使用Tensor.numpy()方法轉換tensorflow.Tensor為numpy陣列,或者如果您不想使用numpy表示Tensor.numpy().tolist()將變數轉換為 python 串列。
test = tf.constant([1,4,5])
np_array = test.numpy()
python_list = np_array.tolist()
integer_value = np_array[0] # or python_list[0]
編輯:
如果你關閉了急切執行,你就會被 TF 1.0 行為所拋棄,所以你必須做一個tensorflow.Session評估任何tensorflow.Tensor
tf.compat.v1.disable_eager_execution()
test = tf.constant([4, 5, 6])
sess = tf.compat.v1.Session()
sess.run(tf.compat.v1.global_variables_initializer())
np_array = test.eval(session=sess))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347402.html
