add negative clipping for softmax.

update-doc-pybind
caoying03 7 years ago
parent 360bde9a70
commit 3d77360b89

@ -25,6 +25,14 @@ template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
template <typename T>
struct ValueClip {
HOSTDEVICE T operator()(const T& x) const {
const T kThreshold = -64.;
return x < kThreshold ? kThreshold : x;
}
};
template <typename Place, typename T>
class SoftmaxFunctor {
public:
@ -47,7 +55,8 @@ class SoftmaxFunctor {
logits.maximum(along_class)
.eval()
.reshape(batch_by_one)
.broadcast(one_by_class));
.broadcast(one_by_class))
.unaryExpr(ValueClip<T>());
softmax.device(context.GetEigenDevice<Place>()) = shifted_logits.exp();
softmax.device(context.GetEigenDevice<Place>()) =

@ -5,7 +5,7 @@ from op_test import OpTest
def stable_softmax(x):
"""Compute the softmax of vector x in a numerically stable way."""
shiftx = x - np.max(x)
shiftx = x - np.max(x).clip(-64.)
exps = np.exp(shiftx)
return exps / np.sum(exps)

Loading…
Cancel
Save