The Truth of Sisyphus
  • Introduction
  • Deep Learning
    • Basics
      • Hinge Loss
      • Regularizations
      • Linear Classification
      • Multi-Class and Cross Entropy Loss
      • Batch Norm and other Normalizations
      • Optimization
      • Optimization Functions
      • Convolution im2col
      • Activation Functions
      • Derivatives
        • Derivatives of Softmax
        • A Smooth (differentiable) Max Function
      • Model Ensemble
      • Layers Python Implementation
    • Classification
      • Mobile friendly networks
      • Non-local Neural Networks
      • Squeeze-and-Excitation Networks
      • Further Attention Utilization -- Efficience & Segmentation
      • Group Norm
      • ShuffleNet V2
    • Segmentation
      • Several Instance Segmentation
      • A Peek at Semantic Segmentation
      • Design Choices for Mobile Friendly Deep Learning Models, Semantic Segmentation
      • Efficient Video Object Segmentation via Network Modulation
      • BiSeNet
      • DeepLabV3+
    • Detection
      • CornerNet
      • IoU-Net
      • Why smooth L1 is popular in BBox Regression
      • MTCNN-NCNN
      • DetNet
      • SSD Illustration
    • RNN Related
      • GRU vs LSTM
      • BERT
    • Reinforcement Learning
      • AutoML in Practice Review
      • DRL for optimal execution of profolio transaction
    • Multi-task
      • Multi-task Overview
      • What are the tricks in Multi-Task network design?
    • Neural Network Interpretation
      • Neuron Visualization
    • Deep Learning Frameworks
      • How does Caffe work
      • [Gluon] When to use (Hybrid)Sequential and (Hybrid)Block
      • Gluon Hybrid Intro
      • Gluon HybridBlocks Walk-Through
      • A quick tour of Torch internals
      • NCHW / NHWC in Pytorch
      • Static & Dynamic Computation Graph
    • Converting Between DL Frameworks
      • Things To Be Considered When Doing Model Converting
      • Caffe to TensorFlow
    • Computation Graph Optimization
      • Two ways of TensorRT to optimize Neural Network Computation Graph
      • Customized Caffe Memory Optimization
      • NCNN Memory Optimization
      • Symbolic Programs Advantages: More Efficient, Reuse Intermediate Memory, Operation Folding
    • Deep Learning Debug
      • Problems caused by dead ReLU
      • Loss jumps to 87.3365
      • Common Causes of NANs During Training
    • Deployment
      • Efficient Convolution Operation
      • Quantization
    • What I read recently
      • Know Google the Paper Way
      • ECCV 2018
      • Neural Machine Translation
      • Street View OCR Extraction System
      • Teaching Machines to Draw
      • Pixel to Graph
      • Burst Image Deblurring
      • Material for Masses
      • Learning to Separate Object Sounds by Watching Unlabeled Video
    • Papers / Posts to be read
    • Dummy thoughts
  • Machine Learning
    • Classification
    • Regression
    • Clustering
    • Dimension Reduction
    • Metrics
    • Regularization
    • Bayesian Example
    • Machine Learning System Design
    • Recommendation
    • Essentials of Machine Learning
    • Linear Regression
    • Logistic Regression
      • Logistic Function
    • Gaussian Discriminant Analysis
    • Naive Bayes
    • SVM
    • MLE vs MAP
    • Boosting
    • Frequent Questions
    • Conclusion of Machine Learning
  • Python notes
    • Python _ or __ underscores usage
    • Python Multiprocess and Threading Differences
    • Heapq vs. Q.PriorityQueue
    • Python decorator
    • Understanding Python super()
    • @ property
    • Python __all__
    • Is Python List a Linked List or Array
    • What is the "u" in u'Hello world'
    • Python "self"
    • Python object and class
    • Python Class' Instance method, Class method, and Static Methods Demystified
    • Python WTF
    • Python find first value index in a list: [list].index(val)
    • Sort tuples, and lambda usecase
    • Reverse order of range()
    • Python check list is empty
    • Python get ASCII value from character
    • An A-Z of useful Python tricks
    • Python nested function variable scope
    • Python reverse a list
    • Python priority queue -- heapq
  • C++ Notes
    • Templates
    • std::string (C++) and char* (or c-string "string" for C)
    • C++ printf and cout
    • Class Member Function
    • Inline
    • Scope Resolution Operator ::
    • Constructor
    • Destructor
    • Garbage Collection is Critical
    • C++ Question Lists
  • Operating System
    • Basics
    • Mutex & Semaphore
    • Ticket Selling System
    • OS and Memory
    • Sort implementation in STL
    • Compile, link, loading & run
    • How to understand Multithreading and Multiprocessing from the view of Operating System
  • Linux & Productivity
    • Jupyter Notebook on Remote Server
    • Nividia-smi monitoring
  • Leetcode Notes
    • Array
      • 11. Container With Most Water
      • 35. Search Insert Position
    • Linked List
      • Difference between Linked List and Array
      • Linked List Insert
      • Design of Linked List
      • Two Pointers
        • 141. Linked List Cycle
        • 142. Linked List Cycle II
        • 160. Intersection of two Linked List
        • 19. Remove N-th node from the end of linked list
      • 206. Reverse Linked List
      • 203. Remove Linked List Elements
      • 328. Odd Even Linked List
      • 234. Palindrome Linked List
      • 21. Merge Two Sorted Lists
      • 430. Flatten a Multilevel Doubly Linked List
      • 430. Flatten a Multilevel Doubly Linked List
      • 708. Insert into a Cyclic Sorted List
      • 138. Copy List with Random Pointer
      • 61. Rotate List
    • Binary Tree
      • 144. Binary Tree Preorder Traversal
      • 94. Binary Tree Iterative In-order Traverse
    • Binary Search Tree
      • 98. Validate Binary Search Tree
      • 285. Inorder Successor in BST
      • 173. Binary Search Tree Iterator
      • 700. Search in a Binary Search Tree
      • 450. Delete Node in a BST
      • 701. Insert into a Binary Search Tree
      • Kth Largest Element in a Stream
      • Lowest Common Ancestor of a BST
      • Contain Duplicate III
      • Balanced BST
      • Convert Sorted Array to Binary Search Tree
    • Dynamic Programming
      • 198. House Robber
      • House Robber II
      • Unique Path
      • Unique Path II
      • Best time to buy and sell
      • Partition equal subset sum
      • Target Sum
      • Burst Ballons
    • DFS
      • Clone Graph
      • General Introduction
      • Array & String
      • Sliding Window
  • Quotes
    • Concert Violinist Joke
    • 船 Ship
    • What I cannot create, I do not understand
    • Set your course by the stars
    • To-do list
Powered by GitBook
On this page
  1. Deep Learning
  2. Basics
  3. Derivatives

A Smooth (differentiable) Max Function

PreviousDerivatives of SoftmaxNextModel Ensemble

Last updated 6 years ago

Take home: 使用 LogSumExp 函数取代max函数

Q:想到一个点,既然多分类下的 hinge loss 可以用 differentiable 的 trick 推出 softmax,那 ranking 问题是不是也可以用 hinge loss 去做 pair-wise 的 loss function? 是不是也可以用 differentiable 的手段推出某种 loss。

LogSumExp函数的导数恰好为softmax函数:

这里出现了一个经典的歧义,softmax实际上并不是max函数的smooth版,而是one-hot向量(最大值为1,其他为0)的smooth版。其实从输出上来看也很明显,softmax的输出是个向量,而max函数的输出是一个数值,不可能直接用softmax来取代max。max函数真正的smooth版本是LogSumExp函数.

经过这一变换,给予非目标分数的1的梯度将会通过LogSumExp函数传播给所有的非目标分数,各个非目标分数得到的梯度是通过softmax函数进行分配的,较大的非目标分数会得到更大的梯度使其更快地下降。这些非目标分数的梯度总和为1,目标分数得到的梯度为-1,总和为0,绝对值和为2,这样我们就有效地限制住了梯度的总幅度。

这个就是大家所熟知的softmax交叉熵损失函数了。在经过两步smooth化之后,我们将一个难以收敛的函数逐步改造成了softmax交叉熵损失函数,解决了原始的目标函数难以优化的问题。从这个推导过程中我们可以看出smooth化不仅可以让优化更畅通,而且还变相地在类间引入了一定的间隔,从而提升了泛化性能。

在最优化问题中,求一个函数的最大值或最小值,最直接的方法是求导,然后比较各阶极值的大小。然而,我们所要优化的函数往往不一定可导,比如函数中含有最大值函数 max( x, y) 的。这时候就得求助于其他思路了。有一个很巧妙的思路是,将这些不可导函数用一个可导的函数来近似它,从而我们用求极值的方法来求出它近似的最优值。本文的任务,就是探究一个简单而有用的函数,它能够作为最大值函数的近似,并且具有多阶导数。

在数学分析中,笔者已经学习过一个关于最大值函数的公式,即当x≥0,y≥0 时,我们有:

Max(x,y) = 1 / 2 * ( | x + y | + | x − y | )

那么,为了寻求一个最大值的函数,我们首先可以考虑寻找一个能够近似表示绝对值 | x | 的函数,这样我们就把问题从二维降低到一维了。那么,哪个函数可以使用呢?

直接观察挺难发现哪个函数可以使用的,我们将问题逐步向简单推进。我们对 f(x)=|x| 求导,除了 x = 0 这一点外,其他都可以顺利求导。

f′(x)= 1, x > 0

f′(x)= −1 x < 0

这是一个简单的分段函数,在物理中,这类函数十分常见,跟它最接近的,应该是单位阶跃函数θ(x):

θ(x) = 1, x > 0

θ(x) = 0 x < 0

那么

f′(x) = 2 * θ(x) − 1

下面只需要寻求 θ(x) 的近似函数,物理学家已经提供现成的函数给我们了,一个比较简单的形式是:

\theta(x)=\lim_{k\to +\infty} \frac{1}{1+e^{-k x}}\

那么我们就可以取 11+e−kx\frac{1}{1+e^{-k x}}1+e−kx1​ 作为近似函数了,代入(4) 式得到 2ekx1+ekx−1\frac{2e^{k x}}{1+e^{k x}}-11+ekx2ekx​−1 ,积分得到

\begin{aligned}f(x)&=\frac{2}{k}\ln(1+e^{kx})-x\\ &=\frac{1}{k}\left[\ln(1+e^{kx})+\ln(1+e^{-kx})\right]\\ &=\frac{1}{k}\ln(2+e^{kx}+e^{-kx})\end{aligned}\

不难发现,(6) 式中的对数部分,在 k 足够大的时候,常数 2 的影响微乎其微,把它去掉之后,我们有一个比较简单的绝对值函数:

|x|=\lim_{k\to +\infty} \frac{1}{k}\ln(e^{kx}+e^{-kx})\

结合 (7) 式和 (1) 式,我们就得到

\max(x,y)=\lim_{k\to +\infty} \frac{1}{2k}\left\{\ln[e^{k(x+y)}+e^{-k(x+y)}]+\ln[e^{k(x-y)}+e^{-k(x-y)}]\right\}\

(8) 式还可以再化简,我们得到

\max(x,y)=\lim_{k\to +\infty} \frac{1}{2k}\ln(e^{2kx}+e^{-2kx}+e^{2ky}+e^{-2ky})\

并且由于 (1) 式是在 x≥0,y≥0 时成立的,所以(9)式中的 e−2kx 和 e−2ky 均变得不重要了,我们也把它们去掉,进一步得到

\max(x,y)=\lim_{k\to +\infty} \frac{1}{2k}\ln(e^{2kx}+e^{2ky})\

或者写成

\max(x,y)=\lim_{k\to +\infty} \frac{1}{k}\ln(e^{kx}+e^{ky})\

(11) 式正是我们希望得到的理想的最大值函数。虽然我们的推导基于 x ≥ 0, y ≥ 0,但是不难发现,对于x,y 中出现负数时,上述公式仍然成立!它甚至还可以推广到多个变量的最大值函数:

\max(x,y,z,\dots)=\lim_{k\to +\infty} \frac{1}{k}\ln(e^{kx}+e^{ky}+e^{kz}+\dots)\

\max(x,y)=\lim_{k\to+\infty} \sqrt[2k+1]{x^{2k+1}+y^{2k+1}}\

当然,(13) 的精度(或者说收敛速度)远没有 (11) 那么好,要提高精度也不难,比如

\max(x,y)=\lim_{k\to +\infty} \frac{1}{k}\ln\ln\left(e^{e^{kx}}+e^{e^{ky}}\right)\

综合精度和简洁两方面考虑,估计最优的选择就是 (11) 了。

A:triplet就是pair-wise版的 hinge loss;softmax的pair-wise版叫

LogSumExp函数值是大于等于max函数值的,而且等于取到的条件也是非常苛刻的,所以使用LogSumExp函数相当于变相地加了一定的 。但这往往还是不够的,我们可以选择跟hinge loss一样添加一个 ,那样效果应该也会不错,不过softmax交叉熵损失走的是另一条路:继续smooth。

注意到ReLU函数 也有一个smooth版,即softplus函数 。使用softplus函数之后,即使 超过了LogSumExp函数,仍会得到一点点梯度让 继续上升,这样其实也是变相地又增加了一点 ,使得泛化性能有了一定的保障。替换之后就可以得到:

关于 (11) 式更多的展示,请阅读Matrix67的《如何构造一个平滑的最大值函数》: 观察 (11) 式的结构可以看出,这实际上是做了这样的一个事情:找一个在整个实数域上都单调递增的函数,而且增长速度要快于线性增长,然后求和,最后取逆函数。因此,不难构造出类似的函数:我们选 y=x2k+1y=x^{2k+1}y=x2k+1 ,那么得到

Neighbourhood components analysis
http://www.matrix67.com/blog/archives/2830
Logo从最优化的角度看待Softmax损失函数知乎专栏
Logo寻求一个光滑的最大值函数 - 科学空间|Scientific Spaces
\max(x,0)
\log(1+e^x)
z_y
z_y
m
m
m