public static Tensor SRN( this Session session, Tensor x, Tensor w, Tensor u, Tensor b, int numberOfNeurons, MatrixLayout matrixLayout) { const string ActionName = "srn"; // calculate gates = W * x + b Tensor y = session.FullyConnected(x, w, b, matrixLayout); int tt = y.Shape.GetAxis(Axis.B); // number of vectors in time sequence float[] uw = u.Weights; float[] yw = y.Weights; // add hidden layer to the output tensor // y += U * y(t-1) (product of hidden weight matrix and hidden vector) Nonlinearity.ReLU(numberOfNeurons, yw, 0, yw, 0); for (int t = 1, yi = numberOfNeurons; t < tt; t++, yi += numberOfNeurons) { Matrix.MxV(matrixLayout, numberOfNeurons, numberOfNeurons, uw, 0, false, yw, yi - numberOfNeurons, yw, yi, false); // TODO: customize activation function Nonlinearity.ReLU(numberOfNeurons, yw, yi, yw, yi); } if (session.CalculateGradients) { session.Push( ActionName, () => { float[] duw = u.Gradient; float[] dyw = y.Gradient; for (int t = tt - 1, yi = t * numberOfNeurons; t > 0; t--, yi -= numberOfNeurons) { Nonlinearity.ReLUGradient(numberOfNeurons, dyw, yi, true, yw, yi, dyw, yi); // dA += dy * x' lock (u) { Matrix.VxV(matrixLayout, numberOfNeurons, numberOfNeurons, dyw, yi, yw, yi - numberOfNeurons, duw, 0, false); } // dx += A' * dy Matrix.MxV(matrixLayout, numberOfNeurons, numberOfNeurons, uw, 0, true, dyw, yi, dyw, yi - numberOfNeurons, false); } Nonlinearity.ReLUGradient(numberOfNeurons, dyw, 0, true, yw, 0, dyw, 0); }); } return(y); }
/// <summary>Test serialization.</summary> private void TestSerialization(Context context, VariantType[] valueTypes, MatrixLayout layout) { // Create and resize int rowCount = 3; int colCount = Math.Max(valueTypes.Length, 4); var originalMatrix = new VariantMatrix(); originalMatrix.Resize(layout, rowCount, colCount); PopulateHeaders(originalMatrix); PopulateValues(valueTypes, originalMatrix); // Serialize the generated table and save serialized string to file string originalNoHeadersString = originalMatrix.ToString(); context.Log.Verify($"{layout}", originalNoHeadersString); // Deserialize from string back into table var parsedNoHeadersMatrix = new VariantMatrix(); parsedNoHeadersMatrix.ParseCsv(layout, valueTypes, originalNoHeadersString); string parsedNoHeadersString = parsedNoHeadersMatrix.ToString(); // Compare serialized strings Assert.Equal(originalNoHeadersString, parsedNoHeadersString); }
public static extern int LAPACKE_dgeev( MatrixLayout matrix_layout, byte jobvl, byte jobvr, int n, double[] a, int lda, double[] wr, double[] wi, double[] vl, int ldvl, double[] vr, int ldvr);
/// <summary>Populate table headers based on the specified layout.</summary> private void PopulateHeaders(VariantMatrix result) { MatrixLayout layout = result.Layout; // Populate row headers if they are specified by the layout if (layout.HasRowHeaders()) { var rowHeaders = new List <string>(); for (int rowIndex = 0; rowIndex < result.RowCount; rowIndex++) { rowHeaders.Add($"Row{rowIndex}"); } result.RowHeaders = rowHeaders.ToArray(); } // Populate column headers if they are specified by the layout if (layout.HasColHeaders()) { var colHeaders = new List <string>(); for (int colIndex = 0; colIndex < result.ColCount; colIndex++) { colHeaders.Add($"Col{colIndex}"); } result.ColHeaders = colHeaders.ToArray(); } // Populate corner header if it is specified by the layout if (layout.HasCornerHeader()) { result.CornerHeader = "Corner"; } }
/// <summary> /// Initializes the <see cref="SRNCell"/>. /// </summary> /// <param name="shape">The shape of the layer's input tensor.</param> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="random">The random numbers generator.</param> private void Initialize( Shape shape, RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout, RandomNumberGenerator <float> random) { if (shape == null) { throw new ArgumentNullException(nameof(shape)); } int[] axes = shape.Axes; // column-major matrix organization - each row contains all weights for one neuron // row-major matrix organization - each column contains all weights for one neuron int xlen = axes.Skip(1).Aggregate(1, (total, next) => total * next); int[] weightsShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { xlen, numberOfNeurons } : new[] { numberOfNeurons, xlen }; int hlen = direction == RNNDirection.ForwardOnly ? numberOfNeurons : numberOfNeurons / 2; int[] hiddenShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { hlen, numberOfNeurons } : new[] { numberOfNeurons, hlen }; int[] biasesShape = new[] { numberOfNeurons }; this.Initialize(direction, numberOfNeurons, matrixLayout, weightsShape, hiddenShape, biasesShape, random); this.OutputShape = new Shape(new int[] { shape.GetAxis(0), numberOfNeurons }); }
/// <summary> /// Initializes the <see cref="StochasticLayer"/>. /// </summary> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="weightsShape">The dimensions of the layer's weights tensor.</param> /// <param name="hiddenShape">The dimensions of the layer's hidden weights tensor.</param> /// <param name="biasesShape">The dimensions of the layer's biases tensor.</param> /// <param name="random">The random numbers generator.</param> private protected void Initialize( RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout, int[] weightsShape, int[] hiddenShape, int[] biasesShape, RandomNumberGenerator <float> random) { if (hiddenShape == null) { throw new ArgumentNullException(nameof(hiddenShape)); } if (random == null) { random = new RandomRangeGenerator(-0.08f, 0.08f); } if (direction == RNNDirection.BiDirectional && (numberOfNeurons % 2) != 0) { throw new ArgumentException("The number of neurons in a bi-directional RNN must be even.", nameof(numberOfNeurons)); } this.Direction = direction; this.Initialize(numberOfNeurons, matrixLayout, weightsShape, biasesShape, random); this.U = new Tensor("hidden weights", hiddenShape); this.U.Randomize(random); }
public static byte[] ToArray(Mat input, MatrixLayout layout = MatrixLayout.RowMajor) { var step = input.ElementSize * input.Cols; var data = new byte[step * input.Rows]; var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); try { Mat dataHeader; switch (layout) { case MatrixLayout.ColumnMajor: dataHeader = new Mat(input.Cols, input.Rows, input.Depth, input.Channels, dataHandle.AddrOfPinnedObject(), input.ElementSize * input.Rows); CV.Transpose(input, dataHeader); break; default: case MatrixLayout.RowMajor: dataHeader = new Mat(input.Rows, input.Cols, input.Depth, input.Channels, dataHandle.AddrOfPinnedObject(), step); CV.Copy(input, dataHeader); break; } } finally { dataHandle.Free(); } return(data); }
/// <summary> /// Initializes the <see cref="StochasticLayer"/>. /// </summary> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="weightsShape">The dimensions of the layer's weights tensor.</param> /// <param name="biasesShape">The dimensions of the layer's biases tensor.</param> /// <param name="random">The random numbers generator.</param> private protected void Initialize( int numberOfNeurons, MatrixLayout matrixLayout, int[] weightsShape, int[] biasesShape, RandomNumberGenerator <float> random) { if (weightsShape == null) { throw new ArgumentNullException(nameof(weightsShape)); } if (biasesShape == null) { throw new ArgumentNullException(nameof(biasesShape)); } this.NumberOfNeurons = numberOfNeurons; this.MatrixLayout = matrixLayout; this.W = new Tensor("weights", weightsShape); this.W.Randomize(random ?? new GaussianGenerator(0.0, Math.Sqrt((double)numberOfNeurons / this.W.Length))); this.B = new Tensor("biases", biasesShape); }
public MatrixDescriptor( int rows, int columns, int stride, MatrixLayout layout = MatrixLayout.RowMajor) { Requires.Positive(rows, nameof(rows)); Requires.Positive(columns, nameof(columns)); switch (layout) { case MatrixLayout.RowMajor: Requires.Range(stride, nameof(stride), stride >= columns); break; case MatrixLayout.ColumnMajor: Requires.Range(stride, nameof(stride), stride >= rows); break; default: throw new ArgumentOutOfRangeException(nameof(layout)); } Rows = rows; Columns = columns; Stride = stride; Layout = layout; }
public BandMatrixDescriptor( int rows, int columns, int upperBandwidth, int lowerBandwidth, int stride, MatrixLayout layout = MatrixLayout.RowMajor) { Requires.Positive(rows, nameof(rows)); Requires.Positive(columns, nameof(columns)); Requires.NonNegative(upperBandwidth, nameof(upperBandwidth)); Requires.Range(upperBandwidth, nameof(upperBandwidth), upperBandwidth < columns); Requires.NonNegative(lowerBandwidth, nameof(lowerBandwidth)); Requires.Range(lowerBandwidth, nameof(lowerBandwidth), lowerBandwidth < rows); Requires.Range(stride, nameof(stride), stride >= upperBandwidth + lowerBandwidth + 1); Requires.Range( layout, nameof(layout), layout == MatrixLayout.RowMajor || layout == MatrixLayout.ColumnMajor); Rows = rows; Columns = columns; UpperBandwidth = upperBandwidth; LowerBandwidth = lowerBandwidth; Stride = stride; Layout = layout; }
/// <summary> /// Populate by parsing multi-line CSV text using the specified /// matrix layout and an array of column parser functions. /// /// If the data has more columns than the array of parsers, /// the last parser in the array is used for all additional /// columns. This permits ingesting CSV files with an unknown /// number of value columns of the same type, after an initial /// set of category columns that have other types. /// /// The specified parser is not used for row and column headers /// which are always dot delimited strings. /// </summary> public void ParseCsv(MatrixLayout layout, VariantType[] colTypes, string csvText) { // Create and populate an array of column parser functions var parsers = new Func <string, object> [colTypes.Length]; for (int i = 0; i < parsers.Length; i++) { // It is important to copy and pass ValueType by value // rather than as array and element index, because the // array may change by the time the expression is invoked VariantType colType = colTypes[i]; parsers[i] = value => Variant.Parse(colType, value).Value; } // Parse CSV text ParseCsv(layout, parsers, csvText); // Populate the array of column value types using the actual matrix size, // padding with the last value of the argument array ColTypes = new VariantType[ColCount]; for (int i = 0; i < ColTypes.Length; i++) { if (i < colTypes.Length) { ColTypes[i] = colTypes[i]; } else { ColTypes[i] = colTypes[colTypes.Length - 1]; } } }
public static extern int LAPACKE_dggev( MatrixLayout matrix_layout, byte jobvl, byte jobvr, int n, double[] a, int lda, double[] b, int ldb, double[] alphar, double[] alphai, double[] beta, double[] vl, int ldvl, double[] vr, int ldvr);
/// <summary> /// Indicates that table has column headers, irrespective of /// whether or not it also has row headers. /// </summary> public static bool HasColHeaders(this MatrixLayout obj) { if (obj == MatrixLayout.Empty) { throw new Exception("Matrix layout is empty"); } return(obj == MatrixLayout.ColHeaders || obj == MatrixLayout.RowAndColHeaders); }
/// <summary> /// Initializes a new instance of the <see cref="ConvolutionLayer"/> class. /// </summary> /// <param name="shape">The shape of the layer's input tensor.</param> /// <param name="numberOfFilters">The number of filters in the layer.</param> /// <param name="kernel">The convolution kernel.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="random">The random numbers generator.</param> public ConvolutionLayer( Shape shape, int numberOfFilters, Kernel kernel, MatrixLayout matrixLayout, RandomNumberGenerator <float> random) { this.Initialize(shape, numberOfFilters, kernel, MatrixLayout.ColumnMajor /*matrixLayout*/, random); }
/// <summary> /// Initializes a new instance of the <see cref="GRUCell"/> class. /// </summary> /// <param name="shape">The shape of the layer's input tensor.</param> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="random">The random numbers generator.</param> public GRUCell( Shape shape, RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout, RandomNumberGenerator <float> random) { this.Initialize(shape, direction, numberOfNeurons, matrixLayout, random); }
/// <summary> /// Initializes a new instance of the <see cref="SRNLayer"/> class. /// </summary> /// <param name="shape">The shape of the layer's input tensor.</param> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the hidden and fully connected layers.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="random">The random numbers generator.</param> public SRNLayer( Shape shape, RNNDirection direction, IList <int> numberOfNeurons, MatrixLayout matrixLayout, RandomNumberGenerator <float> random) { this.Initialize(shape, direction, numberOfNeurons, matrixLayout, random); }
public static Tensor GRU( this Session session, Tensor x, Tensor w, Tensor u, Tensor b, RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout) { const string ActionName = "gru"; // calculate gates = W * x + b Tensor g = session.FullyConnected(x, w, b, matrixLayout); int tt = g.Shape.GetAxis(0); // number of vectors in time sequence return(session.RunOperation( ActionName, () => { bool calculateGradient = session.CalculateGradients; Tensor h = session.AllocateTensor(ActionName, new Shape(new int[] { tt, numberOfNeurons }), calculateGradient); NativeMethods.gru( tt, numberOfNeurons, u.Weights, g.Weights, h.Weights, direction == RNNDirection.BiDirectional, matrixLayout == MatrixLayout.RowMajor); if (calculateGradient) { session.Push( ActionName, () => { NativeMethods.gru_gradient( tt, numberOfNeurons, u.Weights, u.Gradient, g.Weights, g.Gradient, h.Weights, h.Gradient, direction == RNNDirection.BiDirectional, matrixLayout == MatrixLayout.RowMajor); }); } return h; })); }
public static Mat4 RotToYZ(Vec4 p, MatrixLayout layout = MatrixLayout.Default) { E _2 = E.NumConst(2); E projPxyLen = E.Sqrt(E.Sum(E.Pow(p.x, _2), E.Pow(p.y, _2))); E cosA = E.Div(p.x, projPxyLen); E sinA = E.Div(p.y, projPxyLen); return(RotZ(cosA, sinA, layout)); }
/// <summary> /// Create from multi-line CSV text using the specified matrix /// layout and a single parser that will be used for all columns /// except the column of row headers (if present). /// /// The specified parser is not used for row and column headers /// which are always dot delimited strings. /// </summary> public void ParseCsv(MatrixLayout layout, Func <string, T> parser, string csvText) { // Create an array of parsers of size one // The function taking the array will use // its last (and only) element for all of // the data columns var parsers = new Func <string, T>[] { parser }; ParseCsv(layout, parsers, csvText); }
public static void SetTransform(IntPtr scene, Int32 geomID, MatrixLayout layout, float *transform) { NativeMethods.rtcSetTransform(scene, geomID, layout, transform); CheckLastError(); }
/// <summary> /// Create from multi-line CSV text using the specified matrix /// layout and a single value type that will be used for all columns /// except the column of row headers (if present). /// /// The specified value type is not used for row and column headers /// which are always dot delimited strings. /// </summary> public void ParseCsv(MatrixLayout layout, VariantType valueType, string csvText) { // Create an array of parsers of size one // The function taking the array will use // its last (and only) element for all of // the data columns var valueTypes = new VariantType[] { valueType }; // Parse and populate the array of column value types ParseCsv(layout, valueTypes, csvText); }
public MatrixDescriptor( int rows, int columns, MatrixLayout layout = MatrixLayout.RowMajor) : this( rows, columns, layout == MatrixLayout.RowMajor ? columns : rows, layout) { }
internal static float[] CalculateDW(Tensor x, Tensor dy, MatrixLayout matrixLayout) { return(matrixLayout == MatrixLayout.RowMajor ? Enumerable.Range(0, dy.Length).SelectMany(j => { return Enumerable.Range(0, x.Length).Select(i => x[i] * dy[j]); }).ToArray() : Enumerable.Range(0, x.Length).SelectMany(i => { return Enumerable.Range(0, dy.Length).Select(j => x[i] * dy[j]); }).ToArray()); }
/// <summary> /// Initializes the <see cref="GRUCell"/>. /// </summary> /// <param name="shape">The dimensions of the layer's input tensor.</param> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="random">The random numbers generator.</param> private void Initialize( Shape shape, RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout, RandomNumberGenerator <float> random) { if (shape == null) { throw new ArgumentNullException(nameof(shape)); } if (random == null) { random = new RandomRangeGenerator(-0.08f, 0.08f); } int[] axes = shape.Axes; // column-major matrix organization - each row contains all weights for one neuron // row-major matrix organization - each column contains all weights for one neuron int xlen = axes.Skip(1).Aggregate(1, (total, next) => total * next); int[] weightsShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { xlen, 3 * numberOfNeurons } : new[] { 3 * numberOfNeurons, xlen }; // keep all weights in single channel // allocate three matrices (one for each of two gates and one for the state) int[] biasesShape = new[] { 3 * numberOfNeurons }; int hlen = direction == RNNDirection.ForwardOnly ? numberOfNeurons : numberOfNeurons / 2; int[] hiddenShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { hlen, 3 * numberOfNeurons } : new[] { 3 * numberOfNeurons, hlen }; this.Initialize( direction, numberOfNeurons, matrixLayout, weightsShape, hiddenShape, biasesShape, random ?? new RandomRangeGenerator(-0.08f, 0.08f)); this.OutputShape = new Shape(new int[] { shape.GetAxis(0), numberOfNeurons }); // initialize biases for update and reset gates only Vectors.Set(2 * numberOfNeurons, 1.0f, this.B.Weights, 0); }
public static Mat4 RotToZ(Vec4 p, MatrixLayout layout = MatrixLayout.Default) { Mat4 rotToYZ = RotToYZ(p, layout); E _2 = E.NumConst(2); E vLen = E.Sqrt(E.Sum(E.Pow(p.x, _2), E.Pow(p.y, _2), E.Pow(p.z, _2))); E projPxyLen = E.Sqrt(E.Sum(E.Pow(p.x, _2), E.Pow(p.y, _2))); E cosA = E.Div(p.z, vLen); E sinA = E.Div(projPxyLen, vLen); return((rotToYZ * RotX(cosA, sinA, layout)).Evaluate()); }
public BandMatrixDescriptor( int rows, int columns, int upperBandwidth, int lowerBandwidth, MatrixLayout layout = MatrixLayout.RowMajor) : this( rows, columns, upperBandwidth, lowerBandwidth, upperBandwidth + lowerBandwidth + 1, layout) { }
public static MatrixLayout Transpose(this MatrixLayout layout) { switch (layout) { case MatrixLayout.RowMajor: return(MatrixLayout.ColumnMajor); case MatrixLayout.ColumnMajor: return(MatrixLayout.RowMajor); default: Debug.Fail(Strings.Unreachable); throw new ArgumentOutOfRangeException(nameof(layout)); } }
/// <summary> /// Create data containers inside the matrix and populate the values /// with default(T). /// /// This method also creates the row and column headers with the /// correct size, if the respective header is specified in the layout. /// </summary> public void Resize(MatrixLayout layout, int rowCount, int colCount) { Layout = layout; RowCount = rowCount; ColCount = colCount; if (layout.HasRowHeaders()) { RowHeaders = new string[rowCount]; } if (layout.HasColHeaders()) { ColHeaders = new string[colCount]; } }
/// <summary> /// Initializes the <see cref="LSTMCell"/>. /// </summary> /// <param name="shape">The dimensions of the layer's input tensor.</param> /// <param name="direction">The cell direction (forward-only or bi-directional).</param> /// <param name="numberOfNeurons">The number of neurons in the layer.</param> /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param> /// <param name="forgetBias">The bias added to forget gates.</param> /// <param name="random">The random numbers generator.</param> private void Initialize( Shape shape, RNNDirection direction, int numberOfNeurons, MatrixLayout matrixLayout, float forgetBias, RandomNumberGenerator <float> random) { if (shape == null) { throw new ArgumentNullException(nameof(shape)); } int[] axes = shape.Axes; // column-major matrix organization - each row contains all weights for one neuron // row-major matrix organization - each column contains all weights for one neuron int mbsize = axes.Skip(1).Aggregate(1, (total, next) => total * next); int[] weightsShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { mbsize, 4 * numberOfNeurons } : new[] { 4 * numberOfNeurons, mbsize }; int[] hiddenShape = matrixLayout == MatrixLayout.ColumnMajor ? new[] { numberOfNeurons, 4 * numberOfNeurons } : new[] { 4 * numberOfNeurons, numberOfNeurons }; // keep all weights in single channel // allocate four matrices (one for each of three gates and one for the state) int[] biasesShape = new[] { 4 * numberOfNeurons }; this.Initialize( direction, numberOfNeurons, matrixLayout, weightsShape, hiddenShape, biasesShape, random ?? new RandomRangeGenerator(-0.08f, 0.08f)); this.ForgetBias = forgetBias; this.OutputShape = new Shape(new int[] { shape.GetAxis(0), numberOfNeurons }); }
public static Tensor FullyConnected( this Session session, Tensor x, Tensor w, Tensor b, MatrixLayout matrixLayout) { // calculate output tensor in column-major mode // y += W * x (product of weight and input matrices) // input and output matrices are column major (one column per mini-batch item) // weights matrix might have to be transposed to have a row per neuron return(session.MxM( MatrixLayout.ColumnMajor, w, matrixLayout == MatrixLayout.RowMajor, x, false, b)); }