# TensorFlow.NET machine learning introduction [1] Introduction to development environment and types

# TensorFlow.NET machine learning introduction [0] preface and contents

I have studied the knowledge of ML.NET for some time. ML.NET is a set of machine learning framework provided by Microsoft. Compared with some other machine learning frameworks, ML.NET focuses on consuming the existing network model, which is not easy to customize its own network model, and the underlying implementation is also highly encapsulated.

Recently, I want to learn the relevant knowledge of machine learning from the bottom. After preliminary screening, I plan to start in the direction of Python + python. After a period of learning, I find it difficult to practice because I am not familiar with Python language. Let alone the code related to machine learning, the surrounding code is in a mess. If you want to make up your mind to practice Python well, it will not happen overnight, so consider whether to learn a machine learning framework using C # so that you can concentrate on studying the object code and the supporting services can be easily done.

After searching, I found the project of scisharp stack. At the beginning, I located in torch Net, and finally chose tensorflow Net framework. Select tensorflow Net is mainly due to the following reasons:

- There are abundant examples. The first download, compilation and operation are very smooth. (if the HelloWorld project provided by the open source project can't run, you won't have the confidence to use it)
- The code is constantly updated, and the community is relatively active. If you ask questions on Issue, someone will answer them;
- The structure is quite reasonable, passed NET encapsulates the native dynamic link library provided by google, and we use mysql The architecture of data is similar, and C# developers are familiar with this architecture;
- The code style is close to the implementation of python+tensorflow, which is easy to refer to a large number of python routines on the network.

This series of articles is what I learned about tensorflow Net, the contents of the whole series of articles are as follows:

- TensorFlow.NET machine learning introduction [1] Introduction to development environment and types
- TensorFlow.NET machine learning introduction [2] linear regression
- TensorFlow.NET machine learning introduction [3] using neural network to realize nonlinear regression
- TensorFlow.NET introduction to machine learning [4] using neural network to deal with classification problems
- TensorFlow.NET introduction to machine learning [5] handwritten digit recognition using neural network (MNIST)
- TensorFlow.NET introduction to machine learning [6] using neural network to deal with fashion MNIST
- TensorFlow.NET introduction to machine learning [7] convolutional neural network (CNN) is used to process fashion MNIST
- TensorFlow.NET machine learning introduction [8] use GPU for learning
- TensorFlow.NET machine learning introduction [9] postscript

Since the author's own understanding of machine learning is still in the initial stage, mistakes are inevitable in the writing process. If you find any mistakes, please point them out in time.

[References]

Introduction to deep learning: Python based theory and Practice (Saito KANGYI)

The project development environment is visual studio 2019 + Net 5

After creating a new project, first import related packages through Nuget:

SciSharp.TensorFlow.Redist is a tensorflow development library provided by Google and a dynamic link library (DLL) developed in C language;

TensorFlow.NET uses C # language to encapsulate the C language library NET calling interface;

TensorFlow.Keras is an advanced tool class that encapsulates the modeling and training process and provides a simple interface.

The library is referenced by the following statement:

using Tensorflow;

using Tensorflow.NumPy;

using static Tensorflow.Binding;

using static Tensorflow.KerasApi;

Here are some tensorflow Net basic type operation:

/// <summary> ///Construction tensor /// </summary> private void Base_Constant() { //Construct tensors through basic types var c1 = tf.constant(3); // int var c2 = tf.constant(1.0f); // float var c3 = tf.constant(2.0); // double var c4 = tf.constant("Hello Tensorflow.Net!"); // string Console.WriteLine(c1); Console.WriteLine(c2); Console.WriteLine(c3); Console.WriteLine(c4); //Constructing tensors by multidimensional numerical methods int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; var nd = np.array(arr); var tensor = tf.constant(nd); Console.WriteLine(tensor); //Construct all 0 or all 1 tensors var tensor0 = tf.constant(np.zeros(new Shape(2, 3))); var tensor1 = tf.constant(np.ones(new Shape(2, 3))); Console.WriteLine(tensor0); Console.WriteLine(tensor1); var tensor_0 = tf.zeros(new Shape(2, 3)); var tensor_1 = tf.ones(new Shape(2, 3)); Console.WriteLine(tensor_0); Console.WriteLine(tensor_1); } /// <summary> ///Tensor operation /// </summary> private void Base_Operator() { var a = tf.constant(2.0f); var b = tf.constant(3.0f); var c = tf.constant(5.0f); // The basic operation can adopt operators such as + - * / var add = tf.add(a, b); var sub = tf.subtract(a, b); var mul = tf.multiply(a, b); var div = tf.divide(a, b); print($"{(float)a} + {(float)b} = {(float)add}"); print($"{(float)a} - {(float)b} = {(float)sub}"); print($"{(float)a} * {(float)b} = {(float)mul}"); print($"{(float)a} / {(float)b} = {(float)div}"); // Average and sum var mean = tf.reduce_mean(tf.constant(new[] { a, b, c })); var sum = tf.reduce_sum(tf.constant(new[] { a, b, c })); print("mean =", mean.numpy()); print("sum =", sum.numpy()); // matrix multiplication var matrix1 = tf.constant(new float[,] { { 1, 2, 3 }, { 3, 4, 5 } }); var matrix2 = tf.constant(new float[,] { { 3, 4 }, { 5, 6 }, { 7, 8 } }); var product1 = tf.matmul(matrix1, matrix2); print("product1 =", product1.numpy()); } /// <summary> ///Generating random number tensor /// </summary> private void Base_Random() { var t1 = tf.random.normal(new Shape(10)); var t2 = tf.random.uniform(new Shape(2, 5)); var t3 = tf.random.uniform(new Shape(2, 5), 1, 100); Console.WriteLine($"t1={t1.numpy()}"); Console.WriteLine($"t2={t2.numpy()}"); Console.WriteLine($"t3={t3.numpy()}"); t1 = tf.random.normal(new Shape(100), mean: 0.5f, stddev: 2); var mean = tf.reduce_mean(t1); var max = tf.reduce_max(t1); var min = tf.reduce_min(t1); Console.WriteLine($"mean={mean.numpy()},max={max.numpy()},min={min.numpy()}"); }

The above codes are basically simple and can be understood at a glance. There are several points that need to be explained:

1. Usually, when we generate random numbers, they are generally evenly distributed, but the data of machine learning tend to be normally distributed, so normal is used to generate random numbers, mean represents the center point and stddev represents the distribution range;

2. On the surface, tf framework seems to provide a set of Math library that can perform matrix operation, but it is not the case in practice. The core of tf framework is the gradient that can be calculated. We will talk about this later;

3. tf has two versions, V1 and V2. If you want to use V1 syntax, you need to add a sentence before the code: tf compat. v1. disable_ eager_ execution();

In contrast, V2 version is: TF enable_ eager_ execution(); Since the default version is V2, this line of code can be omitted.

All codes in this series are in V2 version. There are a lot of code provided in the official version of V1, but there may not be a lot of code provided in the official version of V1.

[References]

TensorFlow tutorial: TensorFlow quick start tutorial

[project source code]

Git: https://gitee.com/seabluescn/tf_not.git

Project Name: SayHello

catalog: TensorFlow.NET machine learning introduction series directory

If you think this blog is helpful or enlightening to you, please click [recommend] on the right to support it. Thank you!