加载MNIST数据并使用numpy数组模型进行训练而不是训练

时间:2020-10-06 23:10:38

标签: python numpy deep-learning model mnist

我一直在尝试使用numpy制作的模型训练一些MNIST数据。但是,该模型并未在所有参数保持为0的情况下进行训练

我在下面粘贴了我的模型代码。如果您想查看我的加载数据步骤,请进一步向下滚动。

for j in range(iterations):
error, correct_cnt = (0.0, 0)

for i in range(len(images)):
    layer_0 = images[i: i + 1]
    layer_1 = relu(np.dot(layer_0, weights_0_1))
    layer_2 = np.dot(layer_1, weights_1_2)
    
    error += np.sum((labels[i: i + 1] - layer_2) ** 2)
    correct_cnt += int(np.argmax(layer_2) == np.argmax(labels[i: i + 1]))
    
    layer_2_delta = layer_2 - labels[i: i +1]
    layer_1_delta = layer_2_delta.dot(weights_1_2.T) * relu2deriv(layer_1)
    
    weights_0_1 = alpha * (layer_0.T.dot(layer_1_delta))
    weights_1_2 = alpha * (layer_1.T.dot(layer_2_delta))

sys.stdout.write("\r"+ \
                 " I:"+str(j)+ \
                 "Error:" + str(error/float(len(images)))[0:5] +\
                 "Correct:" + str(correct_cnt/float(len(images))))

这是加载数据步骤,以防您也需要。

(x_train, y_train), (x_test, y_test) = mnist.load_data()
images, labels = (x_train[0:1000].reshape(1000, 28 * 28) / 255, y_train[0:1000])
one_hot_labels = np.zeros((len(labels), 10))
for i, l in enumerate(labels):
    one_hot_labels[i][1] = 1
labels = one_hot_labels
test_images = x_test.reshape(len(x_test), 28*28)/ 255
test_labels = np.zeros((len(y_test), 10))
for i, l in enumerate(y_test):
    test_labels[i][l] = 1
np.random.seed(1)
relu = lambda x: (x>=0) * x
relu2deriv = lambda x: (x>=0)

alpha, iterations, hidden_size, pixels_per_image, num_labels = (0.005, 350, 40, 784, 10)
weights_0_1 = 0.2*np.random.random((pixels_per_image, hidden_size)) - 0.1
weights_1_2 = 0.2*np.random.random((hidden_size, num_labels)) - 0.1

感谢您的帮助。

0 个答案:

没有答案