创建感知机 python

来源:互联网 发布:在手机上java编程软件 编辑:程序博客网 时间:2024/06/01 21:45
# ----------# # In this exercise, you will add in code that decides whether a perceptron will fire based# on the threshold. Your code will go in lines 32 and 34. ## ----------import numpy as npclass Perceptron:    """    This class models an artificial neuron with step activation function.    """    def __init__(self, weights = np.array([1]), threshold = 0):        """        Initialize weights and threshold based on input arguments. Note that no        type-checking is being performed here for simplicity.        """        self.weights = weights        self.threshold = threshold    def activate(self,inputs):        """        Takes in @param inputs, a list of numbers equal to length of weights.        @return the output of a threshold perceptron with given inputs based on        perceptron weights and threshold.        """         # The strength with which the perceptron fires.        strength = np.dot(self.weights, inputs)        # TODO: return 0 or 1 based on the threshold        if strength <= self.threshold :            self.result = 0        else:            self.result = 1            return self.resultdef test():    """    A few tests to make sure that the perceptron class performs as expected.    Nothing should show up in the output if all the assertions pass.    """    p1 = Perceptron(np.array([1, 2]), 0.)    assert p1.activate(np.array([ 1,-1])) == 0 # < threshold --> 0    assert p1.activate(np.array([-1, 1])) == 1 # > threshold --> 1    assert p1.activate(np.array([ 2,-1])) == 0 # on threshold --> 0if __name__ == "__main__":    test()
原创粉丝点击