forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftMarginCriterion.cu
65 lines (57 loc) · 1.9 KB
/
SoftMarginCriterion.cu
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <THCUNN/THCUNN.h>
#include <THCUNN/common.h>
#include <TH/THHalf.h>
#include <THCUNN/THCHalfAutoNumerics.cuh>
#include <THC/THCApply.cuh>
#include <thrust/fill.h>
#include <thrust/functional.h>
#include <thrust/device_ptr.h>
#include <thrust/reduce.h>
#include <thrust/inner_product.h>
template <typename Dtype, typename Acctype>
struct softmargin_functor
{
__host__ __device__ Acctype operator()(const Dtype& x, const Dtype& y) const
{
return log(1 + exp(ScalarConvert<Dtype, Acctype>::to(-x)*y));
}
};
template <typename Dtype, typename Acctype>
struct softmargin_no_reduce_functor
{
__host__ __device__ void operator()(
const Dtype *x,
const Dtype *y,
Dtype *out) const
{
*out = ScalarConvert<Acctype, Dtype>::to(log(ScalarConvert<int, Acctype>::to(1)
+ exp(ScalarConvert<Dtype, Acctype>::to(-*x) * *y)));
}
};
template <typename Dtype, typename Acctype>
struct softmargin_updateGradInput_functor
{
const Acctype norm;
const Dtype gradOutput;
softmargin_updateGradInput_functor(Acctype norm_, Dtype gradOutput_) :
norm(norm_), gradOutput(gradOutput_) {}
__host__ __device__ Dtype operator()(const Dtype& x, const Dtype& y) const
{
Acctype temp = exp(ScalarConvert<Dtype, Acctype>::to(-x)*y);
return ScalarConvert<Acctype, Dtype>::to(-y*temp*norm/(ScalarConvert<int, Acctype>::to(1) + temp) * gradOutput);
}
};
template <typename Dtype, typename Acctype>
struct softmargin_updateGradInput_no_reduce_functor
{
__forceinline__ __host__ __device__ void operator()(
const Dtype *x,
const Dtype *y,
Dtype *gradInput) const
{
Acctype temp = exp(ScalarConvert<Dtype, Acctype>::to(-*x) * *y);
*gradInput = ScalarConvert<Acctype, Dtype>::to(-*y * temp / (ScalarConvert<int, Acctype>::to(1) + temp));
}
};
#include <THCUNN/generic/SoftMarginCriterion.cu>
#include <THC/THCGenerateFloatTypes.h>