多层感知器对 mnist 数据集进行分类

Posted

技术标签:

【中文标题】多层感知器对 mnist 数据集进行分类【英文标题】:multiple layer perceptron to classify mnist dataset 【发布时间】:2021-07-25 15:28:53 【问题描述】:

我正在为数据科学课程开展的一个项目需要一些帮助。在这个项目中,我以三种方式对 MNIST 数据集的数字进行分类:

    使用由距离 1,2 和无穷大引起的相异矩阵 使用 BallTree 使用神经网络。

前两部分已完成,但我收到了无法解决的神经网络代码错误。这是代码。

#Upload the MNIST dataset
data = load('mnist.npz')

x_train = data['arr_0']
y_train = data['arr_1']
x_test  = data['arr_2']
y_test  = data['arr_3']

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

输出是

(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)

那么,

#Setting up the neural network and defining sigmoid function

#self.mtrx holds the neurons in each level

#self.weight, bias, grad hold weight, bias and gradient values between level L and L - 1

​

class NeuralNetwork:

​

    def __init__(self, rows, columns=0):

        self.mtrx = np.zeros((rows, 1))

        self.weight = np.random.random((rows, columns)) / columns ** .5

        self.bias = np.random.random((rows, 1)) * -1.0

        self.grad = np.zeros((rows, columns))

​

    def sigmoid(self):

        return 1 / (1 + np.exp(-self.mtrx))

​

    def sigmoid_derivative(self):

        return self.sigmoid() * (1.0 - self.sigmoid())

#Initializing neural network levels

​

lvl_input = NeuralNetwork(784)

lvl_one = NeuralNetwork(200, 784)

lvl_two = NeuralNetwork(200, 200)

lvl_output = NeuralNetwork(10, 200)

#Forward and backward propagation functions

​

def forward_prop():

    lvl_one.mtrx = lvl_one.weight.dot(lvl_input.mtrx) + lvl_one.bias

    lvl_two.mtrx = lvl_two.weight.dot(lvl_one.sigmoid()) + lvl_two.bias

    lvl_output.mtrx = lvl_output.weight.dot(lvl_two.sigmoid()) + lvl_output.bias
  ​    
​

def back_prop(actual):

    val = np.zeros((10, 1))

    val[actual] = 1

​

    delta_3 = (lvl_output.sigmoid() - val) * lvl_output.sigmoid_derivative()

    delta_2 = np.dot(lvl_output.weight.transpose(), delta_3) * lvl_two.sigmoid_derivative()

    delta_1 = np.dot(lvl_two.weight.transpose(), delta_2) * lvl_one.sigmoid_derivative()

​

    lvl_output.grad = lvl_two.sigmoid().transpose() * delta_3

    lvl_two.grad = lvl_one.sigmoid().transpose() * delta_2

    lvl_one.grad = lvl_input.sigmoid().transpose() * delta_1

#Storing mnist data into np.array

​

def make_image(c): 

    lvl_input.mtrx = x_train[c]

#Evaluating cost function

​

def cost(actual):

    val = np.zeros((10, 1))

    val[actual] = 1

    cost_val = (lvl_output.sigmoid() - val) ** 2

    return np.sum(cost_val)

#Subtraction gradients from weights and initializing learning rate

​

learning_rate = .01

​

def update():

    lvl_output.weight -= learning_rate * lvl_output.grad

    lvl_two.weight -= learning_rate * lvl_two.grad

    lvl_one.weight -= learning_rate * lvl_one.grad

最后我训练神经网络。

#Training neural network
#iter_1 equals number of batches
#iter_2 equals number of iterations in one batch

iter_1 = 50
iter_2 = 100

for batch_num in range(iter_1):
    update()
    counter=0
    for batches in range(iter_2):
        make_image(counter)
        num = np.argmax(y_train[counter])
        counter += 1
        forward_prop()
        back_prop(num)
        print("actual: ", num, "     guess: ", np.argmax(lvl_output.mtrx), "     cost", cost(num))

我收到以下错误,但我无法弄清楚我的代码有什么问题。有人可以帮忙吗?

ValueError                                Traceback (most recent call last)
<ipython-input-12-8821054ddd29> in <module>
     13         num = np.argmax(y_train[counter])
     14         counter += 1
---> 15         forward_prop()
     16         back_prop(num)
     17         print("actual: ", num, "     guess: ", np.argmax(lvl_output.mtrx), "     cost", cost(num))

<ipython-input-6-e6875bcd1a03> in forward_prop()
      2 
      3 def forward_prop():
----> 4     lvl_one.mtrx = lvl_one.weight.dot(lvl_input.mtrx) + lvl_one.bias
      5     lvl_two.mtrx = lvl_two.weight.dot(lvl_one.sigmoid()) + lvl_two.bias
      6     lvl_output.mtrx = lvl_output.weight.dot(lvl_two.sigmoid()) + lvl_output.bias

ValueError: shapes (200,784) and (28,28) not aligned: 784 (dim 1) != 28 (dim 0)

【问题讨论】:

请不要用后续问题更新您的帖子,尤其是在提供答案(已编辑)之后。如果您有新问题,随时欢迎您提出新问题。 【参考方案1】:

在您的代码中:

def make_image(c): 
    lvl_input.mtrx = x_train[c]

尽管您使用形状 (row, 1) 初始化 lvl_input.mtrx,使用形状 (28,28) 的数据然后分配给 lvl_input.mtrx。基本上reshape()需要做训练数据

【讨论】:

谢谢!你能告诉我应该在哪里添加reshape() 吗? lvl_input.mtrx = x_train[c].reshape((row, 1)) 如果 x_train 是一个 np 数组

以上是关于多层感知器对 mnist 数据集进行分类的主要内容,如果未能解决你的问题,请参考以下文章

翻译: 4.2. 从零开始实现多层感知器MLP pytorch

在 MATLAB 中使用感知器对数据进行分类

多层感知机的从零开始实现( 从D2L 包中抽取函数)

多层感知机的从零开始实现( 从D2L 包中抽取函数)

动手深度学习10- pytorch多层感知机从零实现

gluon 实现多层感知机MLP分类FashionMNIST