2021-03-09

Deep Learning for Computer Vision with Python


前言

随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


一、SGD是什么?

*相比于GD,SGD不是使用整个数据集更新参数,而是按照指定的batch_size更新参数

二、使用步骤

1.引入库

代码如下(示例):

import  numpy as np
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 argparse

2.代码

相比于GD,添加了函数gengerate_batch产生batch_size大小的数据块。
代码如下(示例):


parse = argparse.ArgumentParser()
parse.add_argument("lr", type = float, default = 0.01, help = "input your learning rate")
parse.add_argument("batch_size", type = int, default = 24, help = "input epoch")
parse.add_argument("--epoch", type = int, default = 500, help = "input epoch")
arg = parse.parse_args()

def activation_fuction(x):
    return 1 / (1 + np.exp(-x))
def predict(X , W):
    preds = activation_fuction(X.dot(W))
    preds[preds <= 0.5] = 0
    preds[preds > 0.5] = 1
    return preds

def generate_bactch(X, Y, batch_size):
    for i in range(0, X.shape[0], batch_size):
        yield (X[i: batch_size], Y[i: batch_size])

if __name__ == '__main__':
    #generate datat sample
    (X ,Y) = make_blobs(1000, n_features = 2, centers = 2, cluster_std = 1.5, random_state = 1)
    #X的shape = (1000, 2), Y的shape = (1000, 1)
    Y = Y.reshape(Y.shape[0], 1)
    X = np.c_[X, np.ones((X.shape[0], 1))]
    #随机切割样本
    trainx, testx, trainy, testy = train_test_split(X, Y, test_size = 0.5, random_state = 42)
    ######初始化权重矩阵W

    W = np.random.randn(3,1)
    Loss = []
    batchLoss = []
    for i in range(arg.epoch):
        for (bacthx,batchy) in generate_bactch(trainx, trainy, arg.batch_size):
            preds = predict(bacthx, W)
            error = batchy - preds
            loss = np.sum(1/2*error**2)
            batchLoss.append(loss)
            gradient = bacthx.T.dot(error)
            W += arg.lr * gradient
        epochloss = np.average(batchLoss)
        Loss.append(epochloss)
        if i > 0 and (i + 1)%5:
            print(f"epoch {i+1}/{arg.epoch+1}: {epochloss}")
    preds = predict(testx, W)
    print(classification_report(testy, preds))

    color = []
    for s in testy:
        if s == 1:
            color.append('r')
        else:
            color.append('b')
    ##第一张图,测试集分布散点图
    plt.style.use("ggplot")
    plt.figure()
    plt.title("data")
    plt.scatter(testx[:,0], testx[:,1],c = color)

    ##第二章图 loss图
    plt.style.use("ggplot")
    plt.figure()
    plt.title("Train Loss")
    plt.xlabel("epoch")
    plt.ylabel("Loss")
    plt.plot(range(0,arg.epoch), Loss)
    plt.show()

总结

就这样啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值