深度学习-神经网络

 1、神经网络介绍

基础依然是线性分类,通过激活函数引入了非线性的成分,使其能拟合任意的函数,通过反向传播自动寻找合适的线性分类器的参数

1.1、前向计算误差:

net_{h1}=w_{1}*i_{1}+w_{2}*i_{2}+b_{1}*1=0.15*0.05+0.2*0.1+0.35*1=0.3775

net_{h2}=w_{3}*i_{1}+w_{4}*i_{2}+b_{1}*1=0.25*0.05+0.3*0.1+0.35*1=0.3925

out_{h1}=\frac{1}{1+e^{-net_{h1}}}=\frac{1}{1+e^{-0.3775}}=0.59327

out_{h2}=\frac{1}{1+e^{-net_{h1}}}=\frac{1}{1+e^{-0.3925}}=0.59688

net_{o1}=w_{5}*h_{1}+w_{6}*h_{2}+b_{2}*1

=0.4*0.59327+0.45*0.59688+0.6*1=1.105904

net_{o2}=w_{7}*h_{1}+w_{8}*h_{2}+b_{2}*1

=0.5*0.59327+0.55*0.59688+0.6*1=1.224919

out_{o1}=\frac{1}{1+e^{-net_{o1}}}=\frac{1}{1+e^{-0.1.105904}}=0.75136

out_{o2}=\frac{1}{1+e^{-net_{o2}}}=\frac{1}{1+e^{-0.1.224919}}=0.77293

E_{total}=\sum \frac{1}{2}(target-output)^{2}=E_{o1}+E_{o2}

=\frac{1}{2}(0.75136-0.01)^{2}+\frac{1}{2}(0.77293-0.99)^{2}=0.2748+0.0235=0.2983

1.2、反向传播

1)更新W5参数

求导,链式法则

\frac{\partial E_{total}}{\partial _{w_{5}}}=\frac{\partial E_{total}}{\partial _{out_{o1}}}*\frac{\partial _{out_{o1}}}{\partial _{net_{o1}}}*\frac{\partial _{net_{o1}}}{\partial _{w_{5}}}

=(target_{o1}-out_{o1})*(out_{o1}-out_{o1}^{2})*(out_{h1})

w_{5}^{+}=w_{5}-\alpha *\frac{\partial E_{total}}{\partial w_{5}}=0.3589

2)更新W1参数

\frac{\partial E_{total}}{\partial _{w_{1}}}=(\sum _{o}\frac{\partial E_{total}}{\partial _{out_{o}}}*\frac{\partial _{out_{o}}}{\partial _{net_{o}}}*\frac{\partial _{net_{o}}}{\partial _{out_{h1}}})*\frac{\partial out_{h1}}{\partial net_{h1}}*\frac{\partial net_{h1}}{\partial w_{1}}

w_{1}^{+}=w_{1}-\alpha *\frac{\partial E_{total}}{\partial w_{1}}=0.14978

1.3、基础代码

import numpy as np
import h5py

def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = np.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

def predict(w, b, X):
    Y_prediction = np.zeros((1, X.shape[1]))
    w = w.reshape(X.shape[0], 1)   #(12288, 1)
    # 计算结果
    A = 1 / (1 + np.exp(-(np.dot(w.T, X) + b)))
    for i in range(A.shape[1]):
        if A[0, i] <= 0.5:
            Y_prediction[0, i] = 0
        else:
            Y_prediction[0, i] = 1
    return Y_prediction


# 1、读取样本数据
train_x, train_y, test_x, test_y, classes = load_dataset()
# print("训练集的样本数: ", train_x.shape[0])  #209
# print("测试集的样本数: ", test_x.shape[0])  #50
# print("train_x形状: ", train_x.shape)  #(209, 64, 64, 3)
# print("train_y形状: ", train_y.shape)  #(1, 209)
# print("test_x形状: ", test_x.shape)  #(50, 64, 64, 3)
# print("test_x形状: ", test_y.shape)  #(1, 50)

# 输入数据的形
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值