Skip to content

Commit a92f603

Browse files
committed
update document.
1 parent f41d6bc commit a92f603

7 files changed

Lines changed: 53 additions & 38 deletions

docs/source/Constant.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Chapter. Constant
1+
# Chapter 2. Constant
22

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:
44

55
* value: scalar value or constant list matching the data type defined in TensorFlow;
66
* dtype: data type;
@@ -9,9 +9,9 @@ In TensorFlow, a constant is a special Tensor that cannot be modified while the
99

1010

1111

12-
##### How to create a Constant
12+
### How to create a Constant
1313

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.
1515

1616
Initialize a scalar constant:
1717

@@ -24,19 +24,45 @@ var c4 = tf.constant("Big Tree"); // string
2424

2525
Initialize a constant through ndarray:
2626

27+
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+
2729
```csharp
2830
// dtype=int, shape=(2, 3)
29-
var nd = np.array(new int[][]
31+
var nd = np.array(new int[,]
3032
{
31-
new int[]{3, 1, 1},
32-
new int[]{2, 3, 1}
33+
{1, 2, 3},
34+
{4, 5, 6}
3335
});
3436
var tensor = tf.constant(nd);
3537
```
3638

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.
48+
49+
<img src="_static\contiguous-block-of-memory.png" />
50+
51+
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`.
54+
55+
<img src="_static\contiguous-block-of-memory-ndarray-example-1.png"/>
56+
57+
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.
3864

39-
Now let's explore how `constant` works.
65+
<img src="_static\tensor-constant-ndarray.png" />
4066

4167

4268

docs/source/HelloWorld.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Let's run a classic HelloWorld program first and see if TensorFlow is running on
1010

1111
### Install the TensorFlow.NET SDK
1212

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.
1414

1515

1616

@@ -38,9 +38,9 @@ PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
3838

3939
### Start coding Hello World
4040

41-
After installing the TensorFlow.NET package, you can use the `using Tensorflow` to introduce the TensorFlow library.
42-
41+
After installing the TensorFlow.NET package, you can use the `using static Tensorflow.Binding` to introduce the TensorFlow .NET library.
4342

43+
TensorFlow 2.x enabled `Eager Mode` by default. About what eager mode is, I will introduce it in detail in the following chapters.
4444

4545
```csharp
4646
using System;
@@ -51,33 +51,26 @@ namespace TensorFlowNET.Examples
5151
/// <summary>
5252
/// Simple hello world using TensorFlow
5353
/// </summary>
54-
public class HelloWorld : IExample
54+
class Program
5555
{
56-
public void Run()
56+
static void Main(string[] args)
5757
{
58-
/* Create a Constant op
59-
The op is added as a node to the default graph.
60-
61-
The value returned by the constructor represents the output
62-
of the Constant op. */
6358
var hello = tf.constant("Hello, TensorFlow!");
64-
65-
// Start tf session
66-
using (var sess = tf.Session())
67-
{
68-
// Run the op
69-
var result = sess.run(hello);
70-
Console.WriteLine(result);
71-
}
59+
Console.WriteLine(hello);
7260
}
7361
}
7462
}
7563
```
7664
After CTRL + F5 run, you will get the output.
7765
```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
79-
Hello, TensorFlow!
80-
Press any key to continue . . .
66+
9/20/2020 2:15:09 AM Starting Hello World
67+
tf.Tensor: shape=(), dtype=string, numpy=Hello, TensorFlow.NET!
68+
9/20/2020 2:15:09 AM Completed Hello World
69+
Example: Hello World in 0.1273463s is OK!
70+
TensorFlow.NET v0.20.1.0
71+
TensorFlow Binary v2.3.0
72+
1 of 21 example(s) are completed.
73+
Press [Enter] to continue...
8174
```
8275

8376
This sample code can be found at [here](https://github.com/SciSharp/SciSharp-Stack-Examples/blob/master/src/TensorFlowNET.Examples/HelloWorld.cs).

docs/source/Tensor.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Chapter. Tensor
1+
# Chapter 1. Tensor
22

33
### Represents one of the outputs of an Operation
44

@@ -8,11 +8,11 @@
88

99
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.
1010

11-
11+
<img src="_static\tensor-naming.png">
1212

1313
##### How to create a Tensor?
1414

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.
1616

1717
```csharp
1818
// Create a tensor holds a scalar value
@@ -32,13 +32,9 @@ Console.WriteLine($"t1: {t1}, t2: {t2}, t3: {t3}");
3232

3333
##### Data Structure of Tensor
3434

35-
36-
37-
38-
3935
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.
4036

41-
```cs
37+
```csharp
4238
// Generate a matrix:[[1, 2, 3], [4, 5, 6]]
4339
var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);
4440
// The index will be 0 2 4 1 3 5, it's column-major order.
64.9 KB
Loading
38.9 KB
Loading
128 KB
Loading
87.3 KB
Loading

0 commit comments

Comments
 (0)