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 2.2 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. Tensor是一个具有单一数据类型的多维数组容器,当维度为零时,可以称之为标量,当维度为2时,可以称之为矩阵,当维度大于2时,通常称之为张量。Tensor的数据结构非常类似于numpy里的ndarray。如果你对numpy非常熟悉的话,那么对Tensor的理解会相当容易。
  6. ##### How to create a Tensor?
  7. There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor.
  8. 在TF.NET中有很多种方式可以初始化一个Tensor对象。它可以从一个标量,字符串,矩阵或张量来初始化。
  9. ```csharp
  10. // Create a tensor holds a scalar value
  11. var t1 = new Tensor(3);
  12. // Init from a string
  13. var t2 = new Tensor("Hello! TensorFlow.NET");
  14. // Tensor holds a ndarray
  15. var nd = new NDArray(new int[]{3, 1, 1, 2});
  16. var t3 = new Tensor(nd);
  17. Console.WriteLine($"t1: {t1}, t2: {t2}, t3: {t3}");
  18. ```
  19. ##### Data Structure of Tensor
  20. 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.
  21. TF 采用的是按列存储模式,如果我们用NumSharp产生一个2 X 3的矩阵,如果按顺序从0到5访问数据的话,是不会得到1-6的数字的,而是得到1,4, 2, 5, 3, 6这个顺序的一组数字。
  22. ```cs
  23. // Generate a matrix:[[1, 2, 3], [4, 5, 6]]
  24. var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);
  25. // The index will be 0 2 4 1 3 5, it's column-major order.
  26. ```
  27. ![column-major order](_static/column-major-order.png)
  28. ![row-major order](_static/row-major-order.png)

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。