Semi-Supervised學習是一種機器學習方法,利用未標記數據來改進監督學習任務。它是監督學習和無監督學習的中間點,兼具兩者的優點。這篇文章將詳細闡述Semi-Supervised學習的各個方面,包括其定義、應用、演算法等,幫助讀者理解並深入了解這一方法。
一、Semi-Supervised的定義
Semi-Supervised學習的核心思想是:在監督學習中,除了具有已標記數據之外,還存在著未標記的數據,而這些未標記的數據通常比已標記的數據多得多。Semi-Supervised學習通過利用未標記數據,來解決監督學習中困難的分類問題。
在Semi-Supervised學習中,通常將已標記數據稱為有標記數據,將未標記數據稱為無標記數據。這其中,有標記數據數量通常很少,而無標記數據數量則會更多。
二、Semi-Supervised的應用領域
Semi-Supervised學習在很多領域具有廣泛應用:
1. 圖像和視頻標註:在計算機視覺領域,標註大量的圖像和視頻是一項費時費力且昂貴的工作。而Semi-Supervised學習可以通過利用未標記的圖像和視頻進行標註,從而大大減少標註的工作量。
2. 文本分類:在文本分類領域,Semi-Supervised學習可以使用未標記的語料庫來學習一個表徵,進而提高分類器的性能。
3. 金融風險控制:Semi-Supervised學習可以幫助金融機構更準確地評估風險和欺詐行為。
三、Semi-Supervised的演算法
1. Self-training
def self_training(X_l, y_l, X_u, classifier):
classifier.fit(X_l, y_l)
while True:
y_u_pred = classifier.predict(X_u)
y_u_proba = classifier.predict_proba(X_u)
sort_proba = np.sort(y_u_proba, axis=1)[:, ::-1]
sort_ind = np.argsort(y_u_proba, axis=1)[:, ::-1]
high_proba = sort_proba[:, 0]
high_ind = sort_ind[:, 0]
cond = high_proba > 0.9
if not np.any(cond):
break
X_l = np.vstack([X_l, X_u[cond]])
y_l = np.concatenate([y_l, y_u_pred[cond]])
X_u = X_u[np.logical_not(cond)]
return classifier
Self-Training演算法中,首先使用有標記數據來訓練分類器。然後,利用分類器對無標記數據進行預測,並篩選出分類器高度確信的樣本。接著將這些高度確信的樣本用作有標記數據再次訓練分類器。這個過程不斷迭代,直到高度確信的樣本數目無法再增加為止。
2. Co-Training
def co_training(X_l, y_l, X_u, classifier1, classifier2):
data_labeled1 = X_l.tolist()
label_labeled1 = y_l.tolist()
data_labeled2 = X_l.tolist()
label_labeled2 = y_l.tolist()
while X_u.shape[0] > 0:
classifier1.fitting(data_labeled1, label_labeled1)
classifier2.fitting(data_labeled2, label_labeled2)
confident_samples1 = [(ind, x) for ind, x in enumerate(X_u) if abs(classifier1.predict_proba(x)[0][0] - 0.5) > confidence_threshold]
confident_samples2 = [(ind, x) for ind, x in enumerate(X_u) if abs(classifier2.predict_proba(x)[0][0] - 0.5) > confidence_threshold]
if len(confident_samples1) == 0 or len(confident_samples2) == 0:
break
c1_indices, c1_data = zip(*confident_samples1)
c2_indices, c2_data = zip(*confident_samples2)
classifier1_data = np.concatenate(data_labeled1 + list(c1_data))
classifier1_label = np.concatenate(label_labeled1 + classifier2.predict(c1_data).tolist())
classifier2_data = np.concatenate(data_labeled2 + list(c2_data))
classifier2_label = np.concatenate(label_labeled2 + classifier1.predict(c2_data).tolist())
data_labeled1, label_labeled1, data_labeled2, label_labeled2 = classifier1_data.tolist(), classifier1_label.tolist(), classifier2_data.tolist(), classifier2_label.tolist()
X_u = np.delete(X_u, c1_indices + c2_indices, axis=0)
return classifier1, classifier2
Co-Training演算法假設有兩個分類器,每個分類器針對樣本的一部分特徵進行訓練。每個分類器都在已標記的樣本上進行訓練,並且在無標記數據上進行預測。每個分類器都將樣本分成兩部分:高度確信的和確信度較低的。傳統的Co-training演算法假設兩個分類器所看到的數據不同,而且為互補的。高度確信的樣本被認為是正確的,它們將被用來擴充已標記的數據。當已標記數據中沒有高度確信的數據時,演算法就停止。
3. S3VM
def s3vm(X_l, y_l, X_u):
unlabeled_indicator = np.ones(X_u.shape[0], dtype=bool)
Y_l, Y_u = y_l.astype(float), np.zeros(X_u.shape[0], dtype=float)
for i in range(10):
clf = svm.SVC(kernel='linear', probability=True, max_iter=500)
Y = np.concatenate([Y_l, Y_u])
X = np.vstack([X_l, X_u])
clf.fit(X, Y)
delta = np.abs(clf.decision_function(X_u))
margin = np.abs(delta - 1.0)
high_margin_ind = margin > 1e-10
low_margin_ind = ~high_margin_ind
if low_margin_ind.sum() == 0:
break
furthest_ind = delta.argmax()
high_confidence_indicatory = np.zeros(X_u.shape[0], dtype=bool)
high_confidence_indicatory[furthest_ind] = True
high_confidence_indicatory[high_margin_ind] = True
low_confidence_indicatory = np.zeros(X_u.shape[0], dtype=bool)
low_confidence_indicatory[low_margin_ind] = True
clf.fit(X[high_confidence_indicatory | (Y > 0.5)], Y[high_confidence_indicatory | (Y > 0.5)])
r = clf.score(X[high_confidence_indicatory | (Y > 0.5)], Y[high_confidence_indicatory | (Y > 0.5)])
p = clf.score(X[low_confidence_indicatory], Y[low_confidence_indicatory])
pred = clf.predict(X)
Y_u[unlabeled_indicator] = pred[y_l.shape[0]:]
Y_u[high_confidence_indicatory] = pred[y_l.shape[0]:][high_confidence_indicatory]
Y_l_new, Y_u_new = Y_l.copy(), Y_u.copy()
Y_l_new[Y_u > 0.5] = 1.0
Y_l_new[Y_u <= 0.5] = 0.0
if ((Y_l_new - Y_l) ** 2).sum() < 1e-6:
break
return clf
S3VM(Smoothed Semi-supervised Support Vector Machine)演算法假設所有的類別都是連續的正數值,並且使用加權的線性梯度下降法來訓練分類器。通過在訓練過程中使用未標記數據的信息來平滑邊界,S3VM演算法可以在一定程度上減少過擬合問題。
四、總結
本文詳細闡述了Semi-Supervised學習的定義、應用和演算法,並詳細講述了Self-training, Co-Training和S3VM等Semi-Supervised演算法的實現原理和代碼示例。通過對Semi-Supervised學習的深入了解,可以看出它在許多實際領域中的應用很廣泛,並且也提供了一種解決監督學習問題的有力方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/276079.html