public static Array <Real> CombineWithBias(this Array <Real> t, Array <Real> x, Array <Real> y, Array <Real> result = null, Real beta = 0) { if (t.Shape.Length != 3 && x.Shape.Length != 1 && y.Shape.Length != 1) { throw new ArgumentException(); } if (t.Shape[2] != x.Shape[0] + 1 && t.Shape[1] != y.Shape[0] + 1) { throw new ArgumentException(); } result = t[_, Slicer.Upto(-1), Slicer.Upto(-1)].Combine21(x, y, result: result); // TODO check this mess //var biasY = t[_, Slicer.Until(-1), -1].Dot(y); t[_, Slicer.Upto(-1), -1].Dot(y, result: result, beta: 1); //Doesn't work actually //int offY = y.offset[0]; //int offT = t.offset[0] + t.offset[1] + t.offset[2] + (t.Shape[2] - 1) * t.Stride[2]; //for (int j = 0; j < y.Shape[0]; ++j) //{ // Blas.axpy(t.Shape[0], y.Values[offY], t.Values, offT, t.Stride[0], result.Values, result.offset[0], result.Stride[0]); // offY += y.Stride[0]; // offT += t.Stride[1]; //} //var biasX = t[_, -1, Slicer.Until(-1)].Dot(x); t[_, -1, Slicer.Upto(-1)].Dot(x, result: result, beta: 1); //var biasXY = t[_, -1, -1]; result.Acc(t[_, -1, -1]); //result = result + biasX + biasY + biasXY; return(result); }
// result = a + alpha * b public static Array <Int> Add(this Array <Int> a, Array <Int> b, Int alpha = 1, Array <Int> result = null) { if (result == a) { return(a.Acc(b, alpha)); } return(Array_.ElementwiseOp(a, b, result, (n, x, offsetx, incx, y, offsety, incy, z, offsetz, incz) => { for (int i = 0; i < n; i++) { z[offsetz] = x[offsetx] + alpha * y[offsety]; offsetx += incx; offsety += incy; offsetz += incz; } })); }
/// <summary> a.Sum(axis = 1)[i] = α * Σ_j a[i, j] </summary> public static Array <Real> Sum(this Array <Real> a, int axis, Array <Real> result = null, Real alpha = 1, Real beta = 0, bool keepDims = false) { if (axis < 0) { axis = a.Shape.Length + axis; } if (beta != 1 && result != null) { result.Scale(beta, result: result); } result = result ?? NN.Zeros <Real>(GetAggregatorResultShape(a, axis, keepDims)); foreach (var row in a.UnsafeRows(axis, keepDims)) { result.Acc(row, alpha: alpha); } return(result); }
public static Array <Int> Sum(this Array <Int> a, int axis, Array <Int> result = null, Int alpha = 1, Int beta = 0, bool keepDims = false) { if (axis < 0) { axis = a.Shape.Length + axis; } if (beta != 1 && result != null) { result.Scale(beta, result: result); } if (result == null) { int[] shape = new int[a.Shape.Length]; Array.Copy(a.Shape, 0, shape, 0, a.NDim); shape[axis] = 1; result = NN.Zeros <Int>(shape); } var slice = a.Slices(); // TODO: create a new ElementwiseOp for (int d = 0; d < a.Shape[axis]; ++d) { slice[axis] = (d, d + 1); result.Acc(a[slice], alpha: alpha); } if (!keepDims) { var resultShape = new int[a.NDim - 1]; Array.Copy(a.Shape, 0, resultShape, 0, axis); if (axis < a.NDim - 1) { Array.Copy(a.Shape, axis + 1, resultShape, axis, a.NDim - axis - 1); } result = result.Reshape(resultShape); } return(result); }
public static Array <Real> DotWithBias(this Array <Real> a, Array <Real> b, Array <Real> result = null, Real alpha = 1, Real beta = 0) { result = a[a.SlicesWithoutBias()].Dot(b, result, alpha: alpha, beta: beta); result.Acc(a[a.BiasSlice()], alpha); return(result); }