实现神经网络代码时出现类型错误

问题描述 投票:0回答:1
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()

尝试使用nor逻辑实现神经网络,但在实现时出现此错误。

类型错误:nor_NN() 不带参数

python tensorflow jupyter-notebook
1个回答
0
投票

尝试将

nor_NN()
的函数调用放在类定义之外,而不是类定义内部。这可能会导致错误,因为正在执行类定义内的代码。

更正代码(只需调整缩进):

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()
© www.soinside.com 2019 - 2024. All rights reserved.