我有一个代码如下所示:
history['test_acc'].append(results.history['val_dense_5_accuracy'][0])
然后我想打印如下:
print('Epoch: '+str(epoch)+'/'+str(epochs-1), 'Learning rate:',
'Test_acc:', history['test_acc'][-1].round(4),
'Test_loss:', history['test_loss'][-1].round(4))`
但是在这一行中:
'Test_acc:', history['test_acc'][-1].round(4)
我有这个错误:'float'对象没有属性'round' 有什么问题吗?
以下是这件事上可能造成混淆的原因:
.round()
方法适用于 numpy.floatXX
对象,这些对象可能看起来像浮点数,但有一些额外的方法。如果不确定对象类型 - 使用 round()
函数。
演示:
>>> import numpy as np
>>> a = np.random.rand(5)
>>> a
array([0.92254859, 0.37214161, 0.35441334, 0.14139935, 0.40351668])
>>> z = a.mean()
>>> type(z)
<class 'numpy.float64'>
>>> z
0.43880391309677796
>>> z.round(3) # works OK with np.float64
0.439
>>> b = 2.345
>>> b.round(1) # fails with regular float
AttributeError: 'float' object has no attribute 'round'
>>> type(b)
<class 'float'>