-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsoftmax.cpp
50 lines (47 loc) · 896 Bytes
/
softmax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>
#include <vector>
#include <random>
#include<dCpp.h>
namespace dCpp
{
namespace examples
{
//simply code the map existing in the programming space dCpp
//and the belonging algebra
std::vector<var> softmax(const std::vector<var>& V)
{
std::vector<var> out;
var sum(0);
init(sum);
for(var v:V)
{
sum=sum+exp(v);
}
for(var v:V)
{
out.push_back(exp(v)/sum);
}
return out;
}
void softmax()
{
// initiaize Virtual memory of fourth order
initSpace(4);
//get a vector of variables
int size=2;
std::vector<var> vars;
for(int i=1; i<=size; i++)
{
var tmp=var(i);
init(tmp);
vars.push_back(tmp);
}
// use the softmax function
std::vector<var> f=softmax(vars);
// display derivatives of all four orders
// of one of the components
f[1].print();
}
}
}