- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
ML:基礎學習
課程:機器學習基石
簡介:第十講 Logistic Regression
是正確的,所以 產生 這些資料的機率是很大的,甚至是 1
所以從 找出最大機率的 就能接近
那為何跟 cross-entropy error 有關呢?
首先 cross-entropy 的定義為
因 是目標值,不是 1 就是 0
而 則是實際的機率,代入 logistic function 做為機率的模型,也可用其他的 sigmoid function 取代
再利用 cross-entropy 比較兩者的差異
而在之前的推導,若不將 ,而是維持,改用 表示
可看到此項與先前推導的 cross-entropy 的第二項是一樣的
而 的值會影響
最小化
因為 連續可微、二次可微、convex function,故可用梯度求解
以 PLA 為例
Double subscripts: use braces to clarify
從一小段線段來看且 夠小,從泰勒展開的餘項,或微分定理都可得到
那麼如何最小化 error 呢?
也就是令 為負最多
故
如何選定
隨 遞減
損失函數
tf.nn.sigmoid_cross_entropy_with_logits
cross entropy的直觀理解
Cross entropy
課程:機器學習基石
簡介:第十講 Logistic Regression
比較
的定義
cross-entropy error
所以從
首先 cross-entropy 的定義為
而
再利用 cross-entropy 比較兩者的差異
而
最小化
因為
利用微分連鎖率
更新方程式
最小化的方法
也就是令
故
如何選定 ?
令
為 fixed learning rate
演算法
- 初始化
- For
- 計算
- 修正錯誤
- 直到
或 大概為 0 或 足夠的次數 - 得到
Python 原始碼
- import matplotlib.pyplot as plt
- import numpy as np
- import operator
- import math
- # 網路上找的 dataset 可以線性分割
- rawData = [
- ((-0.4, 0.3), -1),
- ((-0.3, -0.1), -1),
- ((-0.2, 0.4), -1),
- ((-0.1, 0.1), -1),
- ((0.9, -0.5), 1),
- ((0.7, -0.9), 1),
- ((0.8, 0.2), 1),
- ((0.4, -0.6), 1),
- ((0.2, 0.6), -1),
- ((-0.5, -0.5), -1),
- ((0.7, 0.3), 1),
- ((0.9, -0.6), 1),
- ((-0.1, 0.2), -1),
- ((0.3, -0.6), 1),
- ((0.5, 0.1), -1), ]
- # 加入 x0
- dataset = [((1,) + x, y) for x, y in rawData]
- # 內積
- def dot(*v):
- return sum(map(operator.mul, *v))
- # 計算向量長度
- def vlen(v):
- square = map(operator.mul, v, v)
- return sum(square) ** (1/2)
- # 取 thita
- def thita(v):
- return 1 / ( 1 + math.exp( -v ))
- # 計算梯度
- def gradient(w, dataset):
- setsV = []
- for x, y in dataset:
- setsV.append( map(operator.mul, [thita(-y*dot(w, x)) * (-y)] * len(x), x) )
- sum = [0] * len(x)
- for v in setsV:
- sum = map(operator.add, sum, v)
- sum = map(operator.truediv, sum, [1]*len(x))
- return list(sum)
- # 更新 w
- def update(w, n, g):
- u = map(operator.mul, [n] * len(g), g)
- w = map(operator.sub, w, u)
- return list(w)
- # LogisticRegression 演算法實作
- def LogisticRegression(dataset):
- # 初始化 w
- w = [0] * 3
- t = 0
- n = 0.1
- max_number = 100
- while True:
- g = gradient(w, dataset)
- print("g {:.5f} => {}: {}".format(vlen(g), t, tuple(w)))
- w = update(w, n, g)
- t += 1
- if vlen(g) < 0.2 or t > max_number:
- break
- return w
- # 主程式
- def main():
- # 執行
- w = LogisticRegression(dataset)
- # 畫圖
- fig = plt.figure()
- # numrows=1, numcols=1, fignum=1
- ax1 = fig.add_subplot(111)
- xx = list(filter(lambda d: d[1] == -1, dataset))
- ax1.scatter([x[0][1] for x in xx], [x[0][2] for x in xx],
- s=100, c='b', marker="x", label='-1')
- oo = list(filter(lambda d: d[1] == 1, dataset))
- ax1.scatter([x[0][1] for x in oo], [x[0][2] for x in oo],
- s=100, c='r', marker="o", label='1')
- l = np.linspace(-2, 2)
- # w0 + w1x + w2y = 0
- # y = -w0/w2 - w1/w2 x
- if w[2]:
- a, b = -w[1] / w[2], -w[0] / w[2]
- ax1.plot(l, a * l + b, 'b-')
- else:
- ax1.plot([-w[0] / w[1]] * len(l), l, 'b-')
- plt.legend(loc='upper left', scatterpoints=1)
- plt.show()
- if __name__ == '__main__':
- main()
參考
如何通俗地解释泰勒公式?損失函數
tf.nn.sigmoid_cross_entropy_with_logits
cross entropy的直觀理解
Cross entropy
留言
張貼留言