ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TensorFlow.NET机器学习入门【1】开发环境与类型简介

2021-12-22 11:04:23  阅读:186  来源: 互联网

标签:constant 入门 float Console var new tf 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机器学习入门系列目录

 

标签:constant,入门,float,Console,var,new,tf,TensorFlow,NET
来源: https://www.cnblogs.com/seabluescn/p/15539089.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有