TypeError: unsupported operand type(s) for /: 'int' and 'Dimension', please use // instead

錯誤

運行此段代碼
print(step, ‘Evaluate Acc:’, total_correct/total)
提示錯誤
TypeError: unsupported operand type(s) for /: ‘int’ and ‘Dimension’, please use // instead

查找原因

查看數據的類型
print(type(total_correct))
print(type(total))
print(step, ‘Evaluate Acc:’, total_correct/total)
返回
<class ‘numpy.int64’>
<class ‘tensorflow.python.framework.tensor_shape.Dimension’>
所以錯誤的原因在,一個是numpy對象,一個是Dimension對象,無法相除

解決方法

用int函數,將Dimension對象對象轉換爲int
print(type(total_correct))
print(type(total))
total = int(total)
print(type(total))
print(step, ‘Evaluate Acc:’, total_correct/total)
問題解決
<class ‘numpy.int64’>
<class ‘tensorflow.python.framework.tensor_shape.Dimension’>
<class ‘int’>
0 Evaluate Acc: 0.0675

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章