1. 程式人生 > 其它 >TensorFlow.NET機器學習 TensorFlow.NET機器學習入門【1】開發環境與型別簡介TensorFlow.NET機器學習入門【0】前言與目錄

TensorFlow.NET機器學習 TensorFlow.NET機器學習入門【1】開發環境與型別簡介TensorFlow.NET機器學習入門【0】前言與目錄

TensorFlow.NET機器學習入門【1】開發環境與型別簡介

 

TensorFlow.NET機器學習入門【0】前言與目錄

 

     曾經學習過一段時間ML.NET的知識,ML.NET是微軟提供的一套機器學習框架,相對於其他的一些機器學習框架,ML.NET側重於消費現有的網路模型,不太好自定義自己的網路模型,底層實現也做了高度封裝。

    最近想從底層學習一下機器學習的相關知識,經過初步篩選,計劃定位於python + pytorch這個方向入手,經過一段時間的學習,我發現由於對python語言不太熟悉,導致實踐起來比較困難,先不說機器學習相關的程式碼,光周邊程式碼就搞得焦頭爛額了。想要下決心好好修煉一下python必然不是一朝一夕的事情,所以考慮是否學一個採用C#的機器學習框架,這樣就可以專心研究目的碼,配套服務就可以輕鬆搞定了。

    經搜尋發現了SciSharp-Stack這個專案,一開始我定位於Torch.Net,最終選擇了TensorFlow.NET這個框架。選擇TensorFlow.NET主要是因為如下幾個原因:

  •   示例比較豐富,第一次下載、編譯、執行都很順利。(如果開源專案提供的HelloWorld專案跑不起來的話,就沒有信心使用了)
  •   程式碼一直在更新,社群比較活躍,Issue上問問題會有人回答;
  •   架構比較合理,通過.NET對google提供的原生動態連結庫進行封裝,和我們使用MySQL.Data的架構類似,C#開發人員很熟悉這種架構;
  •   程式碼風格接近python+tensorflow的實現方式,便於參考網路上大量python的例程。

這個系列的文章就是我這段時間學習TensorFlow.Net的心得體會,整個系列的文章目錄如下: 

  1. TensorFlow.NET機器學習入門【1】開發環境與型別簡介
  2. TensorFlow.NET機器學習入門【2】線性迴歸
  3. TensorFlow.NET機器學習入門【3】採用神經網路實現非線性迴歸
  4. TensorFlow.NET機器學習入門【4】採用神經網路處理分類問題
  5. TensorFlow.NET機器學習入門【5】採用神經網路實現手寫數字識別(MNIST)
  6. TensorFlow.NET機器學習入門【6】採用神經網路處理Fashion-MNIST
  7. TensorFlow.NET機器學習入門【7】採用卷積神經網路(CNN)處理Fashion-MNIST
  8. TensorFlow.NET機器學習入門【8】採用GPU進行學習
  9. TensorFlow.NET機器學習入門【9】後記

由於作者本人對於機器學習的認識還處於入門階段,寫作過程中難免存在紕漏,如果您發現有什麼錯誤的地方,請及時指出來。

 

【參考資料】

TensorFlow.NET

SciSharp-Stack-Examples

《深度學習入門:基於Python的理論與實踐(齋藤康毅)》

     曾經學習過一段時間ML.NET的知識,ML.NET是微軟提供的一套機器學習框架,相對於其他的一些機器學習框架,ML.NET側重於消費現有的網路模型,不太好自定義自己的網路模型,底層實現也做了高度封裝。

    最近想從底層學習一下機器學習的相關知識,經過初步篩選,計劃定位於python + pytorch這個方向入手,經過一段時間的學習,我發現由於對python語言不太熟悉,導致實踐起來比較困難,先不說機器學習相關的程式碼,光周邊程式碼就搞得焦頭爛額了。想要下決心好好修煉一下python必然不是一朝一夕的事情,所以考慮是否學一個採用C#的機器學習框架,這樣就可以專心研究目的碼,配套服務就可以輕鬆搞定了。

    經搜尋發現了SciSharp-Stack這個專案,一開始我定位於Torch.Net,最終選擇了TensorFlow.NET這個框架。選擇TensorFlow.NET主要是因為如下幾個原因:

  •   示例比較豐富,第一次下載、編譯、執行都很順利。(如果開源專案提供的HelloWorld專案跑不起來的話,就沒有信心使用了)
  •   程式碼一直在更新,社群比較活躍,Issue上問問題會有人回答;
  •   架構比較合理,通過.NET對google提供的原生動態連結庫進行封裝,和我們使用MySQL.Data的架構類似,C#開發人員很熟悉這種架構;
  •   程式碼風格接近python+tensorflow的實現方式,便於參考網路上大量python的例程。

這個系列的文章就是我這段時間學習TensorFlow.Net的心得體會,整個系列的文章目錄如下: 

  1. TensorFlow.NET機器學習入門【1】開發環境與型別簡介
  2. TensorFlow.NET機器學習入門【2】線性迴歸
  3. TensorFlow.NET機器學習入門【3】採用神經網路實現非線性迴歸
  4. TensorFlow.NET機器學習入門【4】採用神經網路處理分類問題
  5. TensorFlow.NET機器學習入門【5】採用神經網路實現手寫數字識別(MNIST)
  6. TensorFlow.NET機器學習入門【6】採用神經網路處理Fashion-MNIST
  7. TensorFlow.NET機器學習入門【7】採用卷積神經網路(CNN)處理Fashion-MNIST
  8. TensorFlow.NET機器學習入門【8】採用GPU進行學習
  9. TensorFlow.NET機器學習入門【9】後記

由於作者本人對於機器學習的認識還處於入門階段,寫作過程中難免存在紕漏,如果您發現有什麼錯誤的地方,請及時指出來。

 

【參考資料】

TensorFlow.NET

SciSharp-Stack-Examples

《深度學習入門:基於Python的理論與實踐(齋藤康毅)》

專案開發環境為Visual Studio 2019 + .Net 5

建立新專案後首先通過Nuget引入相關包:

 

 SciSharp.TensorFlow.Redist是Google提供的TensorFlow開發庫,是採用C語言開發的動態連結庫(DLL);

TensorFlow.NET採用C#語言對C語言的庫進行封裝,提供.NET呼叫介面;

TensorFlow.Keras是一個高階工具類,對建模和訓練過程進行封裝,提供簡便介面。

 通過下列語句對庫進行引用:

using Tensorflow;

using Tensorflow.NumPy;

using static Tensorflow.Binding;

using static Tensorflow.KerasApi;

下面展示一些TensorFlow.NET的基本型別操作:

       /// <summary>
        /// 構建張量
        /// </summary>
        private void Base_Constant()
        {
            //通過基本型別構建張量
            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);

            //通過多維數值構建張量
            int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            var nd = np.array(arr);
            var tensor = tf.constant(nd);
            Console.WriteLine(tensor);

            //構建全0或全1張量
            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>
        /// 張量運算
        /// </summary>
        private void Base_Operator()
        {
            var a = tf.constant(2.0f);
            var b = tf.constant(3.0f);
            var c = tf.constant(5.0f);

            // 基本運算,可以採+ - * / 等運算子
            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}");

            // 求平均、求和
            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());

            // 矩陣相乘
            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>
        /// 生成隨機數張量
        /// </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()}");
        }

上述程式碼基本都比較簡單,基本一看就能懂,有幾處需要解釋一下:

1、平常我們在生成隨機數時,一般都是平均分佈,但機器學習的資料更多趨向正態分佈,所以採用normal生成隨機數,mean表示中心點,stddev表示分佈範圍;

2、從表面看tf的框架似乎提供了一套可以進行矩陣運算的Math庫,但實際並非如此,tf框架的核心是可以計算運算的梯度,這個問題我們後面再講;

3、tf有兩個版本,V1版和V2版本,如果要使用V1版本語法,需要在程式碼之前加一句:tf.compat.v1.disable_eager_execution();

     相對的,V2版本為:tf.enable_eager_execution();由於預設為V2版本,所以這行程式碼可以省略不寫。

    本系列的所有程式碼均採用V2版本。官方提供的樣例裡有大量V1版本程式碼,有一些V2版沒有提供的功能,可能不得不採用V1版程式碼實現。

 

【參考資料】

TensorFlow教程:TensorFlow快速入門教程

 

【專案原始碼】

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

專案名稱:SayHello

目錄:TensorFlow.NET機器學習入門系列目錄

 


簽名區:
如果您覺得這篇部落格對您有幫助或啟發,請點選右側【推薦】支援,謝謝!
  分類: TensorFlow.Net

專案開發環境為Visual Studio 2019 + .Net 5

建立新專案後首先通過Nuget引入相關包:

 

 SciSharp.TensorFlow.Redist是Google提供的TensorFlow開發庫,是採用C語言開發的動態連結庫(DLL);

TensorFlow.NET採用C#語言對C語言的庫進行封裝,提供.NET呼叫介面;

TensorFlow.Keras是一個高階工具類,對建模和訓練過程進行封裝,提供簡便介面。

 通過下列語句對庫進行引用:

using Tensorflow;

using Tensorflow.NumPy;

using static Tensorflow.Binding;

using static Tensorflow.KerasApi;

下面展示一些TensorFlow.NET的基本型別操作:

       /// <summary>
        /// 構建張量
        /// </summary>
        private void Base_Constant()
        {
            //通過基本型別構建張量
            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);

            //通過多維數值構建張量
            int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            var nd = np.array(arr);
            var tensor = tf.constant(nd);
            Console.WriteLine(tensor);

            //構建全0或全1張量
            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>
        /// 張量運算
        /// </summary>
        private void Base_Operator()
        {
            var a = tf.constant(2.0f);
            var b = tf.constant(3.0f);
            var c = tf.constant(5.0f);

            // 基本運算,可以採+ - * / 等運算子
            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}");

            // 求平均、求和
            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());

            // 矩陣相乘
            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>
        /// 生成隨機數張量
        /// </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()}");
        }

上述程式碼基本都比較簡單,基本一看就能懂,有幾處需要解釋一下:

1、平常我們在生成隨機數時,一般都是平均分佈,但機器學習的資料更多趨向正態分佈,所以採用normal生成隨機數,mean表示中心點,stddev表示分佈範圍;

2、從表面看tf的框架似乎提供了一套可以進行矩陣運算的Math庫,但實際並非如此,tf框架的核心是可以計算運算的梯度,這個問題我們後面再講;

3、tf有兩個版本,V1版和V2版本,如果要使用V1版本語法,需要在程式碼之前加一句:tf.compat.v1.disable_eager_execution();

     相對的,V2版本為:tf.enable_eager_execution();由於預設為V2版本,所以這行程式碼可以省略不寫。

    本系列的所有程式碼均採用V2版本。官方提供的樣例裡有大量V1版本程式碼,有一些V2版沒有提供的功能,可能不得不採用V1版程式碼實現。

 

【參考資料】

TensorFlow教程:TensorFlow快速入門教程

 

【專案原始碼】

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

專案名稱:SayHello

目錄:TensorFlow.NET機器學習入門系列目錄