gpt4 book ai didi

python - MNIST 手写数字

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:38:46 26 4
gpt4 key购买 nike

我尝试使用以下数据集在 python 中制作一个能够识别手写数字的脚本:http://deeplearning.net/data/mnist/mnist.pkl.gz .

关于这个问题和我试图实现的算法的更多信息可以在这个链接中找到:http://neuralnetworksanddeeplearning.com/chap1.html

我已经为每个数字使用感知器实现了分类算法。

import cPickle, gzip
import numpy as np

f = gzip.open('mnist.pkl.gz', 'rb')
train_set, valid_set, test_set = cPickle.load(f)
f.close()

def activation(x):
if x > 0:
return 1
return 0

bias = 0.5
learningRate = 0.01

images = train_set[0]
targets = train_set[1]

weights = np.random.uniform(0,1,(10,784))
for nr in range(0,10):
for i in range(0,49999):
x = images[i]
t = targets[i]
z = np.dot(weights[nr],x) + bias
output = activation(z)
weights[nr] = weights[nr] + (t - output) * x * learningRate
bias = bias + (t - output) * learningRate

images = test_set[0]
targets = test_set[1]

OK = 0

for i in range range(0, 10000):
vec = []
for j in range(0,10):
vec.append(np.dot(weights[j],images[i]))
if np.argmax(vec) == targets[i]:
OK = OK + 1

print("The network recognized " + str(OK) +'/'+ "10000")

我通常会识别 10% 的数字,这意味着我的算法什么都不做,与随机算法相同。

虽然我知道这个问题很流行,而且我可以很容易地在网上找到另一个解决方案,但我仍然请求您帮助我找出代码中的错误。

也许我错误地初始化了 learningRate、bias 和 weights 的值。

最佳答案

感谢@Kevinj22 和其他人,我最终能够解决这个问题。

import cPickle, gzip
import numpy as np

f = gzip.open('mnist.pkl.gz', 'rb')
train_set, valid_set, test_set = cPickle.load(f)
f.close()

def activation(x):
if x > 0:
return 1
return 0

learningRate = 0.01

images = train_set[0]
targets = train_set[1]

weights = np.random.uniform(0,1,(10,784))

for nr in range(0,10):
for i in range(0,50000):
x = images[i]
t = targets[i]
z = np.dot(weights[nr],x)
output = activation(z)
if nr == t:
target = 1
else:
target = 0
adjust = np.multiply((target - output) * learningRate, x)
weights[nr] = np.add(weights[nr], adjust)

images = test_set[0]
targets = test_set[1]

OK = 0

for i in range(0, 10000):
vec = []
for j in range(0,10):
vec.append(np.dot(weights[j],images[i]))
if np.argmax(vec) == targets[i]:
OK = OK + 1

print("The network recognized " + str(OK) +'/'+ "10000")

这是我更新的代码。我在第一次尝试时没有引入损失计算。我也摆脱了偏见,因为我发现它在我的实现中没有用。

这段代码我跑了10次,平均准确率为88%

关于python - MNIST 手写数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com