我和我的朋友是编码新手,更具体地说是机器学习新手。他为我们在 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
在没有明确方向的情况下谷歌搜索并弄乱代码。我期待输出的准确性。
发生的事情没什么。我将运行或调试代码,当我期望输出准确性时,不会显示任何内容
简单返回准确度:
return self.accuracy
目前,您正在返回其
append
方法。