我在这里所做的不是在这个类中返回输出?[关闭]

What I'm doing here that is not returning an output in this class? [closed]

提问人:علي ليث جعفر A 提问时间:11/16/2023 最后编辑:desertnautعلي ليث جعفر A 更新时间:11/16/2023 访问量:59

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

4天前关闭。

我的朋友为我们在 main 中使用的代码创建了一个类,我们正在尝试调试它。我有限的经验和缺乏沟通让我感到困惑。代码如下:

任何需要的东西都在其他地方定义,所有需要的库都被导入

class NN:
    def __init__(self, data_path, n_hidden,epochs):
        # loading data set
        self.data_path = data_path
        self.data = pd.read_csv(data_path, nrows=1000).values
        self.xtr = self.data[:, 1:]
        self.ytr = self.data[:, 0]
        self.epochs = epochs

        # define network hyperparameters
        self.n_hidden = n_hidden
        self.n_input = self.xtr.shape[1]
        self.n_samples = self.xtr.shape[0]
        self.n_out = len(np.unique(self.ytr))
        self.accuracy = []
        self.accum_accuracy = 0
        self.alpha = 0.01
        self.yhat_max = 0

        # create weights of the network
        self.whi = np.random.uniform(-1, 1, size=(self.n_hidden,self.n_input))
        self.who = np.random.uniform(-1, 1, size=(self.n_out,self.n_hidden))

        # One-hot encoding
        self.ytr_hot = np.zeros((self.xtr.shape[0], self.n_samples))
        self.ytr_hot[np.arange(0, len(self.ytr)), self.ytr] = 1

    # data forward pass
    def forward_pass(self):
        for epoch in range(self.epochs):
            self.accum_accuracy = 0
            for i in range(self.n_samples):
                # hidden layer
                self.h_logit = np.dot(self.xtr[i, :], self.whi.T)
                self.hidden_out = sigmoid(self.h_logit)

                # output layer
                self.o_logit = np.dot(self.hidden_out,self.who.T)
                self.yhat = sigmoid(self.o_logit)

                # Append the max indices of the output layer and compute accumulated accuracy
                self.yhat_max = np.argmax(self.yhat)
                self.accum_accuracy += self.yhat_max == self.ytr[i]

                # Estimate the network error
                self.error = self.yhat - self.ytr_hot[i, :]
                self.who -= self.alpha * np.outer(self.error, self.hidden_out)

            self.accuracy.append(100*self.accum_accuracy/self.n_samples)
        return self.accuracy

四处搜索,弄乱代码,没有明确的方向。我期望准确性是输出。

发生的事情什么都不是。我将运行或调试代码,当我期望输出精度时,不会显示任何内容

Python 机器学习 深度学习 神经网络

评论

1赞 MatsLindh 11/16/2023
self.accuracy是一个列表;你返回的是该列表中的方法,而不是值本身:应该是 - 如果你想实际输出一些东西,你必须把它打印/写入文件或其他东西。appendreturn self.accuracy.appendreturn self.accuracy
2赞 rivimey 11/16/2023
当问这样的问题时,最好解释一下你要完成什么任务,发生了什么,以及为什么这不是你所期望的。

答:

0赞 JustLearning 11/16/2023 #1

只需返回精度:

return self.accuracy

目前,您正在返回其方法。append

评论

0赞 علي ليث جعفر A 11/16/2023
我编辑了它,但仍然没有显示任何内容
1赞 JustLearning 11/16/2023
你如何使用这个班级?
0赞 Gabriel Ramuglia 11/16/2023 #2

问题在于,代码定义了 NN 类及其方法,但不创建该类的实例或调用其 forward_pass 方法来获取输出。应实例化 NN 类并调用其 forward_pass 方法以查看准确性输出。

喜欢这个:

data_path = "path/to/your/data.csv"
n_hidden = 10
epochs = 5

nn_model = NN(data_path, n_hidden, epochs)

accuracy = nn_model.forward_pass()
print(accuracy)