10. The only skill that will be important in
the 21st century is the skill of learning
new skills. Everything else will become
obsolete over time.
- Peter Drucker
11. There’s this very simple concept that Carol Dweck talks
about, which is if you take two people, one of them is a
learn-it-all and the other one is a know-it-all, the learn-it-all
will always trump the know-it-all in the long run.
A NEW CORPORATE MINDSET
Source: 主動打造⼈才
16. ⼀些判斷⼩訣竅
exists some underlying pattern to be learned
performance measure can be improved
but no programmable definition
there is data about the pattern
87. x_, y_ = np.array(x, dtype=np.int64), np.array(y, dtype=np.int64)
%%timeit
z = x_ + y_
147 µs ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
NumPy
x_, y_ = np.array(x, dtype=np.int32), np.array(y, dtype=np.int32)
%%timeit
z = x_ + y_
46.6 µs ± 2.06 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
NumPy
%%timeit
i, z = 0, []
while i < n:
z.append(x[i] + y[i])
i += 1
307 µs ± 20.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
While Loop
%%timeit
z = []
for i in range(n):
z.append(x[i] + y[i])
208 µs ± 6.69 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
For Loop
import random
import numpy as np
r = [random.randrange(100) for _ in range(100_000)] # ⼗萬個 0~99 的隨機數
n = 1_000
x, y = random.sample(r, n), random.sample(r, n)
向量加法
88. x_, y_ = np.array(x, dtype=np.int32), np.array(y, dtype=np.int32)
%%timeit
z = x_ + y_
46.6 µs ± 2.06 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
NumPy
x_, y_ = np.array(x, dtype=np.int64), np.array(y, dtype=np.int64)
%%timeit
z = x_ + y_
147 µs ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
NumPy
%%timeit
i, z = 0, []
while i < m:
j, z_ = 0, []
while j < n:
z_.append(x[i][j] + y[i][j])
j += 1
z.append(z_)
i += 1
35.8 ms ± 1.05 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
While Loop
%%timeit
z = []
for i in range(m):
z_ = []
for j in range(n):
z_.append(x[i][j] + y[i][j])
z.append(z_)
31.4 ms ± 4.05 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
For Loop
m, n = 100, 1_000 # 模擬 100x1000 matrix
x = [random.sample(r, n) for _ in range(m)]
y = [random.sample(r, n) for _ in range(m)]
矩陣加法
89. Vectorization
1
2
1
2
L1 L2
W1,1
W1,2
y1 = (x1w1,1) + (x2w1,2)
x1
x2
import numpy as np
# define two arrays a, b:
a = np.random.rand(1_000_000)
b = np.random.rand(1_000_000)
%%timeit
c = np.dot(a,b) # 求內積
1.09 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
print(c)
249581.03910735418
NumPy
%%timeit
c = 0
for i in range(1_000_000):
c += a[i]*b[i]
781 ms ± 30.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
print(c)
249581.03910735913
For Loop
90. import numpy as np
import torch
import random
r = [random.randrange(100) for _ in range(100_000_000)] # ⼀億個 0~99 的隨機數
n = 1_000_000
x, y = random.sample(r, n), random.sample(r, n)
向量加法
x_, y_ = np.array(x, dtype=np.int64), np.array(y, dtype=np.int64)
%%timeit
z = x_ + y_
841 µs ± 5.43 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
NumPy
tensor_x = torch.from_numpy(x_)
tensor_y = torch.from_numpy(y_)
device = torch.device('cuda')
x = tensor_x.to(device)
y = tensor_y.to(device)
%%timeit
z = x + y
61.7 µs ± 10.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
PyTorch
117. 最佳策略問題 3
• 規則同問題1,⼝袋中只有藍⾊球
log2
1
p
• 歸納上述問題,若⾊球出現的機率為 p,則猜中該⾊球需要的問題數為
1
4
log24 = 2
• 例如問題2,紅⾊球機率為 ,要兩個問題才能猜中
∑
i
pi × log2
1
pi
= −
∑
i
pi × log2pi
• 整個題⽬所需的問題個數為期望值
log21 = 0
所需問題數
118. (Shannon) Entropy vs Randomness
Entropy is maximum at maximum randomness
𝖧(𝖷) = −
n
∑
i=1
P(xi)logb P(xi)
= −
2
∑
i=1
1
2
log2
1
2
= −
2
∑
i=1
1
2
⋅ (−1) = 1
119. 問題 2 使⽤策略1
• 規則同問題1,但機率為
1
8
1
8
1
4
1
2
是藍⾊或紅⾊嗎?
是藍⾊嗎? 是綠⾊嗎?
Y
Y N Y
N
N
1
8
× 2 +
1
8
× 2 +
1
4
× 2 +
1
2
× 2 = 2 期望值
給定⼀個策略,cross entropy 就是該策略下猜中顏⾊的期望值
這個策略比較差
120. 數學上的定義
∑
i
pi × log2
1
pi
= −
∑
i
pi × log2pi
L(y, ̂
y) = −
M
∑
j=0
N
∑
i=0
(yij * log( ̂
yij))
categorical cross entropy cost function
−
∑
i
pi × log2
̂
pi
為真實的機率
pi
為錯誤假設的機率
̂
pi
cross entropy
network.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
126. One-hot Encoding
train_labels = tf.keras.utils.to_categorical(train_labels)
Color
Red
Red
Yellow
Green
Yellow
Red Yellow Green
1 0 0
1 0 0
0 1 0
0 0 1
0 1 0