Пример #1
0
        /// <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());
        }
Пример #2
0
 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);
     }
 }
Пример #3
0
        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]);
            }
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
 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;
 }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        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);
        }
Пример #10
0
        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);
        }
Пример #11
0
        /// <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));
        }
Пример #12
0
        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));
                }
            }
        }
Пример #13
0
        /// <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);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
            }
        }
Пример #16
0
 public ITensorVar Variable(ITensor seqOrFor) => _variables[seqOrFor];
Пример #17
0
 /// <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;
Пример #18
0
 public IReadOnlyList <ITensor> Slice(ITensor tensor, IReadOnlyList <Shape> shapes)
 {
     throw new NotImplementedException();
 }
Пример #19
0
 ITensorVar ILoop.Variable(ITensor seqOrFor) => this.Variable(seqOrFor);
Пример #20
0
 public static void BindToShape(this ITensor thiz, ITensor that)
 {
     thiz.BindToShape(that.Shape);
 }
Пример #21
0
 private static Tensor <T> _try <T>(ITensor tensor, Func <Tensor <T>, Tensor <T> > f) =>
 tensor is Tensor <T>?f((Tensor <T>) tensor) : null;
Пример #22
0
 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;
Пример #23
0
 public static void AssertOfShape <T>(this ITensor <T> thiz, ITensor that)
 {
     thiz.AssertOfShape(that.Shape);
 }