|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Tensorflow |
| 6 | +{ |
| 7 | + public class constant_op |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// Creates a constant tensor. |
| 11 | + /// |
| 12 | + /// The resulting tensor is populated with values of type `dtype`, as |
| 13 | + /// specified by arguments `value` and (optionally) `shape` |
| 14 | + /// </summary> |
| 15 | + /// <param name="value">A constant value (or list) of output type `dtype`.</param> |
| 16 | + /// <param name="dtype">The type of the elements of the resulting tensor.</param> |
| 17 | + /// <param name="shape">Optional dimensions of resulting tensor.</param> |
| 18 | + /// <param name="name">Optional name for the tensor.</param> |
| 19 | + /// <param name="verify_shape">Boolean that enables verification of a shape of values.</param> |
| 20 | + /// <returns></returns> |
| 21 | + public static Tensor Create(object value, TF_DataType dtype = TF_DataType.DtInvalid, TensorShape shape = null, string name = "Const", bool verify_shape = false) |
| 22 | + { |
| 23 | + Graph g = ops.get_default_graph(); |
| 24 | + var tensor_value = new AttrValue(); |
| 25 | + var tensor_pb = tensor_util.make_tensor_proto(value, dtype, shape, verify_shape); |
| 26 | + tensor_value.Tensor = tensor_pb; |
| 27 | + var dtype_value = new AttrValue |
| 28 | + { |
| 29 | + Type = tensor_value.Tensor.Dtype, |
| 30 | + }; |
| 31 | + |
| 32 | + var attrs = new Dictionary<string, AttrValue>(); |
| 33 | + attrs["dtype"] = dtype_value; |
| 34 | + attrs["value"] = tensor_value; |
| 35 | + var const_tensor = g.create_op("Const", null, new TF_DataType[] { (TF_DataType)dtype_value.Type }, attrs: attrs).outputs[0]; |
| 36 | + const_tensor.value = value; |
| 37 | + |
| 38 | + return const_tensor; |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments