/// <inheritdoc/> public override void Forward(Span <Tensor> inputs, out Tensor z, out Tensor a) { Tensor.New(inputs[0].Entities, inputs[0].Length, out z); CpuBlas.Sum(inputs, z); Tensor.Like(z, out a); CpuDnn.ActivationForward(z, ActivationFunctions.Activation, a); }
public unsafe void HadamardProductTest() { // Test values float[,] m1 = { { 1, 2, 3 }, { 5, 1, -2 }, { 1, 2, 3 }, }, m2 = { { 5, 2, -1 }, { -5, 2, -7 }, { 1, 2, 2 } }, r = { { 5, 4, -3 }, { -25, 2, 14 }, { 1, 4, 6 } }; fixed(float *pm1 = m1, pm2 = m2) { Tensor.Reshape(pm1, 3, 3, out Tensor m1Tensor); Tensor.Reshape(pm2, 3, 3, out Tensor m2Tensor); Tensor.New(3, 3, out Tensor result); CpuBlas.MultiplyElementwise(m1Tensor, m2Tensor, result); Assert.IsTrue(result.ToArray2D().ContentEquals(r)); result.Free(); } }
public unsafe void SpatialMultiplication() { // Test values float[,] m1 = { { 1, 2, 3 }, { 5, 0.1f, -2 } }, m2 = { { 5, 2, -1, 3 }, { -5, 2, -7, 0.9f }, { 0.1f, 0.2f, -0.1f, 2 } }, r = { { -4.7f, 6.6f, -15.3f, 10.8f }, { 24.3f, 9.7999999999999989f, -5.5f, 11.09f } }; fixed(float *pm1 = m1, pm2 = m2) { Tensor.Reshape(pm1, 2, 3, out Tensor m1Tensor); Tensor.Reshape(pm2, 3, 4, out Tensor m2Tensor); Tensor.New(2, 4, out Tensor result); CpuBlas.Multiply(m1Tensor, m2Tensor, result); Assert.IsTrue(result.ToArray2D().ContentEquals(r)); result.Free(); } }
public unsafe void Transposition() { // Test values float[,] m = { { 1, 1, 1, 1 }, { 0, 2, -1, 0 } }, r = { { 1, 0 }, { 1, 2 }, { 1, -1 }, { 1, 0 } }; fixed(float *pm = m) { Tensor.Reshape(pm, 2, 4, out Tensor mTensor); Tensor.New(4, 2, out Tensor result); CpuBlas.Transpose(mTensor, result); Assert.IsTrue(result.ToArray2D().ContentEquals(r)); result.Free(); } }
public unsafe void LinearMultiplication() { // Test values float[,] m = { { 1, 1, 1, 1 }, { 0, 2, -1, 0 }, { 1, 1, 1, 1 }, { 0, 0, -1, 1 } }; float[] v = { 1, 2, 0.1f, -2 }, r = { 1.1f, 5.1f, 1.1f, -0.9f }; fixed(float *pm = m, pv = v) { Tensor.Reshape(pm, 4, 4, out Tensor mTensor); Tensor.Reshape(pv, 1, 4, out Tensor vTensor); Tensor.New(1, 4, out Tensor rTensor); CpuBlas.Multiply(vTensor, mTensor, rTensor); Assert.IsTrue(rTensor.ToArray().ContentEquals(r)); rTensor.Free(); } }