Search code examples
pythontensorflowjupyter-notebook

TypeError while implementing Neural Network code


import numpy as np

class nor_NN:
  def __init__(self,iv):
    self.input_vector = np.array(iv)
    self.weight_vector = np.array([[0.5,-1,-1]])
    self.output = np.array([])

  def compute(self):
    mvf = np.c_[np.ones(4),self.input_vector]
    result = mvf.dot(self.weight_vector.transpose())
    self.output = [0 if r < 0 else 1 for r in result]

  def show_result(self):
    print("Input vector : \n", self.input_vector)
    print("Output : \n", self.output)

  def get_output(self):
    return self.output

  n = nor_NN(np.array([[0,0], [0,1], [1,0], [1,1]]))

  n.compute()
  n.show_result()

Tried to implement the neural network using nor logic but got this error while implementing it.

TypeError: nor_NN() takes no arguments


Solution

  • Try to place the function call of nor_NN() outside of the class definition, not inside it. This might be causing the error because the code inside the class definition is being executed.

    Corrected code (just adjust the indentation):

    import numpy as np
    
    class nor_NN:
        def __init__(self, iv):
            self.input_vector = np.array(iv)
            self.weight_vector = np.array([[0.5,-1,-1]])
            self.output = np.array([])
    
        def compute(self):
            mvf = np.c_[np.ones(4), self.input_vector]
            result = mvf.dot(self.weight_vector.transpose())
            self.output = [0 if r < 0 else 1 for r in result]
    
        def show_result(self):
            print("Input vector : \n", self.input_vector)
            print("Output : \n", self.output)
    
        def get_output(self):
            return self.output
    
    n = nor_NN(np.array([[0,0], [0,1], [1,0], [1,1]]))
    n.compute()
    n.show_result()