In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python how to use three-layer neural network to achieve handwritten digital classification", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python how to use three-layer neural network to achieve handwritten digital classification" bar!
I. composition of neural network
A complete neural network is usually made up of several basic network layers. The three-layer fully connected neural network in this experiment is composed of three fully connected layers. The ReLU activation function is inserted between each two fully connected layers to introduce nonlinear transformation. Finally, the Softmax layer is used to calculate the cross loss, as shown in the following figure. Therefore, the basic units used in this experiment include full connection layer, ReLU activation function and Softmax loss function.
Second, the code implementation 1. Import the library import numpy as npimport structimport os2. Import dataset MNIST_DIR = "mnist_data" TRAIN_DATA = "train-images-idx3-ubyte" TRAIN_LABEL = "train-labels-idx1-ubyte" TEST_DATA = "t10k-images-idx3-ubyte" TEST_LABEL = "t10k-labels-idx1-ubyte"
Dataset link
Remember to decompress the dataset after downloading
3. Full connection layer class FullyConnectedLayer (object): def _ init__ (self, num_input, num_output): # full connection layer initialization self.num_input = num_input self.num_output = num_output def init_param (self, std=0.01): # Parameter initialization self.weight = np.random.normal (loc=0, scale=std, size= (self.num_input) Self.num_output)) self.bias = np.zeros ([1, self.num_output]) def forward (self, input): # forward propagation calculation self.input = input self.output = np.dot (self.input,self.weight) + self.bias return self.output def backward (self, top_diff): # back propagation calculation self.d_weight = np.dot (self.input.T Top_diff) self.d_bias = top_diff # bottom_diff = np.dot (top_diff,self.weight.T) return bottom_diff def update_param (self, lr): # Parameter update self.weight = self.weight-lr * self.d_weight self.bias = self.bias-lr * self.d_bias def load_param (self, weight Bias): # Parameter load assert self.weight.shape = = weight.shape assert self.bias.shape = = bias.shape self.weight = weight self.bias = bias def save_param (self): # Parameter Save return self.weight, self.bias4.ReLU activate function layer class ReLULayer (object): def forward (self) Input): # calculation of forward propagation self.input = input output = np.maximum (self.input,0) return output def backward (self, top_diff): # calculation of back propagation b = self.input b [b > 0] = 1b [b = 3: # Friendlier error message IV, results show
Under the condition of not changing the network structure, I can adjust the parameters by myself.
If _ _ name__ ='_ _ main__': H2, h3, e = 128,64, 20class MNIST_MLP (object): def _ init__ (self, batch_size=64, input_size=784, hidden1=32, hidden2=16, out_classes=10, lr=0.01, max_epoch=1,print_iter=100):
In order to improve the accuracy, of course, you can modify it in other ways. Here is my output:
Supplement
ValueError: Object arrays cannot be loaded when allow_pickle=False solution
Error reading .npz file Times:
Population_data=np.load (". / data/populations.npz") print (population_data.files) # contains two arrays data feature_namesdata=population_data ['data'] print (data) print (population_data [' feature_names'])
Error report:
['data',' feature_names'] Traceback (most recent call last): File "E:/pycharm file/ uses scikit-learn to build models / build unary linear models .py", line 32, in data=population_data ['data'] File "E:\ pycharm file\ venv\ lib\ site-packages\ numpy\ lib\ npyio.py", line 262 In _ _ getitem__ pickle_kwargs=self.pickle_kwargs) File "E:\ pycharm file\ venv\ lib\ site-packages\ numpy\ lib\ format.py", line 692, in read_array raise ValueError ("Object arrays cannot be loaded when" ValueError: Object arrays cannot be loaded when allow_pickle=False
Error: numpy version is too high, I use 1.16.3, should be downgraded to 1.16.2
Two solutions:
Numpy 1.16.3 was released a few days ago. Stated from the release: "the functions np.load () and np.lib.format.read_array () use the allow_pickle keyword, which now defaults to False in response to CVE-2019-6446
< nvd.nist.gov/vuln/detail / CVE-2019-6446 >Downgrading to 1.16.2 helped me, because the error occurred within some library
The first: click on the error report, enter the source code (.py), and comment out 693 lines:
# if not allow_pickle: # raise ValueError ("Object arrays cannot be loaded when" # "allow_pickle=False") # Now read the actual data. If dtype.hasobject: # The array contained Python objects. We need to unpickle the data. # if not allow_pickle: # raise ValueError ("Object arrays cannot be loaded when" # "allow_pickle=False") if pickle_kwargs is None: pickle_kwargs = {} try: array = pickle.load (fp * * pickle_kwargs) except UnicodeError as err: if sys.version_info [0] > = 3: # Friendlier error message
The problem was solved successfully after modification, but I don't know if there will be sequelae if the source code is changed.
Second: downgrade the numpy version
Pip install numpy==1.16.2
Both of the above two methods can successfully solve the problem of error reporting.
Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.