单神经元神经网络—识别猫

单神经元神经网络—识别猫

1. Load Data

from h5 file

x各维度 表示(样本数,图片长,图片宽,几个RGB通道)。通过shape查看,后面3个值相乘即可得到 x 总的特征值
y转化为2个维度,表示样本数,每个样本数对应标签

然后将 x 的特征值扁平化(化为一维数组),并标准化,通过将值 /300 化为 [ 0 , 1 ] [0,1] [0,1] 以内
变成(样本数,特征值)(后面向量处理注意转置)

2. 逻辑回归

z = d o t ( w , x ( i ) ) + b z = dot(w,x^{(i)})+b z=dot(w,x(i))+b

x i x^{i} xi 代表一个样本代表特征的向量,w 为对应的权重,b 为阈值,z 为预测结果

向量化:
Z = d o t ( W . T , X ) Z = dot(W.T,X) Z=dot(W.T,X)

( X , W 均为列向量)

3. 激活函数

σ ( z ( i ) ) = 1 1 + e − z ( i ) \sigma(z^{(i)}) = \frac{1}{1+e^{-z^{(i)}}} σ(z(i))=1+ez(i)1

z ( i ) z^{(i)} z(i) 为每个样本通过逻辑回归算出来的值。通过激活函数,可以将其映射到 [ 0 , 1 ] [0,1] [0,1]区间,方便处理

向量化:
A = σ ( Z ) = 1 1 + e − Z A = \sigma(Z) = \frac{1}{1+e^{-Z}} A=σ(Z)=1+eZ1

此处 Z 为向量,代表所有样本的激活函数

4. 损失函数/成本函数

损失函数:

L ( y ^ ( i ) , y ( i ) ) = − ( y ( i ) log ⁡ ( y ^ ( i ) ) + ( 1 − y ( i ) log ⁡ ( 1 − y ^ ( i ) ) ) ) L(\hat{y}^{(i)},y^{(i)}) = -(y^{(i)}\log{(\hat{y}^{(i)})} + (1-y^{(i)}\log{(1-\hat{y}^{(i)})})) L(y^(i),y(i))=(y(i)log(y^(i))+(1y(i)log(1y^(i))))

y ^ ( i ) \hat{y}^{(i)} y^(i) 为预测结果(需要通过激活函数), y y y 为实际结果

向量化求和取平均得到成本函数:

J = ( ∑ i = 1 m ( − ( Y log ⁡ ( Y ^ ) + ( 1 − Y ) log ⁡ ( 1 − Y ^ ) ) ) ) / m J = (\sum_{i=1}^{m}(-(Y\log{(\hat{Y})} + (1-Y)\log{(1-\hat{Y})})))/m J=(i=1m((Ylog(Y^)+(1Y)log(1Y^))))/m

成本函数越小越好

5. 训练

通过不停训练,数值逼近到损失函数最小值

W ′ = W − r × ∂ J ∂ W W^{'} = W - r\times\frac{\partial J}{\partial W} W=Wr×WJ

r 为学习率

可求得:
∂ J ∂ Z = A − Y \frac{\partial J}{\partial Z} = A - Y ZJ=AY

∂ J ∂ w = d o t ( X , ∂ J ∂ Z . T ) / m \frac{\partial J}{\partial w} = dot(X,\frac{\partial J}{\partial Z}.T)/m wJ=dot(X,ZJ.T)/m

实际上是每个样本求出一个 ∂ L ∂ z ( i ) \frac{\partial L}{\partial z^{(i)}} z(i)L,再乘以 x ( i ) x^{(i)} x(i) ,最后求平均得到平均偏移值

重复很多次,即可训练出结果

6. 预测

将值代入 w , b w,b w,b 中,得到一个浮点数,并设大于某个值即为猫,否则不是(此处设的是0.1)

7. 缺陷

准确度还需要加强,学习率不知道怎么取,模型比较简单,还需学习

程序
My_First_AI_Code.py

from package.outer import*

def mode(train_x, train_y, test_x, test_y, num_iterate = 1000, learn_rate = 0.005, print_cost = False):
    mp = init(np.shape(train_x)[1])
    w,b = mp["w"],mp["b"]
    d = optimize(w, b, train_x, train_y, num_iterate, learn_rate, print_cost)
    w,b = d["w"],d["b"]
    Y_predict__train = predict(w,b,train_x)
    Y_predict__test = predict(w,b,test_x)
    print("The accuracy of train data: %d" %(100 - np.mean(np.abs(Y_predict__train - train_y))*100) )
    print("The accuracy of test data: %d" %(100 - np.mean(np.abs(Y_predict__test - test_y))*100) )
    return d

train_x,train_y,test_x,test_y,train_x_orig = load_dataset()

train_hei = np.shape(train_x_orig)[1]
train_wid = np.shape(train_x_orig)[2]
train_num = np.shape(train_x_orig)[0]
train_len = np.shape(train_x)[1]

d = mode(train_x, train_y, test_x, test_y, num_iterate = 3000, learn_rate = 0.005, print_cost = False)
my_image = np.zeros((0,train_len))
images = []
for i in range(1,11):
    my_Image = str(i) + ".jpg" 
    fname = "images/" + my_Image
    image = np.array(plt.imread(fname))
    images.append(image)
    image = np.array(plt.imread(fname))
    temp_image = tf.resize(image,(train_hei,train_wid), mode='reflect').reshape((1, train_hei*train_wid*3))
    my_image = np.r_[my_image,temp_image]   

my_predicted_image = predict(d["w"], d["b"], my_image)

cnt = 0
for i in my_predicted_image:
    cnt=cnt+1
    print(str(cnt) + " : 预测结果为 " + str(i))
    plt.figure()
    plt.imshow(images[cnt-1])
    plt.show()

outer.py

import numpy as np
import matplotlib.pyplot as plt
import h5py
import skimage.transform as tf

def load_dataset():
    # 训练集
    train_dataset = h5py.File('../src/datasets/train_catvnoncat.h5')
    # 提取数据
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])
    # 训练集特征信息
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])
    # 训练集标签
    train_set_y = train_set_y_orig.reshape(1,np.shape(train_set_y_orig)[0])
    # 向量化
    train_set_x_flat = train_set_x_orig.reshape(np.shape(train_set_x_orig)[0],-1)
    # 扁平化
    train_set_x = train_set_x_flat/300.
    # 标准化

    # 测试集
    test_dataset = h5py.File('../src/datasets/test_catvnoncat.h5')
    # 提取数据
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])
    # 测试集特征信息
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])
    # 测试集标签
    test_set_y = test_set_y_orig.reshape(1,np.shape(test_set_y_orig)[0])
    # 向量化
    test_set_x_flat = test_set_x_orig.reshape(np.shape(test_set_x_orig)[0],-1)
    # 扁平化
    test_set_x = test_set_x_flat/300.
    # 标准化

    return train_set_x,train_set_y,test_set_x,test_set_y,train_set_x_orig

def sigma(Z):
    return 1/(np.exp(-Z)+1)

def init(Len):
    b = 0
    w = np.zeros((1,Len))
    mp = {"w":w,"b":b}
    return mp

def propogate(w, b, X, Y):
    num = np.shape(X)[0]
    Z = np.dot(w,X.T)+b
    A = sigma(Z)
    cost = np.sum(-(Y*np.log(A)+(1-Y)*np.log(1-A)))/num
    dZ = A-Y 
    dw = np.dot(dZ,X)/num
    db = np.sum(dZ)/num 
    dic = {"dw":dw,"db":db}
    return dic,cost

def optimize(w, b, X, Y, num_interate = 1000, learn_rate = 0.005, print_cost = False):   
    for i in range(num_interate):
        dic,cost = propogate(w,b,X,Y)
        dw = dic["dw"]
        db = dic["db"]
        w = w - learn_rate*dw
        b = b - learn_rate*db
        if i%100 == 0:
            print("The cost is " + str(cost) + "! When we opt " + str(i) + " times")
    d={"w":w,"b":b}
    return d


def predict(w,b,X):
    Z = np.dot(w,X.T)+b
    A = sigma(Z)
    res = []
    A = A.reshape(-1)
    for i in A:
        if i > 0.1:
            res.append(1)
        else:
            res.append(0)
    return res

资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值