为什么我的神经网络不适用于MNIST数据集?

时间:2018-04-02 15:48:02

标签: python neural-network mnist

我已经写了附加的神经网络两次仔细阅读每一行我已经参考了tariq rashid的书如何训练你的神经网络 在书中,你声称要具有高达95%的准确度。 但是我的代码没有收敛,准确度大约是10%,这意味着它是随机猜测的。 请帮忙

import numpy as np
import scipy.special
from random import *

class NeuralNetwork:
def __init__(self,inp_layer,hid_layer,out_layer):
    self.inp_layer = inp_layer
    self.hid_layer = hid_layer
    self.out_layer = out_layer
    self.learning_rate = 0.1

    self.weights_ih = np.random.normal(0.0, pow(self.hid_layer, 0.5),(self.hid_layer, self.inp_layer))
    self.weights_ho = np.random.normal(0.0, pow(self.out_layer, 0.5),(self.out_layer, self.hid_layer))

    self.bias_h = np.random.rand(self.hid_layer,1)
    self.bias_o = np.random.rand(self.out_layer,1)

    #self.activation = lambda x: scipy.special.expit(x)
    #self.d_activation = lambda x: x*(1-x)
    self.activation = lambda x: np.tanh(x)
    self.d_activation = lambda x: 1-(x*x)

def train(self,inps,targs):
    inputs = np.array(inps,ndmin=2).T
    targets = np.array(targs,ndmin=2).T

    hidden_inputs = np.dot(self.weights_ih, inputs)
    hidden_inputs += self.bias_h
    hidden_outputs = self.activation(hidden_inputs)
    output_inputs = np.dot(self.weights_ho, hidden_outputs)
    output_inputs += self.bias_o
    outputs = self.activation(output_inputs)

    output_errors = targets - outputs
    hidden_errors = np.dot(self.weights_ho.transpose(),output_errors)

    delta_weights_ho = self.learning_rate*np.dot((output_errors*self.d_activation(outputs)),hidden_outputs.transpose())
    delta_weights_ih = self.learning_rate*np.dot((hidden_errors*self.d_activation(hidden_outputs)),inputs.transpose())
    delta_bias_o = self.learning_rate*(output_errors*self.d_activation(outputs))
    delta_bias_h = self.learning_rate*(hidden_errors*self.d_activation(hidden_outputs))

    self.weights_ho += delta_weights_ho
    self.weights_ih += delta_weights_ih

    self.bias_o += delta_bias_o
    self.bias_h += delta_bias_h

def predict(self,inps):
    inputs = np.array(inps,ndmin=2).T

    hidden_inputs = np.dot(self.weights_ih, inputs)
    hidden_inputs += self.bias_h
    hidden_outputs = self.activation(hidden_inputs)
    output_inputs = np.dot(self.weights_ho, hidden_outputs)
    output_inputs += self.bias_o
    outputs = self.activation(output_inputs)

    return outputs

这里是上述神经网络的实现

from NeuralNetwork import *
from random import *
import numpy as np

nn = NeuralNetwork(784,200,10)

file = open('mnist_train.csv')
lines = file.readlines()
file.close()
tFile = open('mnist_test.csv')
tLines = tFile.readlines()
tFile.close()

for epoch in range(7):
    shuffle(lines)
    for line in range(60000):
        data = lines[line].split(',')
        inputs = (np.asfarray(data[1:])/255)
        targets = np.zeros(10)
        targets[int(data[0])] = 0.99
        nn.train(inputs,targets)

    fitness = 0
    for tLine in range(10000):
        tData = tLines[tLine].split(',')
        tInputs = (np.asfarray(tData[1:])/255)
        tTarget = int(tData[0])
        prediction = nn.predict(tInputs)
        if np.argmax(prediction)==tTarget:
            fitness+=1

    print('epoch',epoch,'complete','fitness',fitness/1000)

这是输出:

epoch 0 complete fitness 0.124
epoch 1 complete fitness 0.169
epoch 2 complete fitness 0.138
epoch 3 complete fitness 0.144
epoch 4 complete fitness 0.178
epoch 5 complete fitness 0.231
epoch 6 complete fitness 0.162

每个时代之后健身应该增加

0 个答案:

没有答案