- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python构建深度神经网络(续)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
这篇文章在前一篇文章:python构建深度神经网络(DNN)的基础上,添加了一下几个内容:
1) 正则化项 。
2) 调出中间损失函数的输出 。
3) 构建了交叉损失函数 。
4) 将训练好的网络进行保存,并调用用来测试新数据 。
1 数据预处理 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017-03-12 15:11
# @Author : CC
# @File : net_load_data.py
from
numpy
import
*
import
numpy as np
import
cPickle
def
load_data():
"""载入解压后的数据,并读取"""
with
open
(
'data/mnist_pkl/mnist.pkl'
,
'rb'
) as f:
try
:
train_data,validation_data,test_data
=
cPickle.load(f)
print
" the file open sucessfully"
# print train_data[0].shape #(50000,784)
# print train_data[1].shape #(50000,)
return
(train_data,validation_data,test_data)
except
EOFError:
print
'the file open error'
return
None
def
data_transform():
"""将数据转化为计算格式"""
t_d,va_d,te_d
=
load_data()
# print t_d[0].shape # (50000,784)
# print te_d[0].shape # (10000,784)
# print va_d[0].shape # (10000,784)
# n1 = [np.reshape(x,784,1) for x in t_d[0]] # 将5万个数据分别逐个取出化成(784,1),逐个排列
n
=
[np.reshape(x, (
784
,
1
))
for
x
in
t_d[
0
]]
# 将5万个数据分别逐个取出化成(784,1),逐个排列
# print 'n1',n1[0].shape
# print 'n',n[0].shape
m
=
[vectors(y)
for
y
in
t_d[
1
]]
# 将5万标签(50000,1)化为(10,50000)
train_data
=
zip
(n,m)
# 将数据与标签打包成元组形式
n
=
[np.reshape(x, (
784
,
1
))
for
x
in
va_d[
0
]]
# 将5万个数据分别逐个取出化成(784,1),排列
validation_data
=
zip
(n,va_d[
1
])
# 没有将标签数据矢量化
n
=
[np.reshape(x, (
784
,
1
))
for
x
in
te_d[
0
]]
# 将5万个数据分别逐个取出化成(784,1),排列
test_data
=
zip
(n, te_d[
1
])
# 没有将标签数据矢量化
# print train_data[0][0].shape #(784,)
# print "len(train_data[0])",len(train_data[0]) #2
# print "len(train_data[100])",len(train_data[100]) #2
# print "len(train_data[0][0])", len(train_data[0][0]) #784
# print "train_data[0][0].shape", train_data[0][0].shape #(784,1)
# print "len(train_data)", len(train_data) #50000
# print train_data[0][1].shape #(10,1)
# print test_data[0][1] # 7
return
(train_data,validation_data,test_data)
def
vectors(y):
"赋予标签"
label
=
np.zeros((
10
,
1
))
label[y]
=
1.0
#浮点计算
return
label
|
2 网络定义和训练 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017-03-28 10:18
# @Author : CC
# @File : net_network2.py
from
numpy
import
*
import
numpy as np
import
operator
import
json
# import sys
class
QuadraticCost():
"""定义二次代价函数类的方法"""
@staticmethod
def
fn(a,y):
cost
=
0.5
*
np.linalg.norm(a
-
y)
*
*
2
return
cost
@staticmethod
def
delta(z,a,y):
delta
=
(a
-
y)
*
sig_derivate(z)
return
delta
class
CrossEntroyCost():
"""定义交叉熵函数类的方法"""
@staticmethod
def
fn(a, y):
cost
=
np.
sum
(np.nan_to_num(
-
y
*
np.log(a)
-
(
1
-
y)
*
np.log(
1
-
a)))
# not a number---0, inf---larger number
return
cost
@staticmethod
def
delta(z, a, y):
delta
=
(a
-
y)
return
delta
class
Network(
object
):
"""定义网络结构和方法"""
def
__init__(
self
,sizes,cost):
self
.num_layer
=
len
(sizes)
self
.sizes
=
sizes
self
.cost
=
cost
# print "self.cost.__name__:",self.cost.__name__ # CrossEntropyCost
self
.default_weight_initializer()
def
default_weight_initializer(
self
):
"""权值初始化"""
self
.bias
=
[np.random.rand(x,
1
)
for
x
in
self
.sizes[
1
:]]
self
.weight
=
[np.random.randn(y, x)
/
float
(np.sqrt(x))
for
(x, y)
in
zip
(
self
.sizes[:
-
1
],
self
.sizes[
1
:])]
def
large_weight_initializer(
self
):
"""权值另一种初始化"""
self
.bias
=
[np.random.rand(x,
1
)
for
x
in
self
.sizes[
1
:]]
self
.weight
=
[np.random.randn(y, x)
for
x, y
in
zip
(
self
.sizes[:
-
1
],
self
.sizes[
1
:])]
def
forward(
self
,a):
"""forward the network"""
for
w,b
in
zip
(
self
.weight,
self
.bias):
a
=
sigmoid(np.dot(w,a)
+
b)
return
a
def
SGD(
self
,train_data,min_batch_size,epochs,eta,test_data
=
False
,
lambd
=
0
,
monitor_train_cost
=
False
,
monitor_train_accuracy
=
False
,
monitor_test_cost
=
False
,
monitor_test_accuracy
=
False
):
"""1)Set the train_data,shuffle;
2) loop the epoches,
3) set the min_batches,and rule of update"""
if
test_data: n_test
=
len
(test_data)
n
=
len
(train_data)
for
i
in
xrange
(epochs):
random.shuffle(train_data)
min_batches
=
[train_data[k:k
+
min_batch_size]
for
k
in
xrange
(
0
,n,min_batch_size)]
for
min_batch
in
min_batches:
# 每次提取一个批次的样本
self
.update_minbatch_parameter(min_batch,eta,lambd,n)
train_cost
=
[]
if
monitor_train_cost:
cost1
=
self
.total_cost(train_data,lambd,cont
=
False
)
train_cost.append(cost1)
print
"epoche {0},train_cost: {1}"
.
format
(i,cost1)
if
monitor_train_accuracy:
accuracy
=
self
.accuracy(train_data,cont
=
True
)
train_cost.append(accuracy)
print
"epoche {0}/{1},train_accuracy: {2}"
.
format
(i,epochs,accuracy)
test_cost
=
[]
if
monitor_test_cost:
cost1
=
self
.total_cost(test_data,lambd)
test_cost.append(cost1)
print
"epoche {0},test_cost: {1}"
.
format
(i,cost1)
test_accuracy
=
[]
if
monitor_test_accuracy:
accuracy
=
self
.accuracy(test_data)
test_cost.append(accuracy)
print
"epoche:{0}/{1},test_accuracy:{2}"
.
format
(i,epochs,accuracy)
self
.save(filename
=
"net_save"
)
#保存网络网络参数
def
total_cost(
self
,train_data,lambd,cont
=
True
):
cost1
=
0.0
for
x,y
in
train_data:
a
=
self
.forward(x)
if
cont: y
=
vectors(y)
#将测试样本标签化为矩阵
cost1
+
=
(
self
.cost).fn(a,y)
/
len
(train_data)
cost1
+
=
lambd
/
len
(train_data)
*
np.
sum
(np.linalg.norm(weight)
*
*
2
for
weight
in
self
.weight)
#加上权值项
return
cost1
def
accuracy(
self
,train_data,cont
=
False
):
if
cont:
output1
=
[(np.argmax(
self
.forward(x)),np.argmax(y))
for
(x,y)
in
train_data]
else
:
output1
=
[(np.argmax(
self
.forward(x)), y)
for
(x, y)
in
train_data]
return
sum
(
int
(out1
=
=
y)
for
(out1, y)
in
output1)
def
update_minbatch_parameter(
self
,min_batch, eta,lambd,n):
"""1) determine the weight and bias
2) calculate the the delta
3) update the data """
able_b
=
[np.zeros(b.shape)
for
b
in
self
.bias]
able_w
=
[np.zeros(w.shape)
for
w
in
self
.weight]
for
x,y
in
min_batch:
#每次只取一个样本?
deltab,deltaw
=
self
.backprop(x,y)
able_b
=
[a_b
+
dab
for
a_b, dab
in
zip
(able_b,deltab)]
#实际上对dw,db做批次累加,最后小批次取平均
able_w
=
[a_w
+
daw
for
a_w, daw
in
zip
(able_w, deltaw)]
self
.weight
=
[weight
-
eta
*
(dw)
/
len
(min_batch)
-
eta
*
(lambd
*
weight)
/
n
for
weight, dw
in
zip
(
self
.weight,able_w) ]
#增加正则化项:eta*lambda/m *weight
self
.bias
=
[bias
-
eta
*
db
/
len
(min_batch)
for
bias, db
in
zip
(
self
.bias, able_b)]
def
backprop(
self
,x,y):
"""" 1) clacu the forward value
2) calcu the delta: delta =(y-f(z)); deltak = delta*w(k)*fz(k-1)'
3) clacu the delta in every layer: deltab=delta; deltaw=delta*fz(k-1)"""
deltab
=
[np.zeros(b.shape)
for
b
in
self
.bias]
deltaw
=
[np.zeros(w.shape)
for
w
in
self
.weight]
zs
=
[]
activate
=
x
activates
=
[x]
for
w,b
in
zip
(
self
.weight,
self
.bias):
z
=
np.dot(w, activate)
+
b
zs.append(z)
activate
=
sigmoid(z)
activates.append(activate)
# backprop
delta
=
self
.cost.delta(zs[
-
1
],activates[
-
1
],y)
#调用不同代价函数的方法求梯度
deltab[
-
1
]
=
delta
deltaw[
-
1
]
=
np.dot(delta ,activates[
-
2
].transpose())
for
i
in
xrange
(
2
,
self
.num_layer):
z
=
zs[
-
i]
delta
=
np.dot(
self
.weight[
-
i
+
1
].transpose(),delta)
*
sig_derivate(z)
deltab[
-
i]
=
delta
deltaw[
-
i]
=
np.dot(delta,activates[
-
i
-
1
].transpose())
return
(deltab,deltaw)
def
save(
self
,filename):
"""将训练好的网络采用json(java script object notation)将对象保存成字符串保存,用于生产部署
encoder=json.dumps(data)
python 原始类型(没有数组类型)向 json 类型的转化对照表:
python json
dict object
list/tuple arrary
int/long/float number
.tolist() 将数组转化为列表
>>> a = np.array([[1, 2], [3, 4]])
>>> list(a)
[array([1, 2]), array([3, 4])]
>>> a.tolist()
[[1, 2], [3, 4]]
"""
data
=
{
"sizes"
:
self
.sizes,
"weight"
: [weight.tolist()
for
weight
in
self
.weight],
"bias"
: ([bias.tolist()
for
bias
in
self
.bias]),
"cost"
:
str
(
self
.cost.__name__)}
# 保存网络训练好的权值,偏置,交叉熵参数。
f
=
open
(filename,
"w"
)
json.dump(data,f)
f.close()
def
load_net(filename):
"""采用data=json.load(json.dumps(data))进行解码,
decoder = json.load(encoder)
编码后和解码后键不会按照原始data的键顺序排列,但每个键对应的值不会变
载入训练好的网络用于测试"""
f
=
open
(filename,
"r"
)
data
=
json.load(f)
f.close()
# print "data[cost]", getattr(sys.modules[__name__], data["cost"])#获得属性__main__.CrossEntropyCost
# print "data[cost]", data["cost"], data["sizes"]
net
=
Network(data[
"sizes"
], cost
=
data[
"cost"
])
#网络初始化
net.weight
=
[np.array(w)
for
w
in
data[
"weight"
]]
#赋予训练好的权值,并将list--->array
net.bias
=
[np.array(b)
for
b
in
data[
"bias"
]]
return
net
def
sig_derivate(z):
"""derivate sigmoid"""
return
sigmoid(z)
*
(
1
-
sigmoid(z))
def
sigmoid(x):
sigm
=
1.0
/
(
1.0
+
exp(
-
x))
return
sigm
def
vectors(y):
"""赋予标签"""
label
=
np.zeros((
10
,
1
))
label[y]
=
1.0
#浮点计算
return
label
|
3) 网络测试 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017-03-12 15:24
# @Author : CC
# @File : net_test.py
import
net_load_data
# net_load_data.load_data()
train_data,validation_data,test_data
=
net_load_data.data_transform()
import
net_network2 as net
cost
=
net.QuadraticCost
cost
=
net.CrossEntroyCost
lambd
=
0
net1
=
net.Network([
784
,
50
,
10
],cost)
min_batch_size
=
30
eta
=
3.0
epoches
=
2
net1.SGD(train_data,min_batch_size,epoches,eta,test_data,
lambd,
monitor_train_cost
=
True
,
monitor_train_accuracy
=
True
,
monitor_test_cost
=
True
,
monitor_test_accuracy
=
True
)
print
"complete"
|
4 调用训练好的网络进行测试 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017-03-28 17:27
# @Author : CC
# @File : forward_test.py
import
numpy as np
# 对训练好的网络直接进行调用,并用测试样本进行测试
import
net_load_data
#导入测试数据
import
net_network2 as net
train_data,validation_data,test_data
=
net_load_data.data_transform()
net
=
net.load_net(filename
=
"net_save"
)
#导入网络
output
=
[(np.argmax(net.forward(x)),y)
for
(x,y)
in
test_data]
#测试
print
sum
(
int
(y1
=
=
y2)
for
(y1,y2)
in
output)
#输出最终值
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/Ychan_cc/article/details/67640185 。
最后此篇关于python构建深度神经网络(续)的文章就讲到这里了,如果你想了解更多关于python构建深度神经网络(续)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!