Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading