Skip to content

Commit 281ba9e

Browse files
author
tanzhongyi
authored
Merge pull request BaiduOSS#6 from tanzhongyibidu/master
update to fix style problem
2 parents af6dcbb + 84aa95c commit 281ba9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1642
-1912
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
1+
Copyright (c) 2018 Baidu Inc. All Rights Reserved
22

33
Apache License
44
Version 2.0, January 2004

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33

44
it includes
5-
* lesson1 python basic
6-
* lesson2 paddle basic
7-
* lesson3 logistic classification with numpy and paddle
8-
* lesson4 shallow network with numpy and paddle
9-
* lesson5 deep network with numpy and paddle
10-
* lesson6 mnist with paddle
11-
* lesson7 recommend system with paddle
12-
* lesson8 paddlecloud
13-
* lesson9 ctr
14-
* lesson10 tuning
5+
* lesson1 Python basic
6+
* lesson2 PaddlePaddle basic
7+
* lesson3 Logistic classification with numpy and paddle
8+
* lesson4 Shallow network with numpy and paddle
9+
* lesson5 Deep network with numpy and paddle
10+
* lesson6 Mnist with paddle
11+
* lesson7 Recommend system with paddle
12+
* lesson8 PaddlePaddleCloud
13+
* lesson9 CTR
14+
* lesson10 Parameter tuning

lesson1/array_module.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
#!/usr/bin/env python2.7
2-
# -*- encoding:utf-8 -*-
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
34
"""
45
Array 基础操作脚本
56
Created on 2017-11-16
6-
@author: denglelai@baidu.com
7-
@copyright: www.baidu.com
7+
author: denglelai
88
"""
9+
910
import numpy as np
1011

1112

12-
# a是python中的list类型
13-
a = [1, 2, 3, 4]
14-
# 数组化之后b的类型变为 array
15-
b = np.array(a)
16-
# a的类型 <type 'list'>
17-
print "原始的类型为:" + str(type(a))
18-
# b的类型 <type 'numpy.ndarray'>
19-
print "数组化之后的类型为:" + str(type(b))
20-
# shape参数表示array的大小,这里是(4,)
21-
print "Array的大小为:" + str(b.shape)
22-
# 调用argmax()函数可以求得array中的最大值的索引,这里是3
23-
print "Array中最大元素索引为:" + str(b.argmax())
24-
# 调用max()函数可以求得array中的最大值,这里是4
25-
print "Array中最大元素值为:" + str(b.max())
26-
# 调用mean()函数可以求得array中的平均值,这里是2.5
27-
print "Array中元素平均值为:" + str(b.mean())
13+
def main():
14+
"""
15+
show np.array related operations
16+
"""
17+
# a是python中的list类型
18+
a = [1, 2, 3, 4]
19+
# 数组化之后b的类型变为 array
20+
b = np.array(a)
21+
# a的类型 <type 'list'>
22+
print "原始的类型为:" + str(type(a))
23+
# b的类型 <type 'numpy.ndarray'>
24+
print "数组化之后的类型为:" + str(type(b))
25+
# shape参数表示array的大小,这里是(4,)
26+
print "Array的大小为:" + str(b.shape)
27+
# 调用argmax()函数可以求得array中的最大值的索引,这里是3
28+
print "Array中最大元素索引为:" + str(b.argmax())
29+
# 调用max()函数可以求得array中的最大值,这里是4
30+
print "Array中最大元素值为:" + str(b.max())
31+
# 调用mean()函数可以求得array中的平均值,这里是2.5
32+
print "Array中元素平均值为:" + str(b.mean())
33+
34+
35+
if __name__ == '__main__':
36+
main()

lesson1/basic_calculate.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,59 @@
1-
#!/usr/bin/env python2.7
2-
# -*- encoding:utf-8 -*-
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
34
"""
45
Numpy中实现基本数学计算脚本
56
Created on 2017-11-16
6-
@author: denglelai@baidu.com
7-
@copyright: www.baidu.com
7+
author: denglelai
88
"""
99
import numpy as np
1010

1111

12-
# 绝对值,1
13-
a = np.abs(-1)
14-
print "-1的绝对值为:" + str(a)
12+
def main():
13+
"""
14+
run basic operations of numpy
15+
"""
16+
17+
# 绝对值,1
18+
a_variable = np.abs(-1)
19+
print "-1的绝对值为:" + str(a_variable)
20+
21+
# sin函数,1.0
22+
a_variable = np.sin(np.pi / 2)
23+
print "pi/2的正弦值为:" + str(a_variable)
24+
25+
# tanh逆函数,0.500001071578
26+
a_variable = np.arctanh(0.462118)
27+
print "tanh(0.462118)值为:" + str(a_variable)
1528

16-
# sin函数,1.0
17-
b = np.sin(np.pi / 2)
18-
print "pi/2的正弦值为:" + str(b)
29+
# e为底的指数函数,20.0855369232
30+
a_variable = np.exp(3)
31+
print "e的3次方值为:" + str(a_variable)
1932

20-
# tanh逆函数,0.50000107157840523
21-
c = np.arctanh(0.462118)
22-
print "tanh(0.462118)值为:" + str(c)
33+
# 2的3次方,8
34+
a_variable = np.power(2, 3)
35+
print "2的3次方值为:" + str(a_variable)
2336

24-
# e为底的指数函数,20.085536923187668
25-
d = np.exp(3)
26-
print "e的3次方值为:" + str(d)
37+
# 点积,1*3+2*4=11
38+
a_variable = np.dot([1, 2], [3, 4])
39+
print "向量[1. 2]与向量[3. 4]点乘值为:" + str(a_variable)
2740

28-
# 2的3次方,8
29-
f = np.power(2, 3)
30-
print "2的3次方值为:" + str(f)
41+
# 开方,5
42+
a_variable = np.sqrt(25)
43+
print "25的2次方根值为:" + str(a_variable)
3144

32-
# 点积,1*3+2*4=11
33-
g = np.dot([1, 2], [3, 4])
34-
print "向量[1. 2]与向量[3. 4]点乘值为:" + str(g)
45+
# 求和,10
46+
a_variable = np.sum([1, 2, 3, 4])
47+
print "对[1, 2, 3, 4]中元素求和结果为:" + str(a_variable)
3548

36-
# 开方,5
37-
h = np.sqrt(25)
38-
print "25的2次方根值为:" + str(h)
49+
# 平均值,5.5
50+
a_variable = np.mean([4, 5, 6, 7])
51+
print "对[1, 2, 3, 4]中元素求平均结果为:" + str(a_variable)
3952

40-
# 求和,10
41-
l = np.sum([1, 2, 3, 4])
42-
print "对[1, 2, 3, 4]中元素求和结果为:" + str(l)
53+
# 标准差,0.968245836552
54+
a_variable = np.std([1, 2, 3, 2, 1, 3, 2, 0])
55+
print "对[1, 2, 3, 2, 1, 3, 2, 0]中元素求标准差结果为:" + str(a_variable)
4356

44-
# 平均值,5.5
45-
m = np.mean([4, 5, 6, 7])
46-
print "对[1, 2, 3, 4]中元素求平均结果为:" + str(m)
4757

48-
# 标准差,0.96824583655185426
49-
p = np.std([1, 2, 3, 2, 1, 3, 2, 0])
50-
print "对[1, 2, 3, 2, 1, 3, 2, 0]中元素求标准差结果为:" + str(p)
58+
if __name__ == '__main__':
59+
main()

lesson1/broadcast.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
#!/usr/bin/env python2.7
2-
# -*- encoding:utf-8 -*-
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
34
"""
45
Numpy中实现广播机制说明脚本
56
Created on 2017-11-16
6-
@author: denglelai@baidu.com
7-
@copyright: www.baidu.com
7+
author: denglelai
88
"""
99
import numpy as np
1010

1111

12-
a = np.array([
13-
[1, 2, 3],
14-
[4, 5, 6]
15-
])
16-
17-
b = np.array([
18-
[1, 2, 3],
19-
[1, 2, 3]
20-
])
21-
22-
'''
23-
维度一样的array,对位计算
24-
array([[2, 4, 6],
25-
[5, 7, 9]])
26-
'''
27-
print "相同维度array, 进行对位运算, 结果为:\n" + str(a + b)
28-
29-
c = np.array([
30-
[1, 2, 3],
31-
[4, 5, 6],
32-
[7, 8, 9],
33-
[10, 11, 12]
34-
])
35-
d = np.array([2, 2, 2])
36-
37-
'''
38-
广播机制让计算的表达式保持简洁
39-
d和c的每一行分别进行运算
40-
array([[ 3, 4, 5],
41-
[ 6, 7, 8],
42-
[ 9, 10, 11],
43-
[12, 13, 14]])
44-
'''
45-
print "广播机制下, c和d进行每一行分别计算, 结果为:\n" + str(c + d)
12+
def main():
13+
"""
14+
show broadcast operation in numpy
15+
"""
16+
array_a = np.array([
17+
[1, 2, 3],
18+
[4, 5, 6]
19+
])
20+
21+
array_b = np.array([
22+
[1, 2, 3],
23+
[1, 2, 3]
24+
])
25+
26+
# 维度一样的array,对位计算
27+
# array([[2, 4, 6],
28+
# [5, 7, 9]])
29+
30+
print "相同维度array, 进行对位运算, 结果为:\n" + str(array_a + array_b)
31+
32+
array_c = np.array([
33+
[1, 2, 3],
34+
[4, 5, 6],
35+
[7, 8, 9],
36+
[10, 11, 12]
37+
])
38+
39+
array_d = np.array([2, 2, 2])
40+
# 广播机制让计算的表达式保持简洁
41+
# array_d和array_c的每一行分别进行运算
42+
# array([[ 3, 4, 5],
43+
# [ 6, 7, 8],
44+
# [ 9, 10, 11],
45+
# [12, 13, 14]])
46+
print "广播机制下, c和d进行每一行分别计算, 结果为:\n" + str(array_c + array_d)
47+
48+
49+
if __name__ == '__main__':
50+
main()

lesson1/compare_efficiency.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1-
#!/usr/bin/env python2.7
2-
# -*- encoding:utf-8 -*-
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
34
"""
45
Numpy中实现向量化与非向量化计算效率比较脚本
56
Created on 2017-11-16
6-
@author: denglelai@baidu.com
7-
@copyright: www.baidu.com
7+
author: denglelai
88
"""
9-
import numpy as np
109
import time
10+
import numpy as np
11+
12+
13+
def main():
14+
"""
15+
show two version of vector operation
16+
"""
17+
18+
# 初始化两个1000000维的随机向量v1,v2用于矩阵相乘计算
19+
vector_1 = np.random.rand(1000000)
20+
vector_2 = np.random.rand(1000000)
21+
result = 0
22+
23+
# 矩阵相乘-非向量化版本
24+
tic = time.time()
25+
for i in range(1000000):
26+
result = result + vector_1[i] * vector_2[i]
27+
toc = time.time()
28+
print "非向量化-计算结果:" + str(result)
29+
print "非向量化-计算时间:" + str((toc - tic) * 1000) + "ms" + "\n"
30+
31+
# 矩阵相乘-向量化版本
32+
tic = time.time()
33+
result = np.dot(vector_1, vector_2)
34+
toc = time.time()
35+
print "向量化-计算结果:" + str(result)
36+
print "向量化-计算时间:" + str((toc - tic) * 1000)+"ms"
1137

1238

13-
#初始化两个1000000维的随机向量v1,v2用于矩阵相乘计算
14-
v1 = np.random.rand(1000000)
15-
v2 = np.random.rand(1000000)
16-
v = 0
17-
18-
#矩阵相乘-非向量化版本
19-
tic = time.time()
20-
for i in range(1000000):
21-
v = v + v1[i] * v2[i]
22-
toc = time.time()
23-
print "非向量化-计算结果:" + str(v) + "ms"
24-
print "非向量化-计算时间:" + str((toc - tic) * 1000) + "ms" + "\n"
25-
26-
#矩阵相乘-向量化版本
27-
tic = time.time()
28-
v = np.dot(v1, v2)
29-
toc = time.time()
30-
print "向量化-计算结果:" + str(v) + "ms"
31-
print "向量化-计算时间:" + str((toc - tic) * 1000)+"ms"
39+
if __name__ == '__main__':
40+
main()

0 commit comments

Comments
 (0)