Neural Network(4)-Gradient Descent Rule, Sigmoid

1. 单层感知器的梯度递减

1.1 目的

替代了原来的学习法则
用梯度下降去寻找假设空间
假设空间:转自:https://blog.csdn.net/zmdsjtu/article/details/52689392
在这里插入图片描述

1.2 重新定义了错误测量

感知器原则不再适用因为其不可微
在这里插入图片描述

1.3 梯度训练法则

在这里插入图片描述
沿着错误下降最快的方向(错误沿权重下降最快的方向因为调节的是权重) 即梯度的反
在这里插入图片描述

为了理解梯度下降我们显化所有权重和相关联的错误值
在这里插入图片描述
error surface must be parabolic with a single global minimum
在这里插入图片描述
下面演示为什么是沿着梯度的反方向趋近全局或者局部最小值
在这里插入图片描述
在这里插入图片描述
梯度即为方向导数即为正负代表递增或递减
因此如果递增那之后的越来越大误差肯定不是最小因此将权重减小倒回去找
如果递减那之后误差越来越小因此将权重增加往前面去找 总之和梯度相反。

1.4 梯度递减公式的推导

在这里插入图片描述
其中Oe =Wi 与 Xie 乘积 wi 对应的是 其中一个权重(单一权重) xie对应的是一个输入pattern中于Wi相连的那个输入部分 其它的部分都不含Wi就省去了
在这里插入图片描述

1.41 批次更新

在这里插入图片描述
注意这里是把所有的权重更新加起来最后再更新
在这里插入图片描述
在这里插入图片描述

1.42 增量梯度递减

上述的梯度递减面对两个问题:

  1. 收敛时间很长因为每次加的太多了可能跳过了最优解(每次都是在旧的权重上计算)
  2. 如果有很多局部最小值很可能会找不到那个全局最小值因为掉进局部最小陷阱中了

Whereas the gradient descent rule updates the weights after calculating the whole error accumulated from all examples, the incremental version approximates the gradient descent error decrease by updating the weights after each training example
简而言之梯度递减法则等所有example跑完后更新一次权重而增量则每次example都更新

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. Sigmoid 激活函数

2.1 目的:

单层感知器的功能太局限因此用sigmoid引入非线性
在这里插入图片描述

2.2 Sigmoid 作为激活函数的梯度递减

在这里插入图片描述
基本原理没变
在这里插入图片描述
变的是在求导过程中激活函数sigmoid包装的那一层输出
在这里插入图片描述
最终结果多了一个封装的 激活函数的导数 注意负号被放进去了
激活函数的导数为:
在这里插入图片描述
注意真正的应该是去掉一个负号

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.13 增量Sigmoid 梯度递减

在这里插入图片描述

2.14 批次梯度递减和增量梯度递减

在这里插入图片描述
批次的是把所有的pattern带来的权重改变相加在更新这导致每次都是沿着最深梯度下降不会有很大的抖动
而增量是每个pattern 更新一次因此会有很大的抖动比如上一个加下一个减但总体趋势是向梯度递减的有可能帮助跳出局部最小值

3. 感知器训练法则与梯度递减的比较

  1. 梯度递减最小化平方误差和而不是误差(target-out)
  2. 感知器能准确划分并保证找到解(线性可分) 梯度递减精度差并不保证找到最优解
  3. 梯度下降不需要线性可分而感知器必须线性可分
Sure! Here's an example of Python code using PyTorch to implement a neural network for binary classification with a positively weighted sub-network and a negatively weighted sub-network: ```python import torch import torch.nn as nn import torch.optim as optim from torch.autograd import Variable # Define the neural network model class BinaryClassifier(nn.Module): def __init__(self, input_size): super(BinaryClassifier, self).__init__() self.positive_network = nn.Linear(input_size, 1) self.negative_network = nn.Linear(input_size, 1) def forward(self, x): positive_output = self.positive_network(x) negative_output = self.negative_network(x) output = positive_output - negative_output return output # Generate random negative weights from the standard normal distribution def generate_negative_weights(model): for param in model.negative_network.parameters(): param.data = torch.abs(torch.randn(param.size())) * -1 # Define the objective function def objective_function(outputs, labels): criterion = nn.BCEWithLogitsLoss() loss = criterion(outputs, labels) return loss # Generate some dummy data input_size = 10 num_samples = 100 input_data = torch.randn(num_samples, input_size) labels = torch.randint(0, 2, (num_samples,)) # Create the model and initialize random negative weights model = BinaryClassifier(input_size) generate_negative_weights(model) # Training loop num_epochs = 10 learning_rate = 0.001 optimizer = optim.SGD(model.parameters(), lr=learning_rate) for epoch in range(num_epochs): model.train() optimizer.zero_grad() inputs = Variable(input_data) targets = Variable(labels.float()) # Forward pass outputs = model(inputs) loss = objective_function(outputs, targets) # Backward pass and optimization loss.backward() optimizer.step() # Print loss for tracking progress print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) ``` In this code, we define a `BinaryClassifier` class that inherits from `nn.Module` and contains two linear layers: `positive_network` and `negative_network`. The `forward` method computes the output by subtracting the negative output from the positive output. To generate random negative weights, we define the `generate_negative_weights` function, which iterates over the parameters of the negative network and assigns random negative values drawn from the standard normal distribution. The objective function is defined using the `nn.BCEWithLogitsLoss` loss function, which combines a sigmoid activation function and a binary cross-entropy loss. Finally, we define the training loop with a fixed number of epochs, a learning rate, and an optimizer (in this case, stochastic gradient descent). The loop performs a forward pass, computes the loss, performs a backward pass, and updates the model parameters. Please note that this code is a basic implementation and may need modifications depending on your specific requirements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值