Пример #1
0
 void apply(cube<real>& v, real bias) noexcept override
 {
     real* d = v.data();
     size_t  n = v.num_elements();
     for ( size_t i = 0; i < n; ++i )
         d[i] = f_(d[i] + bias);
 }
Пример #2
0
 typename std::enable_if<has_public_member_grad<T>::value>::type
 apply_grad(cube<real>& g, const cube<real>& f, const T& fn) noexcept
 {
     real* gp = g.data();
     const real* fp = f.data();
     size_t  n = g.num_elements();
     for ( size_t i = 0; i < n; ++i )
         gp[i] *= fn.grad(fp[i]);
 }
Пример #3
0
inline void convolve_constant( cube<T> const & a,
                               identity_t<T> b,
                               cube<T> & r) noexcept
{
    ZI_ASSERT(size(a)==size(r));

    T const * ap = a.data();
    T * rp = r.data();

    for ( size_t i = 0; i < r.num_elements(); ++i )
        rp[i] = ap[i] * b;
}
Пример #4
0
inline T convolve_constant_flipped( cube<T> const & a,
                                    cube<T> const & b ) noexcept
{
    ZI_ASSERT(size(a)==size(b));

    T r = 0;
    T const * ap = a.data();
    T const * bp = b.data();

    for ( size_t i = 0; i < a.num_elements(); ++i )
        r += ap[i] * bp[i];

    return r;
}
Пример #5
0
    // performs inplace dropout backward
    inline void dropout_backward(cube<real> & g)
    {
        ZI_ASSERT(mask_);

        size_t s = g.num_elements();
        for ( size_t i = 0; i < s; ++i )
        {
            if ( mask_->data()[i] )
                g.data()[i] *= scale();
            else
                g.data()[i]  = static_cast<real>(0);
        }

        // Should we reset mask_ here?
    }
Пример #6
0
    // performs inplace dropout and returns dropout mask
    inline void dropout_forward(cube<real>& f)
    {
        if ( !mask_ )
        {
            mask_ = get_cube<bool>(size(f));
        }

        // new random mask
        bernoulli_init<bool>(ratio_).initialize(mask_);

        size_t s = f.num_elements();
        for ( size_t i = 0; i < s; ++i )
        {
            // dropout
            if ( mask_->data()[i] )
                f.data()[i] *= scale();
            else
                f.data()[i]  = static_cast<real>(0);
        }
    }
Пример #7
0
  std::tuple<real,real,cube_p<real>> square_loss( cube<real> const & cprop,
                                                  cube<real> const & clab )
    {
        std::tuple<real,real,cube_p<real>> ret;
        std::get<0>(ret) = 0;
        std::get<1>(ret) = 0;
        std::get<2>(ret) = get_copy(cprop);

        real* grad = std::get<2>(ret)->data();
        const real* lab  = clab.data();

        long_t n = clab.num_elements();

        for ( long_t i = 0; i < n; ++i )
        {
            std::get<1>(ret) += ( grad[i] > 0.5 ? ( lab[i] > 0.5 ? 0 : 1 ) : ( lab[i] > 0.5 ? 1 : 0 ) );
            grad[i] -= lab[i];
            std::get<0>(ret) += grad[i]*grad[i];
            grad[i] *= 2;
        }

        return ret;
    }