-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorize_calculat.py
77 lines (60 loc) · 1.73 KB
/
vectorize_calculat.py
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
# 矢量化计算
# 其目的是利用线性代数库或者深度学习框架进行计算而不是使用语言的for循环
# 加快计算的目的和减少计算开销
import math
import time
import numpy as np
import tensorflow as tf
from matplotlib_inline import backend_inline
import matplotlib.pyplot as plt
n = 1000
a = tf.ones([n])
b = tf.ones([n])
print("a :", a)
class Timer:
'''记录运行多长时间'''
def __init__(self):
self.times = []
self.start()
def start(self):
'''启动记时器'''
self.tik = time.time()
def stop(self):
'''停止计时器,并将记录保存到记录列表中'''
self.times.append(time.time() - self.tik)
return self.times[-1]
def avg(self):
'''返回平均时间'''
return sum(self.times) / len(self.times)
def sum(self):
'''返回时间总和'''
return sum(self.times)
def cumsum(self):
'''返回累计时间'''
return np.array(self.times).cumsum().tolist()
# 使用for循环看需要多长时间
c = tf.Variable(tf.zeros(n))
timer = Timer()
for i in range(n):
c[i].assign(a[i] + b[i])
print(f'{timer.stop():.5f} sec')
# 第二种方法更快
timer.start()
d = a + b
print(f'{timer.stop():.5f} sec')
# 正态分布(高斯分布)与平方损失
# 正态分布函数
def normal(x, mu, sigma):
p = 1 / math.sqrt(2 * math.pi * sigma**2)
return p * np.exp(-0.5 / sigma**2 * (x - mu)**2)
# 使用Numpy进行可视化
x = np.arange(-7, 7, 0.01)
# 均值和标准差对
params = [(0, 1), (0, 2), (3, 1)]
plt.xlabel('x')
plt.ylabel('p(x)')
plt.figsize=(4.5, 2.5)
for mu, sigma in params:
plt.plot(x, normal(x, mu, sigma), label=f'mean {mu}, std {sigma}')
plt.legend()
plt.show()