例子:逻辑回归
在逻辑回归还有参数b,同样的进行迭代更新
计算代码实现:
J=0;dw1=0;dw2=0;db=0;
for i = 1 to m
z(i) = wx(i)+b;
a(i) = sigmoid(z(i));
J += -[y(i)log(a(i))+(1-y(i))log(1-a(i));
dz(i) = a(i)-y(i);
dw1 += x1(i)dz(i);
dw2 += x2(i)dz(i);
db += dz(i);
J/= m;
dw1/= m;
dw2/= m;
db/= m;
w=w-alpha*dw
b=b-alpha*db
缺点:
尽量使用numpy内置的向量操作 * 比如向量乘积(dot) * 还有很多其他的:求指数,绝对值,倒数等
(n,)
assert(a.shape == (5,1))
,可以避免其维度是(5,)
的情况如何种西瓜?
收获后才知道种出的瓜好不好【好瓜是辛勤种植的奖赏】
难以判断当前操作对最终操作的影响
这种过程的抽象
奖赏:在转移到另一个状态时,根据潜在的奖赏函数R反馈给机器一个奖赏,如健康+1,凋零-10
根据这个策略,在状态x下就能得知要执行的动作\(a=\pi(x)\)
【2】概率表示:\(\pi: X \times A \longmapsto R\),随机性策略表示,当前状态x下选择动作a的概率
模型形式无差异
最大化单步奖赏:
通过尝试发现各个动作产生的结果
单步强化学习:
赌徒目标:通过一定的策略最大化自己的奖赏
每个状态-动作的累计奖赏采样值求和取平均,得到状态-动作值函数的估计
转换:是难题
缓解:直接模仿人类专家的“状态-动作对”
隐藏单元:比如第一个节点代表家庭人口,这个只与size和bedroom特征有关,通过权重来实现
–
自动驾驶
RNN:序列数据,如音频、语言
去除此规则所覆盖的正样本,尝试生成下一条规则
重复,直到通过剪枝无法提高验证集的性能为止
复杂度太高
把\(r_i'\)和\(r_i''\)分别与R中除了\(r_i\)之外的规则放在一起,组成规则集R’和R‘’,与R进行比较,选择最优的保留下来。
把特殊的规则转变为更一般的规则
势函数(potential functions):又称为因子,定义在变量子集上的非负实函数,主要用于定义概率分布函数
每个节点至少出现在一个极大团中
联合概率:\(P(x)=\frac{1}{Z}\prod_{Q\subset C}\phi_Q(x_Q)\),\(\phi_Q\)是团Q对应的势函数(因子),可对Q中的变量关系进行建模,Z是规范化因子,确保P(x)是被正确定义的概率。
最直观的精确推断法
同样适用于无向图
关键:采样,高效的基于图模型所描述的概率分布来获取样本
通过限制近似分布的类型,得到一种局部最优、但具有确定解的近似后验分布
工具:sklearn(CountVectorizer统计文本、LatentDirichletAllocation进行LDA分析),pyLDAvis模块进行话题结果的可视化
softmax函数(归一化的指数函数):”squashes”(maps) a K-dimensional vector z of arbitrary real values to a K-dimensional vector σ(z) of real values in the range (0, 1) that add up to 1 (from wiki)。
>>> import math
>>> z = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]
>>> z_exp = [math.exp(i) for i in z]
>>> sum_z_exp = sum(z_exp)
>>> softmax = [i / sum_z_exp for i in z_exp]
>>> print([round(i, 3) for i in softmax])
[0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175]
e
的下标k
应该写错了位置,k
应该是z
的下标(一般最后基于概率值有一个独热编码,对应具体的类别):具体参考sklearn: 数据集加载
>>> from sklearn import datasets
>>> iris = datasets.load_iris()
>>> digits = datasets.load_digits()
>>> print(digits.data)
>>> digits.target
>>> from sklearn import svm
# 设置
>>> clf = svm.SVC(gamma=0.001, C=100.)
# 学习
>>> clf.fit(digits.data[:-1], digits.target[:-1])
# 预测
>>> clf.predict(digits.data[-1:])
>>> import pickle
# 保存
>>> s = pickle.dumps(clf)
# 重新加载
>>> clf2 = pickle.loads(s)
# 预测新数据
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
>>> import numpy as np
>>> from sklearn.datasets import load_iris
>>> from sklearn.svm import SVC
>>> X, y = load_iris(return_X_y=True)
# 第一次训练,得到一个模型
>>> clf = SVC()
>>> clf.set_params(kernel='linear').fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
kernel='linear', max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False)
>>> clf.predict(X[:5])
array([0, 0, 0, 0, 0])
# 第二次训练,得到一个模型
# 直接指定模型需要更改的超参数
>>> clf.set_params(kernel='rbf', gamma='scale').fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> clf.predict(X[:5])
array([0, 0, 0, 0, 0])
如果预测的y是属于多类别的,比如有0,1,2三种类别,有两种方式进行预测:
>>> from sklearn.svm import SVC
>>> from sklearn.multiclass import OneVsRestClassifier
>>> from sklearn.preprocessing import LabelBinarizer
# 直接预测,每个样本是某一类
>>> X = [[1, 2], [2, 4], [4, 5], [3, 2], [3, 1]]
>>> y = [0, 0, 1, 1, 2]
# 先对y进行二值化编码,预测值是二值编码的形式
>>> classif = OneVsRestClassifier(estimator=SVC(random_state=0))
>>> classif.fit(X, y).predict(X)
array([0, 0, 1, 1, 2])
>>> y = LabelBinarizer().fit_transform(y)
>>> classif.fit(X, y).predict(X)
array([[1, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 0, 0], # 这里为0,表示预测的结果不匹配训练集合的任一标签
[0, 0, 0]])
类似的,多标签的(一个样本属于多个类别,比如一个图片同时包含猫和狗)也可以先对y进行标签化,再训练预测:
>> from sklearn.preprocessing import MultiLabelBinarizer
>> y = [[0, 1], [0, 2], [1, 3], [0, 2, 3], [2, 4]]
>> y = MultiLabelBinarizer().fit_transform(y)
>> classif.fit(X, y).predict(X)
array([[1, 1, 0, 0, 0],
[1, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 1, 1, 0],
[0, 0, 1, 0, 1]])
# 这里有0-4共5个类别,所以是一个1x5的数组
# 如果某个样本属于多个类别,相应类别值为1
这个是使用面部特征进行人脸识别的例子:
"""
===================================================
Faces recognition example using eigenfaces and SVMs
===================================================
The dataset used in this example is a preprocessed excerpt of the
"Labeled Faces in the Wild", aka LFW_:
http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz (233MB)
.. _LFW: http://vis-www.cs.umass.edu/lfw/
Expected results for the top 5 most represented people in the dataset:
================== ============ ======= ========== =======
precision recall f1-score support
================== ============ ======= ========== =======
Ariel Sharon 0.67 0.92 0.77 13
Colin Powell 0.75 0.78 0.76 60
Donald Rumsfeld 0.78 0.67 0.72 27
George W Bush 0.86 0.86 0.86 146
Gerhard Schroeder 0.76 0.76 0.76 25
Hugo Chavez 0.67 0.67 0.67 15
Tony Blair 0.81 0.69 0.75 36
avg / total 0.80 0.80 0.80 322
================== ============ ======= ========== =======
"""
from time import time
import logging
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC
print(__doc__)
# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
# #############################################################################
# Download the data, if not already on disk and load it as numpy arrays
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
# introspect the images arrays to find the shapes (for plotting)
n_samples, h, w = lfw_people.images.shape
# for machine learning we use the 2 data directly (as relative pixel
# positions info is ignored by this model)
X = lfw_people.data
n_features = X.shape[1]
# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]
print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)
# #############################################################################
# Split into a training set and a test set using a stratified k fold
# split into a training and testing set
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42)
# #############################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction / dimensionality reduction
n_components = 150
print("Extracting the top %d eigenfaces from %d faces"
% (n_components, X_train.shape[0]))
t0 = time()
pca = PCA(n_components=n_components, svd_solver='randomized',
whiten=True).fit(X_train)
print("done in %0.3fs" % (time() - t0))
eigenfaces = pca.components_.reshape((n_components, h, w))
print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print("done in %0.3fs" % (time() - t0))
# #############################################################################
# Train a SVM classification model
print("Fitting the classifier to the training set")
t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'),
param_grid, cv=5, iid=False)
clf = clf.fit(X_train_pca, y_train)
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_)
# #############################################################################
# Quantitative evaluation of the model quality on the test set
print("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(X_test_pca)
print("done in %0.3fs" % (time() - t0))
print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
# #############################################################################
# Qualitative evaluation of the predictions using matplotlib
def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
"""Helper function to plot a gallery of portraits"""
plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
for i in range(n_row * n_col):
plt.subplot(n_row, n_col, i + 1)
plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
plt.title(titles[i], size=12)
plt.xticks(())
plt.yticks(())
# plot the result of the prediction on a portion of the test set
def title(y_pred, y_test, target_names, i):
pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
return 'predicted: %s\ntrue: %s' % (pred_name, true_name)
prediction_titles = [title(y_pred, y_test, target_names, i)
for i in range(y_pred.shape[0])]
plot_gallery(X_test, prediction_titles, h, w)
# plot the gallery of the most significative eigenfaces
eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces, eigenface_titles, h, w)
plt.show()