日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的神经网络编程_Python神经网络编程 第二章 使用Python进行DIY

發布時間:2024/7/19 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的神经网络编程_Python神经网络编程 第二章 使用Python进行DIY 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用神經網絡識別手寫數字:

import numpy

# scipy.special for the sigmoid function expit(),即S函數

import scipy.special

# library for plotting arrays

import matplotlib.pyplot

# ensure the plots are inside this notebook, not an external window

%matplotlib inline // 在notebook上繪圖,而不是獨立窗口

# neural network class definition

class neuralNetwork:

# initialise 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, wih 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

# numpy.random.normal(loc,scale,size) loc:概率分布的均值;scale:概率分布的方差;size:輸出的shape

self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))

self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))

# learning rate

self.lr = learningrate

# activation function is the sigmoid function

# 使用lambda創建的函數是沒有名字的

self.activation_function = lambda x: scipy.special.expit(x)

pass

# train the neural 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 emerging from 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 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 weights 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)

# calculate the signals emerging from 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 emerging from final output layer

final_outputs = self.activation_function(final_inputs)

return final_outputs

# number of input, hidden and output nodes

# 選擇784個輸入節點是28*28的結果,即組成手寫數字圖像的像素個數

input_nodes = 784

# 選擇使用100個隱藏層不是通過使用科學的方法得到的。通過選擇使用比輸入節點的數量小的值,強制網絡嘗試總結輸入的主要特點。

# 但是,如果選擇太少的隱藏層節點,會限制網絡的能力,使網絡難以找到足夠的特征或模式。

# 同時,還要考慮到輸出層節點數10。

# 這里應該強調一點。對于一個問題,應該選擇多少個隱藏層節點,并不存在一個最佳方法。同時,我們也沒有最佳方法選擇需要幾層隱藏層。

# 就目前而言,最好的辦法是進行實驗,直到找到適合你要解決的問題的一個數字。

hidden_nodes = 200

output_nodes = 10

# learning rate,需要多次嘗試,0.2是最佳值

learning_rate = 0.1

# 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("mnist_dataset/mnist_train.csv", 'r')

training_data_list = training_data_file.readlines()

training_data_file.close()

# train the neural network

# epochs is the number of times the training data set is used for training

# 就像調整學習率一樣,需要使用幾個不同的世代進行實驗并繪圖,以可視化這些效果。直覺告訴我們,所做的訓練越多,所得到的的性能越好。

# 但太多的訓練實際上會過猶不及,這是由于網絡過度擬合訓練數據。

# 在大約5或7個世代時,有一個甜蜜點。在此之后,性能會下降,這可能是過度擬合的效果。

# 性能在6個世代的情況下下降,這可能是運行中出了問題,導致網絡在梯度下降過程中被卡在了一個局部的最小值中。

# 事實上,由于沒有對每個數據點進行多次實驗,無法減小隨機過程的影響。

# 神經網絡的學習過程其核心是隨機過程,有時候工作得不錯,有時候很糟。

# 另一個可能的原因是,在較大數目的世代情況下,學習率可能設置過高了。在更多世代的情況下,減小學習率確實能夠得到更好的性能。

# 如果打算使用更長的時間(多個世代)探索梯度下降,那么可以采用較短的步長(學習率),總體上可以找到更好的路徑。

# 要正確、科學地選擇這些參數,必須為每個學習率和世代組合進行多次實驗,盡量減少在梯度下降過程中隨機性的影響。

# 還可嘗試不同的隱藏層節點數量,不同的激活函數。

epochs = 5

for e in range(epochs):

# 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

# 輸入值需要避免0,輸出值需要避免1

inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01

# create the target output values (all 0.01, except the desired 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

pass

# load the mnist test data CSV file into a list

test_data_file = open("mnist_dataset/mnist_test.csv", 'r')

test_data_list = test_data_file.readlines()

test_data_file.close()

# test the neural network

# scorecard for how well the network performs, initially empty

scorecard = []

# go through all the records in the test data set

for record in test_data_list:

# split the record by the ',' commas

all_values = record.split(',')

# correct answer is first value

correct_label = int(all_values[0])

# scale and shift the inputs

inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01

# query the network

outputs = n.query(inputs)

# the index of the highest value corresponds to the label

label = numpy.argmax(outputs)

# append correct or incorrect to list

if (label == correct_label):

# network's answer matches correct answer, add 1 to scorecard

scorecard.append(1)

else:

# network's answer doesn't match correct answer, add 0 to scorecard

scorecard.append(0)

pass

pass

# calculate the performance score, the fraction of correct answers

scorecard_array = numpy.asarray(scorecard)

print ("performance = ", scorecard_array.sum() / scorecard_array.size)

# performance = 0.9712

總結

以上是生活随笔為你收集整理的python的神经网络编程_Python神经网络编程 第二章 使用Python进行DIY的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。