Skip to content

Commit 95d0dc6

Browse files
authored
Add files via upload
1 parent 2707576 commit 95d0dc6

File tree

15 files changed

+1430
-0
lines changed

15 files changed

+1430
-0
lines changed

NeuralNetwork_Project1/MLP.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Aug 14 22:21:25 2018
4+
5+
@author: wzy
6+
"""
7+
"""
8+
# =============神经网络用于分类=============
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
from sklearn.neural_network import MLPClassifier
12+
from sklearn.preprocessing import StandardScaler
13+
data = [
14+
[-0.017612, 14.053064, 0],[-1.395634, 4.662541, 1],[-0.752157, 6.53862, 0],[-1.322371, 7.152853, 0],[0.423363, 11.054677, 0],
15+
[0.406704, 7.067335, 1],[0.667394, 12.741452, 0],[-2.46015, 6.866805, 1],[0.569411, 9.548755, 0],[-0.026632, 10.427743, 0],
16+
[0.850433, 6.920334, 1],[1.347183, 13.1755, 0],[1.176813, 3.16702, 1],[-1.781871, 9.097953, 0],[-0.566606, 5.749003, 1],
17+
[0.931635, 1.589505, 1],[-0.024205, 6.151823, 1],[-0.036453, 2.690988, 1],[-0.196949, 0.444165, 1],[1.014459, 5.754399, 1],
18+
[1.985298, 3.230619, 1],[-1.693453, -0.55754, 1],[-0.576525, 11.778922, 0],[-0.346811, -1.67873, 1],[-2.124484, 2.672471, 1],
19+
[1.217916, 9.597015, 0],[-0.733928, 9.098687, 0],[1.416614, 9.619232, 0],[1.38861, 9.341997, 0],[0.317029, 14.739025, 0]
20+
]
21+
dataMat = np.array(data)
22+
X = dataMat[:,0:2]
23+
y = dataMat[:,2]
24+
# 神经网络对数据尺度敏感,所以最好在训练前标准化,或者归一化,或者缩放到[-1,1]
25+
scaler = StandardScaler() # 标准化转换
26+
scaler.fit(X) # 训练标准化对象
27+
X = scaler.transform(X) # 转换数据集
28+
# solver='lbfgs', MLP的求解方法:L-BFGS 在小数据上表现较好,Adam 较为鲁棒,SGD在参数调整较优时会有最佳表现(分类效果与迭代次数);SGD标识随机梯度下降。
29+
# alpha:L2的参数:MLP是可以支持正则化的,默认为L2,具体参数需要调整
30+
# hidden_layer_sizes=(5, 2) hidden层2层,第一层5个神经元,第二层2个神经元),2层隐藏层,也就有3层神经网络
31+
32+
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5,2), random_state=1) # 神经网络输入为2,第一隐藏层神经元个数为5,第二隐藏层神经元个数为2,输出结果为2分类。
33+
clf.fit(X, y)
34+
print('每层网络层系数矩阵维度:\n',[coef.shape for coef in clf.coefs_])
35+
y_pred = clf.predict([[0.317029, 14.739025]])
36+
print('预测结果:',y_pred)
37+
y_pred_pro =clf.predict_proba([[0.317029, 14.739025]])
38+
print('预测结果概率:\n',y_pred_pro)
39+
40+
cengindex = 0
41+
for wi in clf.coefs_:
42+
cengindex += 1 # 表示底第几层神经网络。
43+
print('第%d层网络层:' % cengindex)
44+
print('权重矩阵维度:',wi.shape)
45+
print('系数矩阵:\n',wi)
46+
47+
# 绘制分割区域
48+
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 # 寻找每个维度的范围
49+
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 # 寻找每个维度的范围
50+
xx1, xx2 = np.meshgrid(np.arange(x_min, x_max, 0.01),np.arange(y_min, y_max,0.01)) # 在特征范围以0.01位步长预测每一个点的输出结果
51+
Z = clf.predict(np.c_[xx1.ravel(), xx2.ravel()]) # 先形成待测样本的形式,在通过模型进行预测。
52+
Z = Z.reshape(xx1.shape) # 将输出结果转换为和网格的矩阵形式,以便绘图
53+
# 绘制区域网格图
54+
plt.pcolormesh(xx1, xx2, Z, cmap=plt.cm.Paired)
55+
# 绘制样本点
56+
plt.scatter(X[:,0],X[:,1],c=y)
57+
plt.show()
58+
59+
"""
60+
# # =============神经网络用于回归=============
61+
62+
import numpy as np
63+
from sklearn.neural_network import MLPRegressor # 多层线性回归
64+
from sklearn.preprocessing import StandardScaler
65+
data = [
66+
[ -0.017612,14.053064,14.035452],[ -1.395634, 4.662541, 3.266907],[ -0.752157, 6.53862,5.786463],[ -1.322371, 7.152853, 5.830482],
67+
[0.423363,11.054677,11.47804 ],[0.406704, 7.067335, 7.474039],[0.667394,12.741452,13.408846],[ -2.46015,6.866805, 4.406655],
68+
[0.569411, 9.548755,10.118166],[ -0.026632,10.427743,10.401111],[0.850433, 6.920334, 7.770767],[1.347183,13.1755,14.522683],
69+
[1.176813, 3.16702,4.343833],[ -1.781871, 9.097953, 7.316082],[ -0.566606, 5.749003, 5.182397],[0.931635, 1.589505, 2.52114 ],
70+
[ -0.024205, 6.151823, 6.127618],[ -0.036453, 2.690988, 2.654535],[ -0.196949, 0.444165, 0.247216],[1.014459, 5.754399, 6.768858],
71+
[1.985298, 3.230619, 5.215917],[ -1.693453,-0.55754, -2.250993],[ -0.576525,11.778922,11.202397],[ -0.346811,-1.67873, -2.025541],
72+
[ -2.124484, 2.672471, 0.547987],[1.217916, 9.597015,10.814931],[ -0.733928, 9.098687, 8.364759],[1.416614, 9.619232,11.035846],
73+
[1.38861,9.341997,10.730607],[0.317029,14.739025,15.056054]
74+
]
75+
76+
dataMat = np.array(data)
77+
X=dataMat[:,0:2]
78+
y = dataMat[:,2]
79+
scaler = StandardScaler() # 标准化转换
80+
scaler.fit(X) # 训练标准化对象
81+
X = scaler.transform(X) # 转换数据集
82+
83+
# solver='lbfgs', MLP的求解方法:L-BFGS 在小数据上表现较好,Adam 较为鲁棒,SGD在参数调整较优时会有最佳表现(分类效果与迭代次数);SGD标识随机梯度下降。
84+
# alpha:L2的参数:MLP是可以支持正则化的,默认为L2,具体参数需要调整
85+
# hidden_layer_sizes=(5, 2) hidden层2层,第一层5个神经元,第二层2个神经元),2层隐藏层,也就有3层神经网络
86+
clf = MLPRegressor(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
87+
clf.fit(X, y)
88+
print('预测结果:', clf.predict([[0.317029, 14.739025]])) # 预测某个输入对象
89+
90+
cengindex = 0
91+
for wi in clf.coefs_:
92+
cengindex += 1 # 表示底第几层神经网络。
93+
print('第%d层网络层:' % cengindex)
94+
print('权重矩阵维度:',wi.shape)
95+

NeuralNetwork_Project1/NN.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Aug 13 16:25:47 2018
4+
神经网络通过调整隐藏节点数、世代数以及学习率改善训练结果
5+
hidden_nodes、epochs、learning_rate
6+
7+
@author: wzy
8+
"""
9+
import numpy as np
10+
# scipy.special for the sigmoid function expit()
11+
import scipy.special
12+
import matplotlib.pyplot as plt
13+
14+
"""
15+
类说明:构建神经网络
16+
17+
Parameters:
18+
None
19+
20+
Returns:
21+
None
22+
23+
Modify:
24+
2018-08-13
25+
"""
26+
class neuralNetwork:
27+
# 神经网络的构造函数
28+
# 初始化函数——设定输入层节点、隐藏层节点和输出层节点的数量。
29+
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
30+
# 输入节点、隐藏节点、输出节点、学习率
31+
self.inodes = inputnodes
32+
self.hnodes = hiddennodes
33+
self.onodes = outputnodes
34+
self.lr = learningrate
35+
# 通过wih和who链接权重矩阵
36+
# weights inside the arrays are w_i_j,where link is from node i to node j in the next layer
37+
# 使用正态概率分布采样权重,平均值为0.0,标准方差为节点传入链接数目的开方,即1/sqrt(传入链接数目)
38+
self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
39+
self.who = np.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))
40+
# activation function is the sigmoid function
41+
self.activation_function = lambda x: scipy.special.expit(x)
42+
pass
43+
44+
# 神经网络的训练函数
45+
# 训练——学习给定训练集样本后,优化权重。
46+
def train(self, inputs_list, targets_list):
47+
# convert inputs list to 2d array
48+
inputs = np.array(inputs_list, ndmin=2).T
49+
targets = np.array(targets_list, ndmin=2).T
50+
# calculate signals into hidden layer
51+
hidden_inputs = np.dot(self.wih, inputs)
52+
# calculate the signals emerging from hidden layer
53+
hidden_outputs = self.activation_function(hidden_inputs)
54+
# calculate signals into final output layer
55+
final_inputs = np.dot(self.who, hidden_outputs)
56+
# calculate the signals emerging from final output layer
57+
final_outputs = self.activation_function(final_inputs)
58+
# error is the (target - actual)
59+
output_errors = targets - final_outputs
60+
# hidden layer error is the output_errors, split by weights, recombined at hidden nodes
61+
hidden_errors = np.dot(self.who.T, output_errors)
62+
# update the weights for the links between the hidden and output layers
63+
self.who += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden_outputs))
64+
# update the weights for the links between the input and hidden layers
65+
self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))
66+
pass
67+
68+
# 神经网络的查询函数
69+
# 查询——给定输入,从输出节点给出答案。
70+
def query(self, inputs_list):
71+
# convert inputs list to 2d array
72+
inputs = np.array(inputs_list, ndmin=2).T
73+
# calculate signals into hidden layer
74+
hidden_inputs = np.dot(self.wih, inputs)
75+
# calculate the signals emerging from hidden layer
76+
hidden_outputs = self.activation_function(hidden_inputs)
77+
# calculate signals into final output layer
78+
final_inputs = np.dot(self.who, hidden_outputs)
79+
# calculate the signals emerging from final output layer
80+
final_outputs = self.activation_function(final_inputs)
81+
return final_outputs
82+
83+
84+
if __name__ == '__main__':
85+
input_nodes = 784
86+
hidden_nodes = 200
87+
output_nodes = 10
88+
learning_rate = 0.2
89+
# 进行两轮训练
90+
epochs = 5
91+
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
92+
# 导入训练数据
93+
training_data_file = open('mnist_dataset/mnist_train_100.csv', 'r')
94+
training_data_list = training_data_file.readlines()
95+
training_data_file.close()
96+
# 数据可视化处理
97+
for e in range(epochs):
98+
for record in training_data_list:
99+
all_values = record.split(',')
100+
# 将数据进行归一化处理,落在区间[0.01, 1.0]内
101+
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
102+
# np.asfarray()将文本字符串转换成实数,并创建这些数字的数组
103+
# image_array = np.asfarray(all_values[1:]).reshape((28, 28))
104+
# output nodes is 10(example)
105+
targets = np.zeros(output_nodes) + 0.01
106+
# all_values[0] is the target label for this record
107+
targets[int(all_values[0])] = 0.99
108+
# cmap='Greys'灰度图
109+
# plt.imshow(image_array, cmap='Greys', interpolation='None')
110+
n.train(inputs, targets)
111+
# 导入测试数据
112+
test_data_file = open('mnist_dataset/mnist_test_10.csv', 'r')
113+
test_data_list = test_data_file.readlines()
114+
test_data_file.close()
115+
# scorecard for how well the network performs, initially empty
116+
scorecard = []
117+
# go through all the records in the test data set
118+
for record in test_data_list:
119+
all_values = record.split(',')
120+
correct_label = int(all_values[0])
121+
print(correct_label, 'correct label')
122+
# 将数据进行归一化处理,落在区间[0.01, 1.0]内
123+
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
124+
# 查询神经网络
125+
outputs = n.query(inputs)
126+
# np.argmax()发现数组中的最大值,并告诉我们它的位置
127+
label = np.argmax(outputs)
128+
print(label, "network's answer")
129+
if (label == correct_label):
130+
scorecard.append(1)
131+
else:
132+
scorecard.append(0)
133+
scorecard_array = np.asarray(scorecard)
134+
print("performance = ", scorecard_array.sum() / scorecard_array.size)
135+
# print(all_values[0])
136+
# image_array = np.asfarray(all_values[1:]).reshape((28, 28))
137+
# plt.imshow(image_array, cmap='Greys', interpolation='None')
138+
# print(n.query((np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These are small subsets of the MNIST data set, transformed into CSV, and made available for easy testing as your code develops.
2+
3+
The full dataset in CSV format is available at: http://pjreddie.com/projects/mnist-in-csv/

0 commit comments

Comments
 (0)