/// <summary> /// Deal with dynamic shape in tensorflow cleanly. /// </summary> static dynamic[] ShapeList(ITensor tensor) { IEnumerable <int?> @static = tensor.shape.as_list(); dynamic dynamic = tf.shape(tensor); return(@static.Select((size, index) => size ?? (object)dynamic[index]).ToArray()); }
public static void AssertOfDim(this ITensor thiz, int n) { if (thiz.NDim != n) { throw RankException("{0} should be of dim: {1} but is of dim: {2}", thiz.ToString(), n, thiz.NDim); } }
public static void BindToShape(this ITensor thiz, Scalar <int>[] shape) { var a = thiz.Shape; thiz.AssertOfDim(shape.Length); for (int d = 0; d < thiz.NDim; ++d) { ShapeExtension.Bind(ref a[d], ref shape[d]); } }
/// <summary> /// Deal with dynamic shape in tensorflow cleanly. /// </summary> static PythonList <dynamic> ShapeList(ITensor tensor) { IEnumerable <int?> @static = tensor.shape.as_list(); dynamic dynamic = tf.shape(tensor); var result = new PythonList <dynamic>(); foreach (object size in @static.Select((size, index) => size ?? (object)dynamic[index])) { result.Add(size); } return(result); }
internal AxisLength(ITensor x, int axis) : base("Shape", x, (Scalar <int>)axis) { if (axis < -x.NDim || axis >= x.NDim) { throw new IndexOutOfRangeException($"Axis {axis} is out of bound for {x}.Shape of length {x.NDim}"); } if (axis < 0) { axis += x.NDim; } this.Axis = axis; }
public void SumExpression() { var compiler = new TileCompiler(); var(linear, a, x, b, res) = LinearTile(); var kernel = compiler.Compile(linear); var tensors = new ITensor[] { a, x, b, res }; kernel.Eval(tensors); CheckRes(res); }
public void ElementWiseExpression() { var compiler = new TileCompiler(); var(transpose, input, res) = TransposeTile(); var kernel = compiler.Compile(transpose); var tensors = new ITensor[] { input, res }; kernel.Eval(tensors); CheckRes(res); }
public void LookupExpression() { var compiler = new TileCompiler(); var(tile, input, lookup, res) = LookupTile(); var kernel = compiler.Compile(tile); var tensors = new ITensor[] { input, lookup, res }; kernel.Eval(tensors); CheckRes(res); }
public void MaxExpression() { var compiler = new TileCompiler(); var(simpleMax, input, res) = SimpleMaxTile(); var kernel = compiler.Compile(simpleMax); var tensors = new ITensor[] { input, res }; kernel.Eval(tensors); CheckRes(res); }
public IFor AddOutput_(ITensor expr) { SequencesLocked = true; var f = expr.Match( (Tensor <float> exprF) => (IFor) new Tensor <float> .For(this, Fors.Count, exprF, null, null), (Tensor <int> exprI) => new Tensor <int> .For(this, Fors.Count, exprI, null, null), null ); Fors.Add(f); return(f); }
/// <summary> Add a recursive For to the loop. Returns the For created. </summary> public IFor AddRecursive_(ITensor expr, ITensor seed, string name) { if (seed == null) { throw new ArgumentException($"{nameof(seed)} can't be null, use AddOutput instead", nameof(seed)); } ITensorVar variable = seed.Match( (Tensor <float> exprF) => new Tensor <float> .Var(seed.Shape, name), (Tensor <int> exprI) => new Tensor <int> .Var(seed.Shape, name), () => { throw new NotImplementedException(); return((ITensorVar)null); } ); return(AddRecursive_(expr, seed, variable)); }
public static void AssertOfShape <T>(this ITensor <T> thiz, params Scalar <int>[] shape) { var a = thiz.Shape; if (thiz.NDim != shape.Length) { throw RankException("{0} of shape {1}, won't match with: {2}", thiz, thiz.Shape.Format(thiz), shape.Format(thiz)); } for (int d = 0; d < thiz.NDim; ++d) { if (!ShapeExtension.CanEqualTo(a[d], shape[d])) { throw RankException("{0} of shape {1}, won't match with: {2}", thiz, thiz.Shape.Format(thiz), shape.Format(thiz)); } } }
/// <summary> Add a recursive For to the loop. Returns the For created. </summary> public IFor AddRecursive_(ITensor expr, ITensor seed, ITensorVar variable) { if (seed == null) { throw new ArgumentException($"{nameof(seed)} can't be null, use AddOutput instead", nameof(seed)); } SequencesLocked = true; var f = expr.Match( (Tensor <float> exprF) => (IFor) new Tensor <float> .For(this, Fors.Count, exprF, (Tensor <float>)seed, (Tensor <float> .Var)variable), (Tensor <int> exprI) => new Tensor <int> .For(this, Fors.Count, exprI, (Tensor <int>)seed, (Tensor <int> .Var)variable), null ); Fors.Add(f); _variables.Add(f, variable); return(f); }
public void ZeroAndSum() { var compiler = new TileCompiler(); var(linear, a, x, b, res) = LinearTile(); var kernel = compiler.Compile(linear); var tensors = new ITensor[] { a, x, b, res }; var sr = res.Buffer.Span; for (var i = 0; i < sr.Length; i++) { sr[i] = i; // non-zero initialization } kernel.Eval(tensors); // correct only if 'res' is re-initialized before the sum CheckRes(res); }
private static ITensorVar _createVar(ITensor outputInfo, System.Reflection.ParameterInfo infos) { if (outputInfo == null) { return(null); } var res = null ?? _tryCreateVar <float>(outputInfo, infos) ?? _tryCreateVar <int>(outputInfo, infos) ?? _tryCreateVar <double>(outputInfo, infos) ; if (res == null) { throw new NotImplementedException(); } else { return(res); } }
public ITensorVar Variable(ITensor seqOrFor) => _variables[seqOrFor];
/// <summary> /// Returns the buffer allocated to the given tensor. Returns null if no buffer has been allocated. /// </summary> public BufferInfo GetBuffer(ITensor tensor) => mem != null && mem.Buffer.ContainsKey(tensor) ? mem.Buffer[tensor] : null;
public IReadOnlyList <ITensor> Slice(ITensor tensor, IReadOnlyList <Shape> shapes) { throw new NotImplementedException(); }
ITensorVar ILoop.Variable(ITensor seqOrFor) => this.Variable(seqOrFor);
public static void BindToShape(this ITensor thiz, ITensor that) { thiz.BindToShape(that.Shape); }
private static Tensor <T> _try <T>(ITensor tensor, Func <Tensor <T>, Tensor <T> > f) => tensor is Tensor <T>?f((Tensor <T>) tensor) : null;
private static ITensorVar _tryCreateVar <T>(ITensor outputInfo, System.Reflection.ParameterInfo infos) => infos.ParameterType == typeof(Tensor <T>) ? new Tensor <T> .Var(outputInfo.Shape, infos.Name) : null;
public static void AssertOfShape <T>(this ITensor <T> thiz, ITensor that) { thiz.AssertOfShape(that.Shape); }