|
|
|
@ -941,59 +941,6 @@ void GpuMatrix::softreluDerivative(Matrix& output) {
|
|
|
|
|
void GpuMatrix::scaledTanh(Matrix& output, real p1, real p2) {
|
|
|
|
|
BaseMatrix::scaledTanh(output, p1, p2);
|
|
|
|
|
}
|
|
|
|
|
void GpuMatrix::cosSim(Matrix& output1, Matrix& output2, real scale) {
|
|
|
|
|
CHECK(output1.useGpu_ == true && output2.useGpu_ == true)
|
|
|
|
|
<< "Matrix type are not equal";
|
|
|
|
|
size_t numSamples = getHeight();
|
|
|
|
|
size_t dim = output1.getWidth();
|
|
|
|
|
CHECK_EQ(getWidth(), 1UL);
|
|
|
|
|
CHECK_EQ(output1.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(output1.getWidth(), output2.getWidth());
|
|
|
|
|
real* out = getData();
|
|
|
|
|
real* x = output1.getData();
|
|
|
|
|
real* y = output2.getData();
|
|
|
|
|
hl_cossim(out, x, y, dim, output1.getHeight(), output2.getHeight(), scale);
|
|
|
|
|
}
|
|
|
|
|
void GpuMatrix::cosSimDerivative(Matrix& output,
|
|
|
|
|
Matrix& prevOut1,
|
|
|
|
|
Matrix& prevOut2,
|
|
|
|
|
Matrix& prevGrad1,
|
|
|
|
|
Matrix& prevGrad2,
|
|
|
|
|
real scale) {
|
|
|
|
|
CHECK(output.useGpu_ == true && prevOut1.useGpu_ == true &&
|
|
|
|
|
prevOut2.useGpu_ == true && prevGrad1.useGpu_ == true &&
|
|
|
|
|
prevGrad2.useGpu_ == true)
|
|
|
|
|
<< "Matrix type are not equal";
|
|
|
|
|
CHECK_EQ(getWidth(), 1UL);
|
|
|
|
|
CHECK_EQ(output.getWidth(), 1UL);
|
|
|
|
|
|
|
|
|
|
size_t numSamples = getHeight();
|
|
|
|
|
CHECK_EQ(output.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(prevOut1.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(prevGrad1.getHeight(), numSamples);
|
|
|
|
|
|
|
|
|
|
size_t dim = prevOut1.getWidth();
|
|
|
|
|
CHECK_EQ(prevOut2.getWidth(), dim);
|
|
|
|
|
CHECK_EQ(prevGrad1.getWidth(), dim);
|
|
|
|
|
CHECK_EQ(prevGrad2.getWidth(), dim);
|
|
|
|
|
|
|
|
|
|
real* grad = getData();
|
|
|
|
|
real* out = output.getData();
|
|
|
|
|
real* prevOutX = prevOut1.getData();
|
|
|
|
|
real* prevOutY = prevOut2.getData();
|
|
|
|
|
real* prevGradX = prevGrad1.getData();
|
|
|
|
|
real* prevGradY = prevGrad2.getData();
|
|
|
|
|
hl_cossim_derivative(grad,
|
|
|
|
|
out,
|
|
|
|
|
prevOutX,
|
|
|
|
|
prevOutY,
|
|
|
|
|
prevGradX,
|
|
|
|
|
prevGradY,
|
|
|
|
|
dim,
|
|
|
|
|
prevOut1.getHeight(),
|
|
|
|
|
prevOut2.getHeight(),
|
|
|
|
|
scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GpuMatrix::randomizeUniform() {
|
|
|
|
|
CHECK(isContiguous());
|
|
|
|
@ -3470,105 +3417,6 @@ void CpuMatrix::softmaxDerivative(Matrix& output, Matrix& sftmaxSum) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CpuMatrix::cosSim(Matrix& output1, Matrix& output2, real scale) {
|
|
|
|
|
size_t numSamples = getHeight();
|
|
|
|
|
size_t dim = output1.getWidth();
|
|
|
|
|
CHECK_EQ(getWidth(), 1UL);
|
|
|
|
|
CHECK_EQ(output1.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(output1.getWidth(), output2.getWidth());
|
|
|
|
|
|
|
|
|
|
real* out = getData();
|
|
|
|
|
const real* x = output1.getData();
|
|
|
|
|
const real* y = output2.getData();
|
|
|
|
|
size_t yInc = dim;
|
|
|
|
|
if (output2.getHeight() == 1LU) {
|
|
|
|
|
yInc = 0;
|
|
|
|
|
} else {
|
|
|
|
|
CHECK_EQ(output2.getHeight(), numSamples);
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < numSamples; ++i, x += dim, y += yInc) {
|
|
|
|
|
real squareSumX = 0;
|
|
|
|
|
real squareSumY = 0;
|
|
|
|
|
real xy = 0;
|
|
|
|
|
for (size_t j = 0; j < dim; ++j) {
|
|
|
|
|
squareSumX += _square(x[j]);
|
|
|
|
|
squareSumY += _square(y[j]);
|
|
|
|
|
xy += x[j] * y[j];
|
|
|
|
|
}
|
|
|
|
|
CHECK(squareSumX > 0 && squareSumY > 0);
|
|
|
|
|
out[i] = scale * xy / (std::sqrt(squareSumX) * std::sqrt(squareSumY));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CpuMatrix::cosSimDerivative(Matrix& output,
|
|
|
|
|
Matrix& prevOut1,
|
|
|
|
|
Matrix& prevOut2,
|
|
|
|
|
Matrix& prevGrad1,
|
|
|
|
|
Matrix& prevGrad2,
|
|
|
|
|
real scale) {
|
|
|
|
|
CHECK(output.useGpu_ == false) << "Matrix type are not equal";
|
|
|
|
|
|
|
|
|
|
CHECK_EQ(getWidth(), 1UL);
|
|
|
|
|
CHECK_EQ(output.getWidth(), 1UL);
|
|
|
|
|
|
|
|
|
|
size_t numSamples = getHeight();
|
|
|
|
|
CHECK_EQ(output.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(prevOut1.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(prevGrad1.getHeight(), numSamples);
|
|
|
|
|
|
|
|
|
|
size_t dim = prevOut1.getWidth();
|
|
|
|
|
CHECK_EQ(prevOut2.getWidth(), dim);
|
|
|
|
|
CHECK_EQ(prevGrad1.getWidth(), dim);
|
|
|
|
|
CHECK_EQ(prevGrad2.getWidth(), dim);
|
|
|
|
|
|
|
|
|
|
const real* grad = getData();
|
|
|
|
|
const real* out = output.getData();
|
|
|
|
|
const real* prevOutX = prevOut1.getData();
|
|
|
|
|
const real* prevOutY = prevOut2.getData();
|
|
|
|
|
real* prevGradX = prevGrad1.getData();
|
|
|
|
|
real* prevGradY = prevGrad2.getData();
|
|
|
|
|
size_t yInc = dim;
|
|
|
|
|
if (prevOut2.getHeight() == 1LU) {
|
|
|
|
|
yInc = 0;
|
|
|
|
|
CHECK_EQ(prevGrad2.getHeight(), 1LU);
|
|
|
|
|
} else {
|
|
|
|
|
CHECK_EQ(prevOut2.getHeight(), numSamples);
|
|
|
|
|
CHECK_EQ(prevGrad2.getHeight(), numSamples);
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < numSamples; ++i,
|
|
|
|
|
prevOutX += dim,
|
|
|
|
|
prevOutY += yInc,
|
|
|
|
|
prevGradX += dim,
|
|
|
|
|
prevGradY += yInc) {
|
|
|
|
|
real squareSumX = 0;
|
|
|
|
|
real squareSumY = 0;
|
|
|
|
|
real xy = 0;
|
|
|
|
|
for (size_t j = 0; j < dim; ++j) {
|
|
|
|
|
squareSumX += _square(prevOutX[j]);
|
|
|
|
|
squareSumY += _square(prevOutY[j]);
|
|
|
|
|
xy += prevOutX[j] * prevOutY[j];
|
|
|
|
|
}
|
|
|
|
|
CHECK(squareSumX > 0 && squareSumY > 0);
|
|
|
|
|
if (xy == 0) {
|
|
|
|
|
real reciprocal = 1.0f / (std::sqrt(squareSumX) * std::sqrt(squareSumY));
|
|
|
|
|
for (size_t j = 0; j < dim; ++j) {
|
|
|
|
|
prevGradX[j] += scale * grad[i] * prevOutY[j] * reciprocal;
|
|
|
|
|
prevGradY[j] += scale * grad[i] * prevOutX[j] * reciprocal;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
real reciprocalXY = 1.0f / xy;
|
|
|
|
|
real reciprocalSquareSumX = 1.0f / squareSumX;
|
|
|
|
|
real reciprocalSquareSumY = 1.0f / squareSumY;
|
|
|
|
|
for (size_t j = 0; j < dim; ++j) {
|
|
|
|
|
prevGradX[j] += out[i] * grad[i] * (prevOutY[j] * reciprocalXY -
|
|
|
|
|
prevOutX[j] * reciprocalSquareSumX);
|
|
|
|
|
prevGradY[j] += out[i] * grad[i] * (prevOutX[j] * reciprocalXY -
|
|
|
|
|
prevOutY[j] * reciprocalSquareSumY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CpuMatrix::sumOfSquares(Matrix& output, Matrix& label) {
|
|
|
|
|
CHECK(output.useGpu_ == false && label.useGpu_ == false)
|
|
|
|
|
<< "Matrix type are not equal";
|
|
|
|
|