You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NDArray.cs 4.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using static Tensorflow.Binding;
  6. namespace Tensorflow.NumPy
  7. {
  8. public partial class NDArray
  9. {
  10. Tensor _tensor;
  11. public NumpyDType dtype => _tensor.dtype.as_numpy_typecode();
  12. public ulong size => _tensor.size;
  13. public ulong dtypesize => _tensor.itemsize;
  14. public int ndim => _tensor.NDims;
  15. public long[] dims => _tensor.dims.Select(x => Convert.ToInt64(x)).ToArray();
  16. public Shape shape => _tensor.shape;
  17. public NDArray(bool value)
  18. {
  19. _tensor = ops.convert_to_tensor(value);
  20. }
  21. public NDArray(byte value)
  22. {
  23. _tensor = ops.convert_to_tensor(value);
  24. }
  25. public NDArray(int value)
  26. {
  27. _tensor = ops.convert_to_tensor(value);
  28. }
  29. public NDArray(float value)
  30. {
  31. _tensor = ops.convert_to_tensor(value);
  32. }
  33. public NDArray(double value)
  34. {
  35. _tensor = ops.convert_to_tensor(value);
  36. }
  37. public NDArray(Array value, Shape shape = null)
  38. {
  39. _tensor = ops.convert_to_tensor(value);
  40. }
  41. public NDArray(Type dtype, Shape shape)
  42. {
  43. }
  44. public NDArray(Shape shape, NumpyDType dtype = NumpyDType.Float)
  45. {
  46. Initialize(shape, dtype: dtype);
  47. }
  48. public NDArray(Tensor value, Shape? shape = null)
  49. {
  50. if (shape is not null)
  51. _tensor = tf.reshape(value, shape);
  52. else
  53. _tensor = value;
  54. }
  55. public static NDArray Scalar<T>(T value) where T : unmanaged
  56. {
  57. return value switch
  58. {
  59. bool b => new NDArray(b),
  60. _ => throw new NotImplementedException("")
  61. };
  62. }
  63. public T GetValue<T>(int index) where T : unmanaged
  64. => _tensor.ToArray<T>()[index];
  65. public T GetAtIndex<T>(int index) where T : unmanaged
  66. => _tensor.ToArray<T>()[index];
  67. public T[] GetData<T>() where T : unmanaged
  68. => _tensor.ToArray<T>();
  69. public NDArray[] GetNDArrays()
  70. => throw new NotImplementedException("");
  71. public ValueType GetValue(params int[] indices)
  72. => throw new NotImplementedException("");
  73. public void SetData(object value, params int[] indices)
  74. => throw new NotImplementedException("");
  75. public NDIterator<T> AsIterator<T>(bool autoreset = false) where T : unmanaged
  76. => throw new NotImplementedException("");
  77. public bool HasNext() => throw new NotImplementedException("");
  78. public T MoveNext<T>() => throw new NotImplementedException("");
  79. public NDArray reshape(Shape newshape) => new NDArray(_tensor, newshape);
  80. public NDArray astype(Type type) => throw new NotImplementedException("");
  81. public NDArray astype(NumpyDType type) => throw new NotImplementedException("");
  82. public bool array_equal(NDArray rhs) => throw new NotImplementedException("");
  83. public NDArray ravel() => throw new NotImplementedException("");
  84. public void shuffle(NDArray nd) => throw new NotImplementedException("");
  85. public Array ToMuliDimArray<T>() => throw new NotImplementedException("");
  86. public byte[] ToByteArray() => _tensor.BufferToArray();
  87. public static string[] AsStringArray(NDArray arr) => throw new NotImplementedException("");
  88. public T[] Data<T>() where T : unmanaged
  89. => _tensor.ToArray<T>();
  90. public T[] ToArray<T>() where T : unmanaged
  91. => _tensor.ToArray<T>();
  92. public static NDArray operator /(NDArray x, NDArray y) => throw new NotImplementedException("");
  93. public override string ToString()
  94. {
  95. return tensor_util.to_numpy_string(_tensor);
  96. }
  97. }
  98. }