I'm trying to get a construct a class that calls a function from within the same class but I'm having trouble doing so.
I've had a look at:
Python function pointers within the same Class
But I've had no luck with the approach presented. I've created this simple example:
#### Libraries ####
# Third Party Libraries
import numpy as np
#### Defines the activation functions ####
class sigmoid_class(object):
def __init__(self, z):
self.z = z
def activation_fn(self):
"""The sigmoid function"""
return 1.0/(1.0+np.exp(-self.z))
def prime(self):
"""The derivative of the sigmoid function"""
return activation_fn(self.z)*(1-activation_fn(self.z))
a = sigmoid_class(0)
print(a.prime())
when I test with print(a.activation_fn(0)) I get the desired output, but when I try `print(a.prime())
I get `NameError: name 'activation_fn' is not defined
I'm not sure how to fix this so that it functions properly
self.activation_fn()activation_fn is a member function of the class, so you need to give it the class instance (which is self) in order to call it.self.activation_fn(self.z)*(1-self.activation_fn(self.z))self.activation_fn().activation_fndoesn't take the z value as argument - it looks it up fromself