af::array Kernel::calculateSplineKernel(const af::array& x1, const af::array& x2) { af::array x1_ = x1 / m_lengthScale; af::array x2_ = x2 / m_lengthScale; int rowX1 = x1.dims(0); int colX1 = x1.dims(1); int rowX2 = x2.dims(0); // int colX2 = x2.dims(1); af::array k = af::constant(1.0,rowX1,rowX1); for(int i = 0 ; i < colX1 ; i++) { af::array xx = matmul( x1_(span,i) , x2_(span,i).T()); af::array xx1 = matmul(x1_(span,i) , af::constant(1.0 , 1 , rowX2)); af::array xx2 = matmul(af::constant(1.0, rowX1,1), x2_(span,i).T()); af::array minXX = af::min(xx1,xx2); af::array temp = 1.0 + xx + xx * minXX - (xx1 + xx2)/2.0 * af::pow(minXX,2.0) + af::pow(minXX, 3.0)/3.0; k = k * temp; } if(m_useBias) { return af::join(1,af::constant(1,rowX1,1), k); } else { return k; } }
af::array Kernel::distanceSquared(const af::array& x , const af::array& y) { int nx = x.dims(0); int ny = y.dims(0); af::array arg1 = af::matmul(af::sum(af::pow(x,2.0),1), af::constant(1.0,1,ny)); af::array arg2 = af::matmul(af::constant(1.0,nx,1), af::sum(af::pow(y,2.0),1).T()); af::array arg3 = 2.0 * (af::matmul(x,y.T())); af::array values = arg1 + arg2 - arg3; return values; }
void MLP::setWeights(const af::array& weights) { if (weights.dims() != this->weights.dims()) { BOOST_THROW_EXCEPTION(InvalidArgumentError() << EInfo_Name("weights")); } this->weights = weights; }
af::array Kernel::calculateGaussianKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); float eta = 1.0/(m_lengthScale*m_lengthScale); af::array temp = af::exp(-eta * distanceSquared(x1,x2) / 2.0); return m_useBias ? af::join(1, af::constant(1.0,rowX1,1),temp) : temp; }
af::array Kernel::calculateHomogeneousPolynomialKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); double eta = 1/(m_lengthScale*m_lengthScale); af::array temp = af::pow(eta * af::matmul(x1, x2.T()) , m_polynomailPower); return m_useBias ? af::join(1, af::constant(1.0,rowX1,1),temp) : temp; }
af::array Kernel::calculateDistanceKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); double eta = 1.0/(m_lengthScale*m_lengthScale); af::array temp = sqrt(eta)*sqrt(distanceSquared(x1,x2)); return m_useBias ? af::join(1, af::constant(1.0,rowX1,1),temp) : temp; }
af::array Kernel::calculatePolynomialKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); double eta = 1.0/(m_lengthScale*m_lengthScale); af::array temp = af::pow(af::matmul(x1,(eta * x2).T()) + 1.0, m_polynomailPower); af_print(af::sum( af::sum( af::isNaN(temp) )),1); return m_useBias ? af::join(1, af::constant(1.0,rowX1,1),temp) : temp; }
af::array Kernel::calculateBubbleKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); double eta = 1/(m_lengthScale*m_lengthScale); af::array r2 = eta*distanceSquared(x1,x2) ; if(m_useBias) { return af::join(1, af::constant(1,rowX1,1), r2 < 1); } else { return r2 < 1; } }
af::array Kernel::calculateThinPlateSplineKernel(const af::array& x1, const af::array& x2) { int rowX1 = x1.dims(0); double eta = 1.0/(m_lengthScale*m_lengthScale); af::array r2 = eta*distanceSquared(x1,x2); if(m_useBias) { return af::join(1, af::constant(1,rowX1,1), 0.5 * r2 * af::log(r2 + (r2 == 0))); } else { return 0.5 * r2 * af::log(r2 + (r2 == 0)); } }
virtual af::array dfn(const af::array &val) { return af::constant(1, val.dims()); }