|
1 | | -# Chapter 1. Tensor 第一章: Tensor |
| 1 | +# Chapter. Tensor |
2 | 2 |
|
3 | 3 | ### Represents one of the outputs of an Operation |
4 | 4 |
|
5 | | -### 表示一个操作的输出 |
6 | | - |
7 | 5 |
|
8 | 6 |
|
9 | 7 | ##### What is Tensor? |
10 | 8 |
|
11 | | -##### Tensor 是什么? |
12 | | - |
13 | | -Tensor holds a multi-dimensional array of elements of a single data type which is very similar with numpy's ndarray. |
| 9 | +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. |
14 | 10 |
|
15 | | -Tensor是一个具有单一数据类型的多维数组容器,非常类似于numpy里的ndarray。如果你对numpy非常熟悉的话,那么对Tensor的理解会相当容易。 |
| 11 | +Tensor是一个具有单一数据类型的多维数组容器,当维度为零时,可以称之为标量,当维度为2时,可以称之为矩阵,当维度大于2时,通常称之为张量。Tensor的数据结构非常类似于numpy里的ndarray。如果你对numpy非常熟悉的话,那么对Tensor的理解会相当容易。 |
16 | 12 |
|
17 | 13 |
|
18 | 14 |
|
19 | 15 | ##### How to create a Tensor? |
20 | 16 |
|
21 | | -##### 如何创建一个Tensor? |
| 17 | +There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor. |
| 18 | + |
| 19 | +在TF.NET中有很多种方式可以初始化一个Tensor对象。它可以从一个标量,字符串,矩阵或张量来初始化。 |
| 20 | + |
| 21 | +```csharp |
| 22 | +// Create a tensor holds a scalar value |
| 23 | +var t1 = new Tensor(3); |
| 24 | + |
| 25 | +// Init from a string |
| 26 | +var t2 = new Tensor("Hello! TensorFlow.NET"); |
| 27 | + |
| 28 | +// Tensor holds a ndarray |
| 29 | +var nd = new NDArray(new int[]{3, 1, 1, 2}); |
| 30 | +var t3 = new Tensor(nd); |
| 31 | + |
| 32 | +Console.WriteLine($"t1: {t1}, t2: {t2}, t3: {t3}"); |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +##### Data Structure of Tensor |
22 | 38 |
|
23 | 39 |
|
24 | 40 |
|
25 | 41 |
|
26 | 42 |
|
27 | | -TF uses column major order. |
| 43 | +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. |
28 | 44 |
|
29 | 45 | TF 采用的是按列存储模式,如果我们用NumSharp产生一个2 X 3的矩阵,如果按顺序从0到5访问数据的话,是不会得到1-6的数字的,而是得到1,4, 2, 5, 3, 6这个顺序的一组数字。 |
30 | 46 |
|
31 | 47 | ```cs |
32 | | -// generate a matrix:[[1, 2, 3], [4, 5, 6]] |
| 48 | +// Generate a matrix:[[1, 2, 3], [4, 5, 6]] |
33 | 49 | var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3); |
34 | | -// the index will be 0 2 4 1 3 5, it's column-major order. |
| 50 | +// The index will be 0 2 4 1 3 5, it's column-major order. |
35 | 51 | ``` |
36 | 52 |
|
37 | 53 |
|
|
0 commit comments