Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Copyright (c) 2018 Baidu Inc. All Rights Reserved

Apache License
Version 2.0, January 2004
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


it includes
* lesson1 python basic
* lesson2 paddle basic
* lesson3 logistic classification with numpy and paddle
* lesson4 shallow network with numpy and paddle
* lesson5 deep network with numpy and paddle
* lesson6 mnist with paddle
* lesson7 recommend system with paddle
* lesson8 paddlecloud
* lesson9 ctr
* lesson10 tuning
* lesson1 Python basic
* lesson2 PaddlePaddle basic
* lesson3 Logistic classification with numpy and paddle
* lesson4 Shallow network with numpy and paddle
* lesson5 Deep network with numpy and paddle
* lesson6 Mnist with paddle
* lesson7 Recommend system with paddle
* lesson8 PaddlePaddleCloud
* lesson9 CTR
* lesson10 Parameter tuning
49 changes: 29 additions & 20 deletions lesson1/array_module.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
#!/usr/bin/env python2.7
# -*- encoding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
Array 基础操作脚本
Created on 2017-11-16
@author: denglelai@baidu.com
@copyright: www.baidu.com
author: denglelai
"""

import numpy as np


# a是python中的list类型
a = [1, 2, 3, 4]
# 数组化之后b的类型变为 array
b = np.array(a)
# a的类型 <type 'list'>
print "原始的类型为:" + str(type(a))
# b的类型 <type 'numpy.ndarray'>
print "数组化之后的类型为:" + str(type(b))
# shape参数表示array的大小,这里是(4,)
print "Array的大小为:" + str(b.shape)
# 调用argmax()函数可以求得array中的最大值的索引,这里是3
print "Array中最大元素索引为:" + str(b.argmax())
# 调用max()函数可以求得array中的最大值,这里是4
print "Array中最大元素值为:" + str(b.max())
# 调用mean()函数可以求得array中的平均值,这里是2.5
print "Array中元素平均值为:" + str(b.mean())
def main():
"""
show np.array related operations
"""
# a是python中的list类型
a = [1, 2, 3, 4]
# 数组化之后b的类型变为 array
b = np.array(a)
# a的类型 <type 'list'>
print "原始的类型为:" + str(type(a))
# b的类型 <type 'numpy.ndarray'>
print "数组化之后的类型为:" + str(type(b))
# shape参数表示array的大小,这里是(4,)
print "Array的大小为:" + str(b.shape)
# 调用argmax()函数可以求得array中的最大值的索引,这里是3
print "Array中最大元素索引为:" + str(b.argmax())
# 调用max()函数可以求得array中的最大值,这里是4
print "Array中最大元素值为:" + str(b.max())
# 调用mean()函数可以求得array中的平均值,这里是2.5
print "Array中元素平均值为:" + str(b.mean())


if __name__ == '__main__':
main()
77 changes: 43 additions & 34 deletions lesson1/basic_calculate.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
#!/usr/bin/env python2.7
# -*- encoding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
Numpy中实现基本数学计算脚本
Created on 2017-11-16
@author: denglelai@baidu.com
@copyright: www.baidu.com
author: denglelai
"""
import numpy as np


# 绝对值,1
a = np.abs(-1)
print "-1的绝对值为:" + str(a)
def main():
"""
run basic operations of numpy
"""

# 绝对值,1
a_variable = np.abs(-1)
print "-1的绝对值为:" + str(a_variable)

# sin函数,1.0
a_variable = np.sin(np.pi / 2)
print "pi/2的正弦值为:" + str(a_variable)

# tanh逆函数,0.500001071578
a_variable = np.arctanh(0.462118)
print "tanh(0.462118)值为:" + str(a_variable)

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

# tanh逆函数,0.50000107157840523
c = np.arctanh(0.462118)
print "tanh(0.462118)值为:" + str(c)
# 2的3次方,8
a_variable = np.power(2, 3)
print "2的3次方值为:" + str(a_variable)

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

# 2的3次方,8
f = np.power(2, 3)
print "2的3次方值为:" + str(f)
# 开方,5
a_variable = np.sqrt(25)
print "25的2次方根值为:" + str(a_variable)

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

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

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

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

# 标准差,0.96824583655185426
p = np.std([1, 2, 3, 2, 1, 3, 2, 0])
print "对[1, 2, 3, 2, 1, 3, 2, 0]中元素求标准差结果为:" + str(p)
if __name__ == '__main__':
main()
81 changes: 43 additions & 38 deletions lesson1/broadcast.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
#!/usr/bin/env python2.7
# -*- encoding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
Numpy中实现广播机制说明脚本
Created on 2017-11-16
@author: denglelai@baidu.com
@copyright: www.baidu.com
author: denglelai
"""
import numpy as np


a = np.array([
[1, 2, 3],
[4, 5, 6]
])

b = np.array([
[1, 2, 3],
[1, 2, 3]
])

'''
维度一样的array,对位计算
array([[2, 4, 6],
[5, 7, 9]])
'''
print "相同维度array, 进行对位运算, 结果为:\n" + str(a + b)

c = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
d = np.array([2, 2, 2])

'''
广播机制让计算的表达式保持简洁
d和c的每一行分别进行运算
array([[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
'''
print "广播机制下, c和d进行每一行分别计算, 结果为:\n" + str(c + d)
def main():
"""
show broadcast operation in numpy
"""
array_a = np.array([
[1, 2, 3],
[4, 5, 6]
])

array_b = np.array([
[1, 2, 3],
[1, 2, 3]
])

# 维度一样的array,对位计算
# array([[2, 4, 6],
# [5, 7, 9]])

print "相同维度array, 进行对位运算, 结果为:\n" + str(array_a + array_b)

array_c = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])

array_d = np.array([2, 2, 2])
# 广播机制让计算的表达式保持简洁
# array_d和array_c的每一行分别进行运算
# array([[ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11],
# [12, 13, 14]])
print "广播机制下, c和d进行每一行分别计算, 结果为:\n" + str(array_c + array_d)


if __name__ == '__main__':
main()
57 changes: 33 additions & 24 deletions lesson1/compare_efficiency.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
#!/usr/bin/env python2.7
# -*- encoding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
Numpy中实现向量化与非向量化计算效率比较脚本
Created on 2017-11-16
@author: denglelai@baidu.com
@copyright: www.baidu.com
author: denglelai
"""
import numpy as np
import time
import numpy as np


def main():
"""
show two version of vector operation
"""

# 初始化两个1000000维的随机向量v1,v2用于矩阵相乘计算
vector_1 = np.random.rand(1000000)
vector_2 = np.random.rand(1000000)
result = 0

# 矩阵相乘-非向量化版本
tic = time.time()
for i in range(1000000):
result = result + vector_1[i] * vector_2[i]
toc = time.time()
print "非向量化-计算结果:" + str(result)
print "非向量化-计算时间:" + str((toc - tic) * 1000) + "ms" + "\n"

# 矩阵相乘-向量化版本
tic = time.time()
result = np.dot(vector_1, vector_2)
toc = time.time()
print "向量化-计算结果:" + str(result)
print "向量化-计算时间:" + str((toc - tic) * 1000)+"ms"


#初始化两个1000000维的随机向量v1,v2用于矩阵相乘计算
v1 = np.random.rand(1000000)
v2 = np.random.rand(1000000)
v = 0

#矩阵相乘-非向量化版本
tic = time.time()
for i in range(1000000):
v = v + v1[i] * v2[i]
toc = time.time()
print "非向量化-计算结果:" + str(v) + "ms"
print "非向量化-计算时间:" + str((toc - tic) * 1000) + "ms" + "\n"

#矩阵相乘-向量化版本
tic = time.time()
v = np.dot(v1, v2)
toc = time.time()
print "向量化-计算结果:" + str(v) + "ms"
print "向量化-计算时间:" + str((toc - tic) * 1000)+"ms"
if __name__ == '__main__':
main()
Loading