I'm new to Python / machine learning. Leave a note of error handling for the data type.
An error occurred when performing the following calculation in a python program.
import numpy as np
def sigmoid(x)
return (1 / (1 + np.exp(-x)))
hoge = sigmoid(3) #An error occurs here
TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
--It seems that an error will occur if the argument assigned to the calculation of the numpy library contains int type data (see GitHub URL in the reference column). --It is good to change the data type to float type before the calculation process to the argument.
--Sigmoid (x) solved by explicitly stating that the data type of x is float.
def sigmoid(x)
x = x.float()
return (1 / (1 + np.exp(-x)))
hoge = sigmoid(3) # ->I was able to calculate correctly without any error
--When creating deep learning, this error occurred at the timing of using the sigmoid function. An array is entered in the argument x, but it seems that this problem occurred because each element of the array was an int type. ――I realized again that one of the characteristics of Python is that it works to some extent without declaring the data type, and I thought that it is better to keep in mind the calculation that specifies the data type.
(that's all)
Recommended Posts