Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 第一次迭代后表现为奇数的神经网络_Python_Python 3.x_Machine Learning_Deep Learning_Data Science - Fatal编程技术网

Python 第一次迭代后表现为奇数的神经网络

Python 第一次迭代后表现为奇数的神经网络,python,python-3.x,machine-learning,deep-learning,data-science,Python,Python 3.x,Machine Learning,Deep Learning,Data Science,我是神经网络领域的新手,刚刚使用手写数字MNIST数据集浏览了我的第一个实际工作样本。我已经编写了一个代码,据我所知,它应该可以工作(至少在某种程度上),但我无法在阅读第一个培训样本后立即找出它被卡住的原因。我的代码如下: from keras.datasets import mnist import numpy as np def relu(x): return (x > 0) * x def relu_deriv(x): return x > 0 (x_tr

我是神经网络领域的新手,刚刚使用手写数字MNIST数据集浏览了我的第一个实际工作样本。我已经编写了一个代码,据我所知,它应该可以工作(至少在某种程度上),但我无法在阅读第一个培训样本后立即找出它被卡住的原因。我的代码如下:

from keras.datasets import mnist
import numpy as np

def relu(x):
    return (x > 0) * x

def relu_deriv(x):
    return x > 0

(x_train, y_train), (x_test, y_test) = mnist.load_data();

images = x_train[0:1000].reshape(1000, 28*28)
labels = y_train[0:1000]

test_images = x_test[0:1000].reshape(1000, 28*28)
test_labels = y_test[0:1000]

# converting the labels to a matrix
one_hot_labels = np.zeros((len(labels),10))
for i,j in enumerate(labels):
    one_hot_labels[i][j] = 1
labels = one_hot_labels


alpha = 0.005
hidden_size = 5 # size of the hidden layer

# initial weight matrixes
w1 = .2 * np.random.random(size=[784, hidden_size]) - .1
w2 = .2 * np.random.random(size=[hidden_size, 10]) - .1

for iteration in range(1000):
    error = 0
    for i in range(len(images)):
        layer_0 = images[i:i+1]
        layer_1 = relu(np.dot(layer_0, w1))
        layer_2 = np.dot(layer_1, w2)
        delta_2 = (labels[i:i+1] - layer_2)
        error += np.sum((delta_2) ** 2)
        delta_1 = delta_2.dot(w2.T) * relu_deriv(layer_1)
        w2 += alpha * np.dot(layer_1.T, delta_2)
        w1 += alpha * np.dot(layer_0.T, delta_1)
    print("error: {0}".format(error))


在第一次迭代中,出现了一个明显的大错误,之后它被修正到1000,但是不管再迭代多少次,它都会永远停留在这个错误上。

您还没有规范化图像数据。图像数据的值范围为0到255。由于这些较大的值,对权重的更新变得较大,从而在第一次迭代后产生非常大的权重。您可以按如下方式规范化图像数据

images = x_train[0:1000].reshape(1000, 28*28)
images = images / 255
labels = y_train[0:1000]

test_images = x_test[0:1000].reshape(1000, 28*28)
test_images = test_images / 255
test_labels = y_test[0:1000]

谢谢这正是问题所在:)