Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'float' object has no attribute 'round'

Tags:

python

keras

I have a code was shown below:

history['test_acc'].append(results.history['val_dense_5_accuracy'][0]) 

then I want to print like below:

 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))`

but in this this line:

'Test_acc:', history['test_acc'][-1].round(4)

i have this error: 'float' object has no attribute 'round' what's the problem?

like image 577
Future Avatar asked Dec 13 '20 16:12

Future


People also ask

How do you round a floating object?

Round() Round() is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

What does float object is not iterable mean?

The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): . Here is an example of how the error occurs.

What does float object is not Subscriptable mean?

The error “TypeError: 'float' object is not subscriptable” occurs when you try to access a floating-point number like a list. To solve this error, ensure you only use indexing or slicing syntax on a subscriptable object, like a list or a string.

How to solve attributeerror 'float' object has no attribute?

The Python "AttributeError: 'float' object has no attribute" occurs when we try to access an attribute that doesn't exist on a floating-point number, e.g. 5.4. To solve the error, make sure the value is of the expected type before accessing the attribute. Here is an example of how the error occurs.

Why do I get attributeerror ‘float’ when calling get() in Python?

If we call the get() method on the float data type, Python will raise an AttributeError: ‘float’ object has no attribute ‘get’. The error can also happen if you have a method which returns an float instead of a dictionary. Let us take a simple example to reproduce this error.

How to round Float Number in Python?

You can round a float number in several ways using python. In this post we will cover: We can round float number in python by using round () which is a built-in function. It will round number x rounded to y decimal points. You can use function format if you want to round floats.

What does round () do in Python?

@xieyj17 round () by itself is a python op which will be slower on numpy arrays. @xieyj17 @seedergy where exactly in the code are you seeing this error?


1 Answers

The problem is that round is a built-in top level function, not a method on floats. Change:

history['test_acc'][-1].round(4)

to:

round(history['test_acc'][-1], 4) 

with a similar change to the test_loss expression.

like image 129
ShadowRanger Avatar answered Oct 28 '22 08:10

ShadowRanger