C++实现softmax函数的面试经验

作者:concyclics 时间:2023-06-16 02:07:47 

背景

今天面试字节算法岗时被问到的问题,让我用C++实现一个softmax函数。softmax是逻辑回归在多分类问题上的推广。大概的公式如下:

C++实现softmax函数的面试经验

即判断该变量在总体变量中的占比。

第一次实现

实现

我们用vector来封装输入和输出,简单的按公式复现。

vector<double> softmax(vector<double> input)
{
double total=0;
for(auto x:input)
{
total+=exp(x);
}
vector<double> result;
for(auto x:input)
{
result.push_back(exp(x)/total);
}
return result;
}

测试

test 1

  • 测试用例1: {1, 2, 3, 4, 5}

  • 测试输出1: {0.0116562, 0.0316849, 0.0861285, 0.234122, 0.636409}

经过简单测试是正常的。

C++实现softmax函数的面试经验

test 2

但是这时面试官提出了一个问题,即如果有较大输入变量时会怎么样?

  • 测试用例2: {1, 2, 3, 4, 5, 1000}

  • 测试输出2: {0, 0, 0, 0, 0, nan}

由于 e^1000已经溢出了双精度浮点(double)所能表示的范围,所以变成了NaN(not a number)。

C++实现softmax函数的面试经验

第二次实现(改进)

改进原理

我们注意观察softmax的公式:

C++实现softmax函数的面试经验

如果我们给上下同时乘以一个很小的数,最后答案的值是不变的。

那我们可以给每一个输入 x i 都减去一个值 a ,防止爆精度。

大致表示如下:

C++实现softmax函数的面试经验

实现

vector<double> softmax(vector<double> input)
{
double total=0;
double MAX=input[0];
for(auto x:input)
{
MAX=max(x,MAX);
}
for(auto x:input)
{
total+=exp(x-MAX);
}
vector<double> result;
for(auto x:input)
{
result.push_back(exp(x-MAX)/total);
}
return result;
}

测试

test 1

  • 测试用例1: {1, 2, 3, 4, 5, 1000}

  • 测试输出1: {0, 0, 0, 0, 0, 1}

C++实现softmax函数的面试经验

test 2

  • 测试用例1: {0, 19260817, 19260817}

  • 测试输出1: {0, 0.5, 0.5}

C++实现softmax函数的面试经验

我们发现结果正常了。

完整代码

#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
vector<double> softmax(vector<double> input)
{
double total=0;
double MAX=input[0];
for(auto x:input)
{
MAX=max(x,MAX);
}
for(auto x:input)
{
total+=exp(x-MAX);
}
vector<double> result;
for(auto x:input)
{
result.push_back(exp(x-MAX)/total);
}
return result;
}
int main(int argc, char *argv[])
{
int n;
cin>>n;
vector<double> input;
while(n--)
{
double x;
cin>>x;
input.push_back(x);
}
for(auto y:softmax(input))
{
cout<<y<<' ';
}
}

来源:https://blog.csdn.net/qq_21008741/article/details/124496100

标签:C++,softmax,函数,面试经验
0
投稿

猜你喜欢

  • Android简易音乐播放器实现代码

    2021-12-22 23:26:53
  • MybatisPlus #{param}和${param}的用法详解

    2023-02-02 13:08:10
  • Spring AOP如何整合redis(注解方式)实现缓存统一管理详解

    2023-11-19 06:09:27
  • Java 抽象类定义与方法实例详解

    2022-10-20 09:26:38
  • C#深浅拷贝的深入解析

    2023-03-28 18:36:28
  • C#验证控件validator的简单使用

    2023-04-26 06:05:31
  • C#实现组合排列的方法

    2021-10-04 02:25:01
  • 一文搞懂JMeter engine中HashTree的配置问题

    2022-05-11 01:21:08
  • 使用Flutter开发的抖音国际版实例代码详解

    2023-11-12 09:38:44
  • JAVA实现301永久重定向方法

    2023-12-18 10:59:08
  • 清除aspx页面缓存的程序实现方法

    2021-10-28 06:16:57
  • android预置默认的语音信箱号码具体实现

    2022-08-22 07:51:59
  • Flutter数据库的使用方法

    2022-02-04 04:18:02
  • C#使用WebService结合jQuery实现无刷新翻页的方法

    2022-02-23 12:00:32
  • Javassist如何操作Java 字节码

    2021-08-09 08:21:28
  • 深入浅析jni中的java接口使用

    2023-07-22 19:54:23
  • 1小时快速上手RabbitMQ(简介及安装过程)

    2022-04-26 15:49:40
  • 教你轻松制作java视频播放器

    2023-06-25 06:34:05
  • C#中async和await的深入分析

    2023-09-14 17:11:20
  • JAVA算法起步之插入排序实例

    2021-05-30 15:52:30
  • asp之家 软件编程 m.aspxhome.com