|
| 1 | +using NumSharp.Core; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Tensorflow |
| 7 | +{ |
| 8 | + public class array_ops |
| 9 | + { |
| 10 | + public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = "") |
| 11 | + { |
| 12 | + Tensor output = null; |
| 13 | + |
| 14 | + dtype = dtype.as_base_dtype(); |
| 15 | + Python.with(new ops.name_scope(name, "zeros", shape), self => |
| 16 | + { |
| 17 | + name = self as ops.name_scope; |
| 18 | + switch (dtype) |
| 19 | + { |
| 20 | + case TF_DataType.TF_BOOL: |
| 21 | + output = _constant_if_small(false, shape, dtype, name); |
| 22 | + break; |
| 23 | + case TF_DataType.TF_DOUBLE: |
| 24 | + output = _constant_if_small(0.0D, shape, dtype, name); |
| 25 | + break; |
| 26 | + case TF_DataType.TF_FLOAT: |
| 27 | + output = _constant_if_small(0.0F, shape, dtype, name); |
| 28 | + break; |
| 29 | + case TF_DataType.TF_INT32: |
| 30 | + output = _constant_if_small(0, shape, dtype, name); |
| 31 | + break; |
| 32 | + default: |
| 33 | + break; |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return output; |
| 38 | + } |
| 39 | + |
| 40 | + private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dtype, string name) |
| 41 | + { |
| 42 | + Tensor tShape = null; |
| 43 | + var nd = np.zeros<T>(shape); |
| 44 | + if (shape.Size < 1000) |
| 45 | + { |
| 46 | + return constant_op.Constant(nd, name); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + tShape = constant_op._tensor_shape_tensor_conversion_function(shape.as_shape(), dtype, name); |
| 51 | + var c = constant_op.Constant(nd, name); |
| 52 | + return gen_array_ops.fill<T>(shape, value, name); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments