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.

Numpy.Creation.cs 4.6 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.IO;
  2. using static Tensorflow.Binding;
  3. namespace Tensorflow.NumPy
  4. {
  5. public partial class np
  6. {
  7. [AutoNumPy]
  8. public static NDArray array(Array data, TF_DataType? dtype = null)
  9. {
  10. var nd = new NDArray(data);
  11. return dtype == null ? nd : nd.astype(dtype.Value);
  12. }
  13. [AutoNumPy]
  14. public static NDArray array<T>(params T[] data)
  15. where T : unmanaged => new NDArray(data);
  16. [AutoNumPy]
  17. public static NDArray arange<T>(T end)
  18. where T : unmanaged => new NDArray(tf.range(default(T), limit: end));
  19. [AutoNumPy]
  20. public static NDArray arange<T>(T start, T? end = null, T? step = null)
  21. where T : unmanaged => new NDArray(tf.range(start, limit: end, delta: step));
  22. [AutoNumPy]
  23. public static NDArray empty(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE)
  24. => new NDArray(tf.zeros(shape, dtype: dtype));
  25. [AutoNumPy]
  26. public static NDArray eye(int N, int? M = null, int k = 0, TF_DataType dtype = TF_DataType.TF_DOUBLE)
  27. => tf.numpy.eye(N, M: M, k: k, dtype: dtype);
  28. [AutoNumPy]
  29. public static NDArray full<T>(Shape shape, T fill_value)
  30. where T : unmanaged => new NDArray(tf.fill(tf.constant(shape), fill_value));
  31. [AutoNumPy]
  32. public static NDArray full_like<T>(NDArray x, T fill_value, TF_DataType? dtype = null, Shape shape = null)
  33. where T : unmanaged => new NDArray(array_ops.fill(x.shape, constant_op.constant(fill_value)));
  34. [AutoNumPy]
  35. public static NDArray frombuffer(byte[] bytes, Shape shape, TF_DataType dtype)
  36. => tf.numpy.frombuffer(bytes, shape, dtype);
  37. [AutoNumPy]
  38. public static NDArray frombuffer(byte[] bytes, string dtype)
  39. => tf.numpy.frombuffer(bytes, dtype);
  40. [AutoNumPy]
  41. public static NDArray linspace<T>(T start, T stop, int num = 50, bool endpoint = true, bool retstep = false,
  42. TF_DataType dtype = TF_DataType.TF_DOUBLE, int axis = 0)
  43. where T : unmanaged => tf.numpy.linspace(start, stop,
  44. num: num,
  45. endpoint: endpoint,
  46. retstep: retstep,
  47. dtype: dtype,
  48. axis: axis);
  49. [AutoNumPy]
  50. public static NDArray load(string file) => tf.numpy.load(file);
  51. [AutoNumPy]
  52. public static T Load<T>(string path)
  53. where T : class, ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
  54. {
  55. using (var stream = new FileStream(path, FileMode.Open))
  56. return Load<T>(stream);
  57. }
  58. [AutoNumPy]
  59. public static T Load<T>(Stream stream)
  60. where T : class, ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
  61. => tf.numpy.Load<T>(stream);
  62. [AutoNumPy]
  63. public static Array LoadMatrix(Stream stream) => tf.numpy.LoadMatrix(stream);
  64. [AutoNumPy]
  65. public static NpzDictionary<T> Load_Npz<T>(byte[] bytes)
  66. where T : class, IList, ICloneable, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
  67. => Load_Npz<T>(new MemoryStream(bytes));
  68. [AutoNumPy]
  69. public static NpzDictionary<T> Load_Npz<T>(Stream stream)
  70. where T : class, ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
  71. => new NpzDictionary<T>(stream);
  72. [AutoNumPy]
  73. public static (NDArray, NDArray) meshgrid<T>(T x, T y, bool copy = true, bool sparse = false)
  74. => tf.numpy.meshgrid(new[] { x, y }, copy: copy, sparse: sparse);
  75. [AutoNumPy]
  76. public static NDArray ndarray(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE)
  77. => new NDArray(tf.zeros(shape, dtype: dtype));
  78. [AutoNumPy]
  79. public static NDArray ones(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE)
  80. => new NDArray(tf.ones(shape, dtype: dtype));
  81. [AutoNumPy]
  82. public static NDArray ones_like(NDArray a, TF_DataType dtype = TF_DataType.DtInvalid)
  83. => new NDArray(tf.ones_like(a, dtype: dtype));
  84. [AutoNumPy]
  85. public static NDArray zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE)
  86. => new NDArray(tf.zeros(shape, dtype: dtype));
  87. [AutoNumPy]
  88. public static NDArray zeros_like(NDArray a, TF_DataType dtype = TF_DataType.DtInvalid)
  89. => new NDArray(tf.zeros_like(a, dtype: dtype));
  90. }
  91. }