zoukankan      html  css  js  c++  java
  • TensorFlow.NET机器学习入门【1】开发环境与类型简介

    项目开发环境为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机器学习入门系列目录


    签名区:
    如果您觉得这篇博客对您有帮助或启发,请点击右侧【推荐】支持,谢谢!
  • 相关阅读:
    python socket 二进制
    全面介绍内存管理机制
    glog修改
    mysql c api
    http twisted
    IOCP 模型1
    IOCP 模型2 AcceptEx
    python
    Python Twisted
    Java 拾遗
  • 原文地址:https://www.cnblogs.com/seabluescn/p/15539089.html
Copyright © 2011-2022 走看看