Exemplo n.º 1
0
 public Drawing(Shape?mainShape = default, ShapeOrNull?shapeOrNull = default, NullableShape?nullableShape = default, List <Shape>?shapes = default)
 {
     MainShape     = mainShape;
     ShapeOrNull   = shapeOrNull;
     NullableShape = nullableShape;
     Shapes        = shapes;
 }
Exemplo n.º 2
0
        public bool Equals(Shape?other)
        {
            if (other is null)
            {
                return(false);
            }

            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            if (Rank != other.Rank)
            {
                return(false);
            }

            for (int i = 0; i < Dims.Length; i++)
            {
                if (Dims[i] != other.Dims[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Drawing" /> class.
 /// </summary>
 /// <param name="mainShape">mainShape.</param>
 /// <param name="shapeOrNull">shapeOrNull.</param>
 /// <param name="nullableShape">nullableShape.</param>
 /// <param name="shapes">shapes.</param>
 public Drawing(Shape?mainShape = default, ShapeOrNull?shapeOrNull = default, NullableShape?nullableShape = default, List <Shape>?shapes = default) : base()
 {
     this.MainShape     = mainShape;
     this.ShapeOrNull   = shapeOrNull;
     this.NullableShape = nullableShape;
     this.Shapes        = shapes;
 }
Exemplo n.º 4
0
        /// <summary>Assigns all needed attributes to the tag</summary>
        /// <returns>This instance downcasted to base class</returns>
        public virtual IndexedTag attr(
            Shape?shape        = null,
            Length[] coords    = null,
            string href        = null,
            NoHref?nohref      = null,
            Target target      = null,
            string alt         = null,
            string id          = null,
            string @class      = null,
            string style       = null,
            string title       = null,
            LangCode lang      = null,
            string xmllang     = null,
            Dir?dir            = null,
            string onclick     = null,
            string ondblclick  = null,
            string onmousedown = null,
            string onmouseup   = null,
            string onmouseover = null,
            string onmousemove = null,
            string onmouseout  = null,
            string onkeypress  = null,
            string onkeydown   = null,
            string onkeyup     = null,
            char?accesskey     = null,
            int?tabindex       = null,
            string onfocus     = null,
            string onblur      = null
            )
        {
            Shape       = shape;
            Coords      = coords;
            Href        = href;
            NoHref      = nohref;
            Target      = target;
            Alt         = alt;
            Id          = id;
            Class       = @class;
            Style       = style;
            Title       = title;
            Lang        = lang;
            XmlLang     = xmllang;
            Dir         = dir;
            OnClick     = onclick;
            OnDblClick  = ondblclick;
            OnMouseDown = onmousedown;
            OnMouseUp   = onmouseup;
            OnMouseOver = onmouseover;
            OnMouseMove = onmousemove;
            OnMouseOut  = onmouseout;
            OnKeyPress  = onkeypress;
            OnKeyDown   = onkeydown;
            OnKeyUp     = onkeyup;
            AccessKey   = accesskey;
            TabIndex    = tabindex;
            OnFocus     = onfocus;
            OnBlur      = onblur;

            return(this);
        }
Exemplo n.º 5
0
        public static void AppendNameValue(this Shape?instance, StringBuilder sb, string variableName)
        {
            if (instance == null || sb == null || string.IsNullOrEmpty(variableName))
            {
                return;
            }

            AppendNameValueStd(sb, variableName, instance);
        }
Exemplo n.º 6
0
 public HitRecord(Point wp, Normal nm, Vec2D sp, float tt, Ray r, Shape? shape = null)
 {
     this.worldPoint = wp;
     this.normal = nm;
     this.surfacePoint = sp;
     this.t = tt;
     this.ray = r;
     this.shape = shape;
 }
        protected override void OnDraw(Shape?shape, Canvas?canvas, global::Android.Graphics.Paint?paint)
        {
            if (paint != null && _backgroundColor != null)
            {
                paint.Color = _backgroundColor.Value;
            }

            base.OnDraw(shape, canvas, paint);
        }
Exemplo n.º 8
0
 public TensorArray TensorArray(TF_DataType dtype, Tensor size, bool dynamic_size = false,
                                bool clear_after_read = true, Shape?element_shape = null, bool colocate_with_first_write_call = true,
                                bool infer_shape      = true)
 => tf.executing_eagerly() ?
 new _EagerTensorArray(dtype, size: size, dynamic_size: dynamic_size,
                       clear_after_read: clear_after_read, element_shape: element_shape, infer_shape: infer_shape,
                       colocate_with_first_write_call: colocate_with_first_write_call) :
 new _GraphTensorArray(dtype, size: size, dynamic_size: dynamic_size,
                       clear_after_read: clear_after_read, element_shape: element_shape, infer_shape: infer_shape,
                       colocate_with_first_write_call: colocate_with_first_write_call);
Exemplo n.º 9
0
        protected internal NDArray reshape_broadcast(int[] shape, Shape?original)
        {
            var ret      = Storage.Alias();
            var newShape = new Shape(shape);

            ret.ReshapeBroadcastedUnsafe(ref newShape, false, original);
            return(new NDArray(ret)
            {
                TensorEngine = TensorEngine
            });
        }
Exemplo n.º 10
0
        public static void StartSwitchCaseWhen()
        {
            Shape?sh = null;

            Shape[] shapes = { new Square(10),   new Rectangle(5,   7),
                               new Rectangle(10,             10), sh !, new Square(0) };
            foreach (var shape in shapes)
            {
                ShowShapeInfo(shape);
            }
        }
Exemplo n.º 11
0
 public _EagerTensorArray(TF_DataType dtype, Tensor size, bool dynamic_size = false,
                          bool clear_after_read = true, string tensor_array_name  = null, Tensor handle = null, Tensor flow = null,
                          bool infer_shape      = true, Shape?element_shape       = null,
                          bool colocate_with_first_write_call = true, string name = null)
 {
     _flow          = constant_op.constant(0);
     _infer_shape   = infer_shape;
     _element_shape = element_shape ?? Shape.Null;
     _colocate_with_first_write_call = colocate_with_first_write_call;
     _dtype            = dtype.as_base_dtype();
     _dynamic_size     = dynamic_size;
     _clear_after_read = clear_after_read;
     _tensor_array     = new List <Tensor>();
 }
Exemplo n.º 12
0
        public bool Equals(Shape?shape)
        {
            if (shape is null)
            {
                return(false);
            }

            if (ReferenceEquals(this, shape))
            {
                return(true);
            }

            if (Rank != shape.Rank)
            {
                return(false);
            }

            return(SimdOps <int> .Equal(Dims, shape.Dims));
        }
Exemplo n.º 13
0
        public NDIterator(IMemoryBlock block, Shape shape, Shape?broadcastedShape, bool autoReset = false)
        {
            if (shape.IsEmpty || shape.size == 0)
            {
                throw new InvalidOperationException("Can't construct NDIterator with an empty shape.");
            }

            Block            = block ?? throw new ArgumentNullException(nameof(block));
            Shape            = shape;
            BroadcastedShape = broadcastedShape;
            if (broadcastedShape.HasValue && shape.size != broadcastedShape.Value.size)
            {
                AutoReset = true;
            }
            else
            {
                AutoReset = autoReset;
            }

            // ReSharper disable once MergeConditionalExpression
            size = broadcastedShape.HasValue ? broadcastedShape.Value.size : shape.size;

            if (shape.IsScalar)
            {
                Type = IteratorType.Scalar;
            }
            else if (shape.NDim == 1)
            {
                Type = IteratorType.Vector;
            }
            else if (shape.NDim == 2)
            {
                Type = IteratorType.Matrix;
            }
            else
            {
                Type = IteratorType.Tensor;
            }

            SetDefaults();
        }
 public Search(
     EffectName name,
     int radius,
     IList <Flag> searchFlags,
     IList <Flag> filterFlags,
     IList <EffectName>?effects = null,
     Location?location          = null,
     Shape?shape                  = null,
     bool?ignoreCenter            = null,
     IList <Validator>?validators = null,
     bool?usedForValidator        = null) : base(name, $"{nameof(Effect)}.{nameof(Search)}", validators ?? new List <Validator>())
 {
     Radius           = radius;
     SearchFlags      = searchFlags;
     FilterFlags      = filterFlags;
     Effects          = effects ?? new List <EffectName>();
     Location         = location ?? Location.Inherited;
     Shape            = shape ?? Shape.Circle;
     IgnoreCenter     = ignoreCenter ?? false;
     UsedForValidator = usedForValidator ?? false;
 }
Exemplo n.º 15
0
 public bool Equals(Shape?input)
 {
     return(OpenAPIClientUtils.compareLogic.Compare(this, input).AreEqual);
 }
Exemplo n.º 16
0
        protected override void OnDraw(Shape?shape, Canvas?canvas, APaint?paint)
        {
            if (_disposed)
            {
                return;
            }

            if (HasBorder())
            {
                if (Paint != null)
                {
                    SetBackground(Paint);
                }

                if (_borderPaint != null)
                {
                    _borderPaint.StrokeWidth = _strokeThickness;
                    _borderPaint.StrokeJoin  = _strokeLineJoin;
                    _borderPaint.StrokeCap   = _strokeLineCap;
                    _borderPaint.StrokeMiter = _strokeMiterLimit * 2;

                    if (_borderPathEffect != null)
                    {
                        _borderPaint.SetPathEffect(_borderPathEffect);
                    }

                    if (_borderColor != null)
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
                    {
                        _borderPaint.Color = _borderColor.Value;
                    }
#pragma warning restore CA1416
                    else
                    {
                        if (_stroke != null)
                        {
                            SetPaint(_borderPaint, _stroke);
                        }
                    }
                }

                if (_invalidatePath)
                {
                    _invalidatePath = false;

                    if (_shape != null)
                    {
                        float offset = _strokeThickness / 2;
                        float w      = (float)(_width / _density) - _strokeThickness;
                        float h      = (float)(_height / _density) - _strokeThickness;

                        var bounds   = new Graphics.Rect(offset, offset, w, h);
                        var path     = _shape.PathForBounds(bounds);
                        var clipPath = path?.AsAndroidPath(scaleX: (float)_density, scaleY: (float)_density);

                        if (clipPath == null)
                        {
                            return;
                        }

                        if (_clipPath != null)
                        {
                            _clipPath.Reset();
                            _clipPath.Set(clipPath);
                        }
                    }
                }

                if (canvas == null)
                {
                    return;
                }

                var saveCount = canvas.SaveLayer(0, 0, _width, _height, null);

                if (_clipPath != null && Paint != null)
                {
                    canvas.DrawPath(_clipPath, Paint);
                }

                if (_clipPath != null && _borderPaint != null)
                {
                    canvas.DrawPath(_clipPath, _borderPaint);
                }

                canvas.RestoreToCount(saveCount);
            }
            else
            {
                if (paint != null)
                {
                    SetBackground(paint);
                }

                base.OnDraw(shape, canvas, paint);
            }
        }
Exemplo n.º 17
0
 public NDIterator(IArraySlice slice, Shape shape, Shape?broadcastedShape, bool autoReset = false) : this((IMemoryBlock)slice, shape, broadcastedShape, autoReset)
 {
 }
Exemplo n.º 18
0
 public NDArray(long[] value, Shape?shape = null) : base(value, shape)
     => NewEagerTensorHandle();
Exemplo n.º 19
0
        protected override void OnDraw(Shape?shape, Canvas?canvas, APaint?paint)
        {
            if (_disposed)
            {
                return;
            }

            if (Paint != null)
            {
                SetBackground(Paint);
            }

            if (HasBorder())
            {
                if (_borderPaint != null)
                {
                    _borderPaint.StrokeWidth = _strokeThickness;

                    if (_borderColor != null)
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
                    {
                        _borderPaint.Color = _borderColor.Value;
                    }
#pragma warning restore CA1416
                    else
                    {
                        if (_stroke != null)
                        {
                            SetPaint(_borderPaint, _stroke);
                        }
                    }
                }
            }

            if (_invalidatePath)
            {
                _invalidatePath = false;

                var path     = GetPath(_width, _height, _cornerRadius, _strokeThickness);
                var clipPath = path?.AsAndroidPath();

                if (clipPath == null)
                {
                    return;
                }

                if (_clipPath != null)
                {
                    _clipPath.Reset();
                    _clipPath.Set(clipPath);
                }
            }

            if (canvas == null)
            {
                return;
            }

            var saveCount = canvas.SaveLayer(0, 0, _width, _height, null);

            if (_clipPath != null && Paint != null)
            {
                canvas.DrawPath(_clipPath, Paint);
            }

            if (HasBorder())
            {
                if (_clipPath != null && _borderPaint != null)
                {
                    canvas.DrawPath(_clipPath, _borderPaint);
                }
            }

            canvas.RestoreToCount(saveCount);
        }
Exemplo n.º 20
0
 public override bool Equals(Shape?other) => Equals(other as Circle);
Exemplo n.º 21
0
        public _GraphTensorArray(TF_DataType dtype, Tensor size, bool?dynamic_size = null,
                                 bool?clear_after_read = null, string tensor_array_name  = null, Tensor handle = null, Tensor flow = null,
                                 bool infer_shape      = true, Shape?element_shape       = null,
                                 bool colocate_with_first_write_call = true, string name = null)
        {
            clear_after_read = clear_after_read ?? true;
            dynamic_size     = dynamic_size ?? false;
            _dynamic_size    = dynamic_size.Value;
            _dtype           = dtype;

            _colocate_with_first_write_call = colocate_with_first_write_call;
            if (colocate_with_first_write_call)
            {
                _colocate_with = new List <Tensor>();
            }

            // Record the current static shape for the array elements. The element
            // shape is defined either by `element_shape` or the shape of the tensor
            // of the first write. If `infer_shape` is true, all writes checks for
            // shape equality.
            if (element_shape == null)
            {
                _infer_shape   = infer_shape;
                _element_shape = new List <Shape> {
                };
            }
            else
            {
                _infer_shape   = true;
                _element_shape = new List <Shape> {
                    element_shape
                };
            }

            tf_with(ops.name_scope(name, "TensorArray", new { handle, size, flow }), scope =>
            {
                if (handle != null)
                {
                    _handle = handle;
                    _flow   = flow;
                }
                else
                {
                    Func <(Tensor, Tensor)> create = () => gen_data_flow_ops.tensor_array_v3(size,
                                                                                             dtype: dtype,
                                                                                             element_shape: element_shape,
                                                                                             identical_element_shapes: infer_shape,
                                                                                             dynamic_size: dynamic_size.Value,
                                                                                             clear_after_read: clear_after_read.Value,
                                                                                             tensor_array_name: tensor_array_name,
                                                                                             name: scope);

                    // Construct the TensorArray with an empty device.  The first
                    // write into the TensorArray from a Tensor with a set device
                    // will retroactively set the device value of this op.
                    if (colocate_with_first_write_call)
                    {
                        ops.colocate_with(ignore_existing: true);
                        (_handle, _flow) = create();
                    }
                    else
                    {
                        (_handle, _flow) = create();
                    }
                }
            });
        }
Exemplo n.º 22
0
        /// <summary>Assigns all needed attributes to the tag</summary>
        /// <returns>This instance downcasted to base class</returns>
        public virtual IndexedTag attr(
            Charset charset    = null,
            MimeType type      = null,
            string name        = null,
            string href        = null,
            LangCode hreflang  = null,
            Target target      = null,
            LinkType?rel       = null,
            LinkType?rev       = null,
            Shape?shape        = null,
            Length[] coords    = null,
            string id          = null,
            string @class      = null,
            string style       = null,
            string title       = null,
            LangCode lang      = null,
            string xmllang     = null,
            Dir?dir            = null,
            string onclick     = null,
            string ondblclick  = null,
            string onmousedown = null,
            string onmouseup   = null,
            string onmouseover = null,
            string onmousemove = null,
            string onmouseout  = null,
            string onkeypress  = null,
            string onkeydown   = null,
            string onkeyup     = null,
            char?accesskey     = null,
            int?tabindex       = null,
            string onfocus     = null,
            string onblur      = null
            )
        {
            Charset     = charset;
            Type        = type;
            Name        = name;
            Href        = href;
            HrefLang    = hreflang;
            Target      = target;
            Rel         = rel;
            Rev         = rev;
            Shape       = shape;
            Coords      = coords;
            Id          = id;
            Class       = @class;
            Style       = style;
            Title       = title;
            Lang        = lang;
            XmlLang     = xmllang;
            Dir         = dir;
            OnClick     = onclick;
            OnDblClick  = ondblclick;
            OnMouseDown = onmousedown;
            OnMouseUp   = onmouseup;
            OnMouseOver = onmouseover;
            OnMouseMove = onmousemove;
            OnMouseOut  = onmouseout;
            OnKeyPress  = onkeypress;
            OnKeyDown   = onkeydown;
            OnKeyUp     = onkeyup;
            AccessKey   = accesskey;
            TabIndex    = tabindex;
            OnFocus     = onfocus;
            OnBlur      = onblur;

            return(this);
        }
Exemplo n.º 23
0
        protected override void OnDraw(Shape?shape, Canvas?canvas, APaint?paint)
        {
            if (_disposed)
            {
                return;
            }

            if (HasBorder())
            {
                if (Paint != null)
                {
                    SetBackground(Paint);
                }

                if (_borderPaint != null)
                {
                    _borderPaint.StrokeWidth = _strokeThickness;

                    if (_borderColor != null)
                    {
                        _borderPaint.Color = _borderColor.Value;
                    }
                    else
                    {
                        if (_stroke != null)
                        {
                            SetPaint(_borderPaint, _stroke);
                        }
                    }
                }

                if (_invalidatePath)
                {
                    _invalidatePath = false;

                    var path     = GetPath(_width, _height, _cornerRadius, _strokeThickness);
                    var clipPath = path?.AsAndroidPath();

                    if (clipPath == null)
                    {
                        return;
                    }

                    if (_clipPath != null)
                    {
                        _clipPath.Reset();
                        _clipPath.Set(clipPath);
                    }
                }

                if (canvas == null)
                {
                    return;
                }

                var saveCount = canvas.SaveLayer(0, 0, _width, _height, null);

                if (_clipPath != null && Paint != null)
                {
                    canvas.DrawPath(_clipPath, Paint);
                }

                if (_clipPath != null && _borderPaint != null)
                {
                    canvas.DrawPath(_clipPath, _borderPaint);
                }

                canvas.RestoreToCount(saveCount);
            }
            else
            {
                if (paint != null)
                {
                    SetBackground(paint);
                }

                base.OnDraw(shape, canvas, paint);
            }
        }
Exemplo n.º 24
0
 public override bool Equals(Shape?other) => Equals(other as Rectangle);
Exemplo n.º 25
0
        public void ReshapeBroadcastedUnsafe(ref Shape newShape, bool guards = false, Shape?original = null)
        {
            //handle -1 in reshape
            InferMissingDimension(ref newShape);

            if (guards)
            {
                if (newShape.size == 0 && _shape.size != 0)
                {
                    throw new ArgumentException("Value cannot be an empty collection.", nameof(newShape));
                }

                if (_shape.size != newShape.size)
                {
                    throw new IncorrectShapeException($"Given shape size ({newShape.size}) does not match the size of the given storage size ({_shape.size})");
                }
            }

            if (_shape.IsSliced)
            {
                // Set up the new shape (of reshaped slice) to recursively represent a shape within a sliced shape
                newShape.ViewInfo = new ViewInfo()
                {
                    ParentShape = _shape, Slices = null
                }
            }
            ;

            if (original.HasValue)
            {
                newShape.BroadcastInfo = new BroadcastInfo(original.Value);
            }

            SetShapeUnsafe(ref newShape);
        }
Exemplo n.º 26
0
        protected override void OnDraw(Shape?shape, Canvas?canvas, APaint?paint)
        {
            if (_disposed)
            {
                return;
            }

            if (HasBorder())
            {
                if (Paint != null)
                {
                    SetBackground(Paint);
                }

                if (_borderPaint != null)
                {
                    _borderPaint.StrokeWidth = _strokeThickness;
                    _borderPaint.StrokeJoin  = _strokeLineJoin;
                    _borderPaint.StrokeCap   = _strokeLineCap;
                    _borderPaint.StrokeMiter = _strokeMiterLimit * 2;

                    if (_borderPathEffect != null)
                    {
                        _borderPaint.SetPathEffect(_borderPathEffect);
                    }

                    if (_borderColor != null)
                    {
                        _borderPaint.Color = _borderColor.Value;
                    }
                    else
                    {
                        if (_stroke != null)
                        {
                            SetPaint(_borderPaint, _stroke);
                        }
                    }
                }

                if (_invalidatePath)
                {
                    _invalidatePath = false;

                    if (_shape != null)
                    {
                        var bounds   = new Graphics.Rect(0, 0, _width, _height);
                        var path     = _shape.PathForBounds(bounds);
                        var clipPath = path?.AsAndroidPath();

                        if (clipPath == null)
                        {
                            return;
                        }

                        if (_clipPath != null)
                        {
                            _clipPath.Reset();
                            _clipPath.Set(clipPath);
                        }
                    }
                }

                if (canvas == null)
                {
                    return;
                }

                var saveCount = canvas.SaveLayer(0, 0, _width, _height, null);

                if (_clipPath != null && Paint != null)
                {
                    canvas.DrawPath(_clipPath, Paint);
                }

                if (_clipPath != null && _borderPaint != null)
                {
                    canvas.DrawPath(_clipPath, _borderPaint);
                }

                canvas.RestoreToCount(saveCount);
            }
            else
            {
                if (paint != null)
                {
                    SetBackground(paint);
                }

                base.OnDraw(shape, canvas, paint);
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Create a TensorProto, invoked in graph mode
        /// </summary>
        /// <param name="values"></param>
        /// <param name="dtype"></param>
        /// <param name="shape"></param>
        /// <param name="verify_shape"></param>
        /// <param name="allow_broadcast"></param>
        /// <returns></returns>
        public static TensorProto make_tensor_proto(object values, TF_DataType dtype = TF_DataType.DtInvalid, Shape?shape = null, bool verify_shape = false, bool allow_broadcast = false)
        {
            if (allow_broadcast && verify_shape)
            {
                throw new ValueError("allow_broadcast and verify_shape are not both allowed.");
            }
            if (values is TensorProto tp)
            {
                return(tp);
            }

            var origin_dtype = values.GetDataType();

            if (dtype == TF_DataType.DtInvalid)
            {
                dtype = origin_dtype;
            }
            else if (origin_dtype != dtype)
            {
                var new_system_dtype = dtype.as_system_dtype();
                if (values is long[] long_values)
                {
                    if (dtype == TF_DataType.TF_INT32)
                    {
                        values = long_values.Select(x => (int)Convert.ChangeType(x, new_system_dtype)).ToArray();
                    }
                }
                else
                {
                    values = Convert.ChangeType(values, new_system_dtype);
                }

                dtype = values.GetDataType();
            }

            shape = shape ?? values.GetShape();
            var tensor_proto = new TensorProto
            {
                Dtype       = dtype.as_datatype_enum(),
                TensorShape = shape.as_shape_proto()
            };

            if (values is NDArray nd)
            {
                // scalar
                if (nd.shape.IsScalar)
                {
                    switch (nd.dtype)
                    {
                    case TF_DataType.TF_BOOL:
                        tensor_proto.BoolVal.AddRange(nd.ToArray <bool>());
                        break;

                    case TF_DataType.TF_UINT8:
                        tensor_proto.IntVal.AddRange(nd.ToArray <byte>().Select(x => (int)x).ToArray());
                        break;

                    case TF_DataType.TF_INT32:
                        tensor_proto.IntVal.AddRange(nd.ToArray <int>());
                        break;

                    case TF_DataType.TF_INT64:
                        tensor_proto.Int64Val.AddRange(nd.ToArray <long>());
                        break;

                    case TF_DataType.TF_FLOAT:
                        tensor_proto.FloatVal.AddRange(nd.ToArray <float>());
                        break;

                    case TF_DataType.TF_DOUBLE:
                        tensor_proto.DoubleVal.AddRange(nd.ToArray <double>());
                        break;

                    default:
                        throw new Exception("make_tensor_proto Not Implemented");
                    }
                }
                else
                {
                    var    len   = nd.dtypesize * nd.size;
                    byte[] bytes = nd.ToByteArray();
                    tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
                }
            }
            else if (dtype == TF_DataType.TF_STRING && !(values is NDArray))
            {
                if (values is string str)
                {
                    tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFromUtf8(str));
                }
                else if (values is string[] str_values)
                {
                    tensor_proto.StringVal.AddRange(str_values.Select(x => Google.Protobuf.ByteString.CopyFromUtf8(x)));
                }
                else if (values is byte[] byte_values)
                {
                    tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(byte_values);
                }
            }
            else if (values is Array array)
            {
                // array
                var    len   = dtype.get_datatype_size() * (int)shape.size;
                byte[] bytes = new byte[len];
                System.Buffer.BlockCopy(array, 0, bytes, 0, len);
                tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
            }
            else
            {
                switch (values)
                {
                case Axis val:
                    tensor_proto.IntVal.AddRange(val.axis);
                    break;

                case Shape val:
                    tensor_proto.Int64Val.AddRange(val.dims);
                    break;

                case bool val:
                    tensor_proto.BoolVal.AddRange(new[] { val });
                    break;

                case sbyte val:
                    tensor_proto.IntVal.AddRange(new[] { (int)val });
                    break;

                case int val:
                    tensor_proto.IntVal.AddRange(new[] { val });
                    break;

                case long val:
                    tensor_proto.Int64Val.AddRange(new[] { val });
                    break;

                case float val:
                    tensor_proto.FloatVal.AddRange(new[] { val });
                    break;

                case double val:
                    tensor_proto.DoubleVal.AddRange(new[] { val });
                    break;

                default:
                    throw new Exception("make_tensor_proto Not Implemented");
                }
            }

            return(tensor_proto);
        }
Exemplo n.º 28
0
 public EagerTensor(object value, Shape?shape = null, string device_name = null, TF_DataType dtype = TF_DataType.TF_UINT8) : base((float[])value)
     => NewEagerTensorHandle(_handle);