1) Deep Learning for Computer Vision with Python Starter Bundle第9章的例子

#博客地址:https://blog.csdn.net/kelsel
#代碼

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np
import argparse

# 正則化函數

y=11+exy = \frac{1}{ 1+e^{-x}}

def sigmoid_activation(x):
    return 1.0 / (1 + np.exp(-x))

def predict(X, W):
    preds = sigmoid_activation(X.dot(W))

    preds[preds <= 0.5] = 0
    preds[preds > 0] = 1

    return preds

ap = argparse.ArgumentParser()
ap.add_argument("-e", "--epochs", type = float, default = 100,
        help="# of epochs")
ap.add_argument("-a", "--alpha", type = float, default = 0.01,
        help = "learning rate")
args = vars(ap.parse_args())

# 造數據
(X, y) = make_blobs(n_samples = 1000, n_features = 2, centers = 2,
        cluster_std = 1.5, random_state = 1)
# y.shape (1000,) -> (1000, 1)
y = y.reshape((y.shape[0], 1))

#  按行連接
#  In [1]: from numpy import c_
#  In [2]: a = ones(4)
#  In [3]: b = zeros((4,10))
#  In [4]: c_[a,b]
#  Out[4]:
#  array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#         [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#         [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#         [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
X = np.c_[X, np.ones((X.shape[0]))]

(trainX, testX, trainY, testY) = train_test_split(X, y,
        test_size = 0.5, random_state = 42)


# 隨機初始化W和loss
print("[INFO] training...")
W = np.random.randn(X.shape[1], 1)
losses = []


import types
for epoch in np.arange(0, args["epochs"]):
    preds = sigmoid_activation(trainX.dot(W))

    # 計算方差
    error = preds - trainY
    loss = np.sum(error ** 2)
    losses.append(loss)


    gradient = trainX.T.dot(error)

    # 右邊的計算結果是(3, 1)的矩陣
    W += -args["alpha"] * gradient

    if epoch == 0 or (epoch + 1) % 5 == 0:
        print("[INFO] epoch = {}, loss = {:.7f}".format(int(epoch + 1), loss))

print("[INFO] evaluating...")
preds = predict(testX, W)
print(classification_report(testY, preds))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章