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.

Tensor.md 1.5 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Chapter. Tensor
  2. ### Represents one of the outputs of an Operation
  3. ##### What is Tensor?
  4. Tensor holds a multi-dimensional array of elements of a single data type which is very similar with numpy's ndarray. When the dimension is zero, it can be called a scalar. When the dimension is 2, it can be called a matrix. When the dimension is greater than 2, it is usually called a tensor. If you are very familiar with numpy, then understanding Tensor will be quite easy.
  5. ##### How to create a Tensor?
  6. There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor.
  7. ```csharp
  8. // Create a tensor holds a scalar value
  9. var t1 = new Tensor(3);
  10. // Init from a string
  11. var t2 = new Tensor("Hello! TensorFlow.NET");
  12. // Tensor holds a ndarray
  13. var nd = new NDArray(new int[]{3, 1, 1, 2});
  14. var t3 = new Tensor(nd);
  15. Console.WriteLine($"t1: {t1}, t2: {t2}, t3: {t3}");
  16. ```
  17. ##### Data Structure of Tensor
  18. TF uses column major order. If we use NumSharp to generate a 2 x 3 matrix, if we access the data from 0 to 5 in order, we won't get a number of 1-6, but we get the order of 1, 4, 2, 5, 3, 6. a set of numbers.
  19. ```cs
  20. // Generate a matrix:[[1, 2, 3], [4, 5, 6]]
  21. var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);
  22. // The index will be 0 2 4 1 3 5, it's column-major order.
  23. ```
  24. ![column-major order](_static/column-major-order.png)
  25. ![row-major order](_static/row-major-order.png)