You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/Constant.md
+35-9Lines changed: 35 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# Chapter. Constant
1
+
# Chapter 2. Constant
2
2
3
-
In TensorFlow, a constant is a special Tensor that cannot be modified while the graph is running. Like in a linear model $\tilde{y_i}=\boldsymbol{w}x_i+b$, constant $b$ can be represented as a Constant Tensor. Since the constant is a Tensor, it also has all the data characteristics of Tensor, including:
3
+
In TensorFlow, a constant is a special Tensor that cannot be modified while the graph is running. Like in a linear model `y = ax + b`, constant `b` can be represented as a `Constant` Tensor. Since the constant is a Tensor, it also has all the data characteristics of Tensor, including:
4
4
5
5
* value: scalar value or constant list matching the data type defined in TensorFlow;
6
6
* dtype: data type;
@@ -9,9 +9,9 @@ In TensorFlow, a constant is a special Tensor that cannot be modified while the
9
9
10
10
11
11
12
-
#####How to create a Constant
12
+
### How to create a Constant
13
13
14
-
TensorFlow provides a handy function to create a Constant. In TF.NET, you can use the same function name `tf.constant` to create it. TF.NET takes the same name as python binding to the API. Naming, although this will make developers who are used to C# naming habits feel uncomfortable, but after careful consideration, I decided to give up the C# convention naming method.
14
+
TensorFlow provides a handy function to create a Constant. In TF.NET, you can use the same function name `tf.constant` to create it. TF.NET takes the same name as python binding for the API. Naming, although this will make developers who are used to C# naming convention feel uncomfortable, but after careful consideration, I decided to give up the C# convention naming method. One of reason is for model developer, they don't have to learn a totally new different APIs.
TF.NET works very well with `NumSharp`'s `NDArray`. You can create a tensor from .NET primitive data type and NDArray as well. An `ndarray` is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its `shape`, which is a tuple of N non-negative integers that specify the sizes of each dimension.
28
+
27
29
```csharp
28
30
// dtype=int, shape=(2, 3)
29
-
varnd=np.array(newint[][]
31
+
varnd=np.array(newint[,]
30
32
{
31
-
newint[]{3, 1, 1},
32
-
newint[]{2, 3, 1}
33
+
{1, 2, 3},
34
+
{4, 5, 6}
33
35
});
34
36
vartensor=tf.constant(nd);
35
37
```
36
38
37
-
##### Dive in Constant
39
+
### Dive in Constant
40
+
41
+
Now let's explore how `constant` works in `eager` mode inside the black box.
42
+
43
+
Let's continue using the last examples, we're going to initialize a tensor in an ndarray of `[shape(2, 3), int32]`.
44
+
45
+
##### NDArray
46
+
47
+
The first thing we need to know is about `ndarray`'s memory model. The ndarray memory model is a very important data structure, and almost all underlying computation are inseparable from this datb a structure. One fundamental aspect of the ndarray is that an array is seen as a "chunk" of memory starting at some location. The interpretation of this memory depends on the stride information.
If we take a look at the real memory allocation in Visual Studio, below diagram helps us understand the data structure more intuitively. The strides keep track the size of every single dimension, help identify the actual offset in heap memory. The formula to calculate offset is: `offset = i * strides[0] + j * strides[1]`.
52
+
53
+
For example: if you want to seek the value in `[1, 1]`, you just need to calculate `1 * 3 + 1 * 1 = 4`, converted to pointer is `0x000002556B194260 + 4 = 0x000002556B194264` where has a value `05`.
Through the above diagram, we know how the data is stored in memory, and then we will look at how the data is transferred to `TensorFlow`.
58
+
59
+
##### Tensor
60
+
61
+
If you don't understand very well what `Tensor` is, you can go back to the chapter `Tensor` there is pretty much explanation if you skipped that chapter. Tensor is actually an NDArray that is with more than 2 dimensions.
62
+
63
+
TensorFlow will decide whether to copy the data or use the same pointer. Normally speaking, it's more safe whenever you copy data for the following process, especially in interoperating between .NET runtime and C++ runtime that they all have their own garbage collection (GC) mechanism, application will crash if someone access a block of destroyed memory.
Copy file name to clipboardExpand all lines: docs/source/HelloWorld.md
+14-21Lines changed: 14 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Let's run a classic HelloWorld program first and see if TensorFlow is running on
10
10
11
11
### Install the TensorFlow.NET SDK
12
12
13
-
TensorFlow.NET uses the .NET Standard 2.0 standard, so your new project Target Framework can be .NET Framework or .NET Core. All the examples in this book are using .NET Core 2.2 and Microsoft Visual Studio Community 2017. To start building TensorFlow program you just need to download and install the .NET SDK (Software Development Kit). You have to download the latest .NET Core SDK from offical website: https://dotnet.microsoft.com/download.
13
+
TensorFlow.NET uses the .NET Standard 2.0 standard, so your new project Target Framework can be .NET Framework or .NET Core/ .NET 5. All the examples in this book are using .NET Core 3.1 and Microsoft Visual Studio Community 2019. To start building TensorFlow program you just need to download and install the .NET SDK (Software Development Kit). You have to download the latest .NET Core SDK from offical website: https://dotnet.microsoft.com/download.
The value returned by the constructor represents the output
62
-
of the Constant op. */
63
58
varhello=tf.constant("Hello, TensorFlow!");
64
-
65
-
// Start tf session
66
-
using (varsess=tf.Session())
67
-
{
68
-
// Run the op
69
-
varresult=sess.run(hello);
70
-
Console.WriteLine(result);
71
-
}
59
+
Console.WriteLine(hello);
72
60
}
73
61
}
74
62
}
75
63
```
76
64
After CTRL + F5 run, you will get the output.
77
65
```cmd
78
-
2019-01-05 10:53:42.145931: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Copy file name to clipboardExpand all lines: docs/source/Tensor.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Chapter. Tensor
1
+
# Chapter 1. Tensor
2
2
3
3
### Represents one of the outputs of an Operation
4
4
@@ -8,11 +8,11 @@
8
8
9
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.
10
10
11
-
11
+
<imgsrc="_static\tensor-naming.png">
12
12
13
13
##### How to create a Tensor?
14
14
15
-
There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor.
15
+
There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor. But the best way to create a Tensor is using high level APIs like `tf.constant`, `tf.zeros` and `tf.ones`. We'll talk about constant more in next chapter.
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.
0 commit comments