第一個神經網絡(測試版)

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 09:01:33 2019

@author: txx
"""

# neural network class definition
import numpy
import scipy.special
import matplotlib.pyplot
class neuralNetwork:
    
    # initialize the neural network
    def __init__(self,inputnodes,hiddennodes,outputnodes,
                 learningrate):
        
        #set number of nodes in each input,hidden,output layer
        self.inodes=inputnodes
        self.hnodes=hiddennodes
        self.onodes=outputnodes
        
        
        # link weight matrices,with and who
        # weights inside the arrays are w_i_j,where link is from
        # node i to node j in the next layer
        # w11 w21
        # w12 w22 etc
        self.wih=numpy.random.normal(0.0,pow(self.hnodes,-0.5),
                                     (self.hnodes,self.inodes))
        self.who=numpy.random.normal(0.0,pow(self.onodes,-0.5),
                                     (self.onodes,self.hnodes))
        
        
        # learning rate
        self.lr=learningrate
        
        # activation function is the signmoid function
        self.activation_function=lambda x: scipy.special.expit(x)
        
        pass
    
    
    # train the neutal network
    def train(self,inputs_list,targets_list):
        
        #convert inputs list to 2d array
        inputs=numpy.array(inputs_list,ndmin=2).T
        targets=numpy.array(targets_list,ndmin=2).T
        
        # calculate signals into hidden layer
        hidden_inputs=numpy.dot(self.wih,inputs)
        #calculate the signals emering from hidden layer
        hidden_outputs=self.activation_function(hidden_inputs)
        
        #calculate signals into final output layer
        final_inputs=numpy.dot(self.who,hidden_outputs)
        #calcluate the signals emerging from final output layer
        final_outputs=self.activation_function(final_inputs)
        
        
        # output layer error is the (target - actual)
        output_errors=targets - final_outputs
        # hidden layer error is the output_errors,split by weights,
# recombined at hidden nodes
        hidden_errors=numpy.dot(self.who.T,output_errors)
        
        
        #update the weigths for the links between the hidden 
#and output layers
        
        self.who +=self.lr * numpy.dot((output_errors *
 final_outputs*(1.0-final_outputs)),
 numpy.transpose(hidden_outputs))
        
        #update the weights for the links between the input and hidden layers
        self.wih +=self.lr * numpy.dot((hidden_errors *
 hidden_outputs * (1.0-hidden_outputs)),numpy.transpose(inputs))
        pass
    
    
        # query the neural network
        def query(self,inputs_list):
            
            # convert inputs list to 2d array
            inputs=numpy.array(inputs_list,ndmin=2).T
            
            #calculate signals into hidden layer
            hidden_inputs=numpy.dot(self.wih,inputs)
            # calcualate the signals emering form hidden layer
            hidden_outputs=self.activation_function(hidden_inputs)
            
            # calculate signals into final output layer
            final_inputs = numpy.dot(self.who,hidden_outputs)
            # calculate the signals emering from final output layer
            final_outputs=self.activation_function(final_inputs)
            return final_outputs
    
    
# number of input,hidden and output nodes
input_nodes=784
hidden_nodes=100
output_nodes=10

# learning rate is 0.3
learning_rate=0.3

# create instance of neural network
n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)

# load the mnist training data CSV file into a list
training_data_file=open("D:/anaconda/mnist_dataset/mnist_train_100.csv",'r')
training_data_list=training_data_file.readlines()
training_data_file.close()

# train the neural network

# go through all records in the training data set
for record in training_data_list:
    # split the record by the ',' commas
    all_values=record.split(',')
    # scale and shift the inputs
    inputs=(numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
    #create the target output values (all  0.01,except the desired label
    # label which is 0.99)
    targets=numpy.zeros(output_nodes)+0.01
    # all_values[0] is the target label for this record
    targets[int(all_values[0])]=0.99
    n.train(inputs,targets)
    pass


test_data_file=open("D:/anaconda/mnist_dataset/mnist_test_10.csv",'r')
test_data_list=test_data_file.readlines()
test_data_file.close()

all_values=test_data_list[0].split(',')

print(all_values[0])

image_array=numpy.asfarray(all_values[1:]).reshape((28,28))
matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')
   
    

        


        
        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章