Sigmoid function optimization

Sigmoid function optimization

Abstract

  Neural Networks is an emerging security paradigm. As neural networks are increasingly widely used in various fields, many applications for edge mobile devices have to use neural networks for computation. However, due to the restrictions on computational resources and computing resources in the edge, we usually need to optimize the neural networks before they can be applied on edge devices. In this article, I describe how to optimize a neural network by optimizing the Sigmoid activation function.

1. Introduction

  The S i g m o i d Sigmoid Sigmoid function, also called the logistic function, is used for the output of the hidden layer neurons and takes values in the range ( 0 , 1 ) (0,1) (0,1), which can map a real number to the interval ( 0 , 1 ) (0,1) (0,1) and can be used for binary classification. S i g m o i d Sigmoid Sigmoid is smooth and easy to derive.

The S i g m o i d Sigmoid Sigmoid function is defined by the following equation:
s i g m o i d ( x ) = 1 1 + e − x sigmoid(x) = \frac{1}{1+e^{-x} } sigmoid(x)=1+ex1
Its derivative with respect to x x x can be expressed in terms of itself as
S ′ ( x ) = e − x ( 1 + e − x ) 2 = S ( x ) ( 1 − S ( x ) ) S^{\prime}(x)=\frac{e^{-x}}{\left(1+e^{-x}\right)^{2}}=S(x)(1-S(x)) S(x)=(1+ex)2ex=S(x)(1S(x))
在这里插入图片描述

  In the S i g m o i d Sigmoid Sigmoid function, there is an e x p exp exp function and a d i v i s i o n division division, both calculations are very cumbersome and time consuming , which is very unfriendly to edge devices with limited resources. So it is a good idea to optimize S i g m o i d Sigmoid Sigmoid in terms of e x p exp exp function and d i v i s i o n division division

2. Optimization Analysis

s i g m o i d ( x ) = 1 1 + e − x sigmoid(x) = \frac{1}{1+e^{-x} } sigmoid(x)=1+ex1
⇓ \Downarrow
1 y = 1 + e − x \frac{1}{y} = 1+e^{-x} y1=1+ex
⇓ \Downarrow
1 y − 1 = e − x \frac{1}{y}-1 = e^{-x} y11=ex
⇓ \Downarrow
l n ( 1 y − 1 ) = − x ln(\frac{1}{y}-1)=-x ln(y11)=x
⇓ \Downarrow
− l n ( 1 y − 1 ) = x -ln(\frac{1}{y}-1)=x ln(y11)=x
⇓ \Downarrow
f − 1 ( y ) = − l n ( 1 y + − 1 ) f^{-1}(y) = -ln(\frac{1}{y}+-1) f1(y)=ln(y1+1)

# The original code
if Sigmoid(P[4]) > threshold then 
	do smothing
# The new code
if P[4] > desigmoid_threshold
    do smothing
3. Algorithm
def sigmoid(x):
	return 1 / (1 + math.exp(-x))
def desigmoid(x):
	return -math.log(1 / y - 1)

在这里插入图片描述

4. Future work

Although the computational efficiency has been greatly improved after d e s i g m o i d desigmoid desigmoid optimization, exponential overflow can easily occur when the data volume become large, and we need to find an effective way to solve this problem. I am now using l o g log log function to smooth the data, which reduces the probability of overflow to some extent, but I need to find a more efficient method to solve exponential overflow problem in the future.

1. whale_optimization 在 Matlab 中实现鲸鱼优化算法(Whale Optimization Algorithm)需要先定义目标函数。以下是一个简单的示例: ``` function f = obj_fun(x) f = x(1)^2 + x(2)^2; end ``` 然后,可以使用以下代码来实现鲸鱼优化算法: ``` function [bestSol, bestFitness] = whale_optimization(obj_fun, nVar, lb, ub) % 参数说明: % obj_fun:目标函数句柄 % nVar:变量个数 % lb:每个变量的下限 % ub:每个变量的上限 % 初始化种群 popSize = 10; maxIter = 100; emptyWhale.Position = []; emptyWhale.Fitness = []; pop = repmat(emptyWhale, popSize, 1); for i = 1:popSize pop(i).Position = unifrnd(lb, ub, 1, nVar); pop(i).Fitness = obj_fun(pop(i).Position); end % 迭代优化 for it = 1:maxIter for i = 1:popSize % 更新位置 A = 2 * rand(1, nVar) - 1; C = 2 * rand(1, nVar); l = rand(); p = rand(); for j = 1:nVar if p < 0.5 if abs(A(j)) >= 1 rand_leader_index = floor(popSize * rand() + 1); X_rand = pop(rand_leader_index).Position; D_X_rand = abs(C(j) * X_rand(j) - pop(i).Position(j)); pop(i).Position(j) = X_rand(j) - A(j) * D_X_rand; else D_Leader = abs(C(j) * bestSol.Position(j) - pop(i).Position(j)); pop(i).Position(j) = bestSol.Position(j) - A(j) * D_Leader; end else dist = abs(bestSol.Position(j) - pop(i).Position(j)); pop(i).Position(j) = dist * exp(b * l) * cos(2 * pi * l) + bestSol.Position(j); end end % 对位置进行限制 pop(i).Position = max(pop(i).Position, lb); pop(i).Position = min(pop(i).Position, ub); % 更新适应度 pop(i).Fitness = obj_fun(pop(i).Position); % 更新最优解 if pop(i).Fitness < bestSol.Fitness bestSol = pop(i); end end end % 返回最优解及其适应度 bestFitness = bestSol.Fitness; bestSol = bestSol.Position; end ``` 2. gru_loss 在 Matlab 中实现 GRU 模型的损失函数需要使用交叉熵损失函数。以下是一个简单的示例: ``` function loss = gru_loss(y_pred, y_true) % 参数说明: % y_pred:模型预测结果,大小为 [batch_size, num_classes] % y_true:真实标签,大小为 [batch_size, num_classes] eps = 1e-10; y_pred = max(min(y_pred, 1 - eps), eps); % 防止出现 log(0) 的情况 loss = -sum(y_true .* log(y_pred), 2); end ``` 3. gru_predict 在 Matlab 中使用 GRU 模型进行预测需要先定义模型。以下是一个示例: ``` function model = gru_model(num_classes) % 参数说明: % num_classes:分类数 inputSize = 100; hiddenSize = 64; outputSize = num_classes; model = struct(); model.Wx = randn(inputSize, hiddenSize); model.Wh = randn(hiddenSize, hiddenSize); model.b = zeros(1, hiddenSize); model.Wy = randn(hiddenSize, outputSize); model.by = zeros(1, outputSize); end ``` 然后,可以使用以下代码进行预测: ``` function y_pred = gru_predict(model, X) % 参数说明: % model:GRU 模型 % X:输入数据,大小为 [batch_size, inputSize, sequence_length] [batch_size, inputSize, sequence_length] = size(X); hiddenSize = size(model.Wx, 2); outputSize = size(model.Wy, 2); h = zeros(batch_size, hiddenSize); for t = 1:sequence_length x_t = reshape(X(:, :, t), [batch_size, inputSize]); z_t = sigmoid(x_t * model.Wx + h * model.Wh + model.b); r_t = sigmoid(x_t * model.Wxr + h * model.Whr + model.br); h_tilde_t = tanh(x_t * model.Wxh + (r_t .* h) * model.Whh + model.bh); h = (1 - z_t) .* h + z_t .* h_tilde_t; end y_pred = softmax(h * model.Wy + model.by); end ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值