Exemplo n.º 1
0
        public static Tensor prod <T1, T2>(T1 input, T2 axis, bool keep_dims = false, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                try
                {
                    using var status = new Status();
                    EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                         "Prod", name, new IntPtr[]
                    {
                        input as EagerTensor,
                        axis as EagerTensor
                    }, 2,
                                                                         op => wrap_tfe_src.SetOpAttrs(op, "keep_dims", keep_dims),
                                                                         status);
                    status.Check(true);
                    return(tensor);
                }
                catch (Exception)
                {
                    return(prod_eager_fallback(input as Tensor, axis as int[], keep_dims, name, tf.context));
                }
            }

            var _op = _op_def_lib._apply_op_helper("Prod", name, args: new { input, reduction_indices = axis, keep_dims });

            return(_op.output);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Outputs random values from a normal distribution.
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="dtype"></param>
        /// <param name="seed"></param>
        /// <param name="seed2"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Tensor random_standard_normal(Tensor shape, TF_DataType dtype = TF_DataType.DtInvalid, int?seed = null, int?seed2 = null, string name = null)
        {
            if (!seed.HasValue)
            {
                seed = 0;
            }
            if (!seed2.HasValue)
            {
                seed2 = 0;
            }

            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "RandomStandardNormal", name, new IntPtr[]
                {
                    shape as EagerTensor,
                }, 1,
                                                                     op => wrap_tfe_src.SetOpAttrs(op,
                                                                                                   "seed", seed,
                                                                                                   "seed2", seed2,
                                                                                                   "dtype", dtype),
                                                                     status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("RandomStandardNormal",
                                                   name: name,
                                                   args: new { shape, dtype, seed, seed2 });

            return(_op.output);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a handle to a Variable resource.
        /// </summary>
        /// <param name="dtype"></param>
        /// <param name="shape"></param>
        /// <param name="container"></param>
        /// <param name="shared_name"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Tensor var_handle_op(TF_DataType dtype, TensorShape shape,
                                           string container = "", string shared_name = "", string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "VarHandleOp", name, null, 0,
                                                                     op => wrap_tfe_src.SetOpAttrs(op,
                                                                                                   "container", container,
                                                                                                   "shared_name", shared_name,
                                                                                                   "dtype", dtype,
                                                                                                   "shape", shape.dims),
                                                                     status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("VarHandleOp", name, new {
                dtype,
                shape,
                container,
                shared_name
            });

            return(_op.output);
        }
Exemplo n.º 4
0
        public static Tensor var_is_initialized_op(Tensor resource, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "VarIsInitializedOp", name,
                                                                     new IntPtr[] { resource as EagerTensor },
                                                                     1, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("VarIsInitializedOp", name, new { resource });

            return(_op.output);
        }
Exemplo n.º 5
0
        public static Tensor strided_slice(Tensor input, Tensor begin, Tensor end, Tensor strides,
                                           int begin_mask       = 0,
                                           int end_mask         = 0,
                                           int ellipsis_mask    = 0,
                                           int new_axis_mask    = 0,
                                           int shrink_axis_mask = 0,
                                           string name          = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "StridedSlice", name, new IntPtr[]
                {
                    input as EagerTensor,
                    begin as EagerTensor,
                    end as EagerTensor,
                    strides as EagerTensor,
                }, 4,
                                                                     op => wrap_tfe_src.SetOpAttrs(op,
                                                                                                   "begin_mask", begin_mask,
                                                                                                   "end_mask", end_mask,
                                                                                                   "ellipsis_mask", ellipsis_mask,
                                                                                                   "new_axis_mask", new_axis_mask,
                                                                                                   "shrink_axis_mask", shrink_axis_mask),
                                                                     status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("StridedSlice", name, new
            {
                input,
                begin,
                end,
                strides,
                begin_mask,
                end_mask,
                ellipsis_mask,
                new_axis_mask,
                shrink_axis_mask
            });

            return(_op.outputs[0]);
        }
Exemplo n.º 6
0
        public static Tensor sqrt(Tensor x, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "Sqrt", name, new IntPtr[]
                {
                    x as EagerTensor,
                }, 1, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("Sqrt", name, args: new { x });

            return(_op.outputs[0]);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Add all input tensors element wise.
        /// </summary>
        /// <param name="inputs"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Tensor add_n(Tensor[] inputs, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                      "AddN", name,
                                                                      inputs.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), inputs.Length,
                                                                      null,
                                                                      status);
                status.Check(true);
                return(_result);
            }

            var _op = _op_def_lib._apply_op_helper("AddN", name, args: new { inputs });

            return(_op.outputs[0]);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Return a tensor with the same shape and contents as the input tensor or value.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="name"></param>
        public static Tensor identity(Tensor input, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "Identity", name, new IntPtr[]
                {
                    input as EagerTensor
                }, 1, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("Identity", name, new { input });

            return(_op.output);
        }
Exemplo n.º 9
0
        public static Tensor add(Tensor x, Tensor y, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                      "Add", name, new IntPtr[]
                {
                    x as EagerTensor,
                    y as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(_result);
            }

            var _op = _op_def_lib._apply_op_helper("Add", name, args: new { x, y });

            return(_op.output);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates a tensor filled with a scalar value.
        /// </summary>
        /// <param name="dims">A `Tensor`.</param>
        /// <param name="value">A `Tensor`. 0-D (scalar). Value to fill the returned tensor.</param>
        /// <param name="name">A name for the operation (optional).</param>
        /// <returns>A `Tensor`. Has the same type as `value`.</returns>
        public static Tensor fill <T>(Tensor dims, T value, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "Fill", name, new IntPtr[]
                {
                    dims as EagerTensor,
                    value as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("Fill", name, new { dims, value });

            return(_op.output);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Concatenates tensors along one dimension.
        /// </summary>
        /// <param name="values"></param>
        /// <param name="axis"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Tensor concat_v2 <T, Ta>(T[] values, Ta axis, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "ConcatV2", name, new IntPtr[]
                {
                    values as EagerTensor,
                    axis as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("ConcatV2", name: name, args: new { values, axis });

            return(_op.output);
        }
Exemplo n.º 12
0
        public static Tensor tile <T>(Tensor input, T multiples, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "Tile", name, new IntPtr[]
                {
                    input as EagerTensor,
                    multiples as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("Tile", name, new { input, multiples });

            return(_op.outputs[0]);
        }
Exemplo n.º 13
0
        public static Tensor reshape <T1, T2>(T1 tensor, T2 shape, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                      "Reshape", name, new IntPtr[]
                {
                    tensor as EagerTensor,
                    shape as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(_result);
            }

            var _op = _op_def_lib._apply_op_helper("Reshape", name, new { tensor, shape });

            return(_op.output);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Returns the truth value of (x != y) element-wise.
        /// </summary>
        /// <typeparam name="Tx">The type of the x.</typeparam>
        /// <typeparam name="Ty">The type of the y.</typeparam>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static Tensor not_equal <Tx, Ty>(Tx x, Ty y, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "NotEqual", name, new IntPtr[]
                {
                    x as EagerTensor,
                    y as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("NotEqual", name, args: new { x, y });

            return(_op.output);
        }
Exemplo n.º 15
0
        public static Tensor shape(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "Shape", name, new IntPtr[]
                {
                    input as EagerTensor,
                }, 1,
                                                                     op => wrap_tfe_src.SetOpAttrs(op, "out_type", out_type),
                                                                     status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("Shape", name, new { input, out_type });

            return(_op.outputs[0]);
        }
Exemplo n.º 16
0
        public static Tensor add_v2 <Tx, Ty>(Tx x, Ty y, string name = null)
        {
            // forward_compatible(2019, 6, 25):
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "AddV2", name, new IntPtr[]
                {
                    x as EagerTensor,
                    y as EagerTensor
                }, 2, null, status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("AddV2", name, args: new { x, y });

            return(_op.output);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Reads the value of a variable.
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="dtype"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string name = null)
        {
            if (tf.context.executing_eagerly())
            {
                using var status = new Status();
                EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
                                                                     "ReadVariableOp", name,
                                                                     new IntPtr[] { resource as EagerTensor }, 1,
                                                                     op => wrap_tfe_src.SetOpAttrs(op, "dtype", dtype),
                                                                     status);
                status.Check(true);
                return(tensor);
            }

            var _op = _op_def_lib._apply_op_helper("ReadVariableOp", name, new
            {
                resource,
                dtype
            });

            return(_op.output);
        }