コード例 #1
0
        public static TFOutput LogOfLaplace(TFGraph graph, TFOutput x, TFOutput mu, TFOutput sigma)
        {
            TFOutput term1 = graph.Mul(graph.Const(-1F), graph.Log(sigma));
            TFOutput term2 = graph.Div(graph.Abs(graph.Sub(x, mu)), graph.Mul(graph.Const(-1F), sigma));

            return(graph.Add(term1, term2));
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // 加载手写数字资源
            var mnist = Mnist.Load();

            // 训练次数和测试次数
            var trainCount = 5000;
            var testCount  = 200;

            // 获取训练图片、训练图片标签、测试图片、测试图片标签
            float[,] trainingImages, trainingLabels, testImages, testLabels;
            mnist.GetTrainReader().NextBatch(trainCount, out trainingImages, out trainingLabels);
            mnist.GetTestReader().NextBatch(testCount, out testImages, out testLabels);

            // 创建图
            var g = new TFGraph();

            // 训练图片占位符和训练标签占位符
            var trainingInput = g.Placeholder(TFDataType.Float, new TFShape(-1, 784)); // 不定数量的像素为24*24的图片
            var xte           = g.Placeholder(TFDataType.Float, new TFShape(784));

            // 创建计算误差和预测的图
            var distance = g.ReduceSum(g.Abs(g.Add(trainingInput, g.Neg(xte))), axis: g.Const(1));
            var pred     = g.ArgMin(distance, g.Const(0));

            // 创建会话
            var sess = new TFSession(g);

            // 精度
            var accuracy = 0.0f;

            // 进行迭代训练,并且每次都输出预测值
            for (int i = 0; i < testCount; i++)
            {
                var runner = sess.GetRunner();

                // 计算并且获取误差和预测值
                var result = runner.
                             Fetch(pred).
                             Fetch(distance).
                             AddInput(trainingInput, trainingImages).
                             AddInput(xte, Extract(testImages, i)).Run();
                var r  = result[0].GetValue();
                var tr = result[1].GetValue();

                var nn_index = (int)(long)result[0].GetValue();

                Console.WriteLine($"训练次数 {i}: 预测: { ArgMax(trainingLabels, nn_index) } 真实值: { ArgMax(testLabels, i)} (nn_index= { nn_index })");
                if (ArgMax(trainingLabels, nn_index) == ArgMax(testLabels, i))
                {
                    accuracy += 1f / testImages.Length;
                }
            }

            // 精确度
            Console.WriteLine("精度:" + accuracy);
        }
コード例 #3
0
        // This sample has a bug, I suspect the data loaded is incorrect, because the returned
        // values in distance is wrong, and so is the prediction computed from it.
        void NearestNeighbor()
        {
            // Get the Mnist data

            var mnist = Mnist.Load();

            // 5000 for training
            const int trainCount = 5000;
            const int testCount  = 200;
            var       Xtr        = mnist.GetBatchReader(mnist.TrainImages).ReadAsTensor(trainCount);
            var       Ytr        = mnist.OneHotTrainLabels;
            var       Xte        = mnist.GetBatchReader(mnist.TestImages).Read(testCount);
            var       Yte        = mnist.OneHotTestLabels;



            Console.WriteLine("Nearest neighbor on Mnist images");
            using (var g = new TFGraph()) {
                var s = new TFSession(g);


                TFOutput xtr = g.Placeholder(TFDataType.Float, new TFShape(-1, 784));

                TFOutput xte = g.Placeholder(TFDataType.Float, new TFShape(784));

                // Nearest Neighbor calculation using L1 Distance
                // Calculate L1 Distance
                TFOutput distance = g.ReduceSum(g.Abs(g.Add(xtr, g.Neg(xte))), axis: g.Const(1));

                // Prediction: Get min distance index (Nearest neighbor)
                TFOutput pred = g.ArgMin(distance, g.Const(0));

                var accuracy = 0f;
                // Loop over the test data
                for (int i = 0; i < testCount; i++)
                {
                    var runner = s.GetRunner();

                    // Get nearest neighbor

                    var result   = runner.Fetch(pred).Fetch(distance).AddInput(xtr, Xtr).AddInput(xte, Xte [i].DataFloat).Run();
                    var r        = result [0].GetValue();
                    var tr       = result [1].GetValue();
                    var nn_index = (int)(long)result [0].GetValue();

                    // Get nearest neighbor class label and compare it to its true label
                    Console.WriteLine($"Test {i}: Prediction: {ArgMax (Ytr, nn_index)} True class: {ArgMax (Yte, i)} (nn_index={nn_index})");
                    if (ArgMax(Ytr, nn_index) == ArgMax(Yte, i))
                    {
                        accuracy += 1f / Xte.Length;
                    }
                }
                Console.WriteLine("Accuracy: " + accuracy);
            }
        }
コード例 #4
0
 public Tensor Abs(Tensor x)
 {
     return(Out(tf.Abs(In(x))));
 }
コード例 #5
0
        static void Main(string[] args)
        {
            // 创建所需数据
            var xList = new List <double>();
            var yList = new List <double>();
            var ran   = new Random();

            for (var i = 0; i < 10; i++)
            {
                var num   = ran.NextDouble();
                var noise = ran.NextDouble();
                xList.Add(num);
                yList.Add(num * 3 + 4 + noise); // y = 3 * x + 4
            }
            var xData         = xList.ToArray();
            var yData         = yList.ToArray();
            var learning_rate = 0.01;

            // 创建图
            var g = new TFGraph();

            // 创建占位符
            var x = g.Placeholder(TFDataType.Double, new TFShape(xData.Length));
            var y = g.Placeholder(TFDataType.Double, new TFShape(yData.Length));

            // 权重和偏置
            var W = g.VariableV2(TFShape.Scalar, TFDataType.Double, operName: "weight");
            var b = g.VariableV2(TFShape.Scalar, TFDataType.Double, operName: "bias");

            var initW = g.Assign(W, g.Const(ran.NextDouble()));
            var initb = g.Assign(b, g.Const(ran.NextDouble()));

            var output = g.Add(g.Mul(x, W), b);

            // 损失
            var loss     = g.ReduceSum(g.Abs(g.Sub(output, y)));
            var grad     = g.AddGradients(new TFOutput[] { loss }, new TFOutput[] { W, b });
            var optimize = new[]
            {
                g.AssignSub(W, g.Mul(grad[0], g.Const(learning_rate))).Operation,
                g.AssignSub(b, g.Mul(grad[1], g.Const(learning_rate))).Operation
            };

            // 创建会话
            var sess = new TFSession(g);

            // 变量初始化
            sess.GetRunner().AddTarget(initW.Operation, initb.Operation).Run();

            // 进行训练拟合
            for (var i = 0; i < 1000; i++)
            {
                var result = sess.GetRunner()
                             .AddInput(x, xData)
                             .AddInput(y, yData)
                             .AddTarget(optimize)
                             .Fetch(loss, W, b).Run();

                Console.WriteLine("loss: {0} W:{1} b:{2}", result[0].GetValue(), result[1].GetValue(), result[2].GetValue());
            }
        }
コード例 #6
0
 public Tensor abs(Tensor input)
 {
     return(Out(tf.Abs(In(input))));
 }
コード例 #7
0
ファイル: Abs.cs プロジェクト: epignatelli/TensorflowVisual
 public static TensorFlow.TFOutput Abs(this TFGraph graph, TensorFlow.TFOutput x, string operName = null)
 {
     return(graph.Abs(x, operName));
 }
コード例 #8
0
        static void KNN()
        {
            //取得数据
            var mnist = Mnist.Load();

            //拿5000个训练数据,200个测试数据
            const int trainCount = 5000;
            const int testCount  = 200;

            //获得的数据有两个
            //一个是图片,它们都是28*28的
            //一个是one-hot的标签,它们都是1*10的
            (var trainingImages, var trainingLabels) = mnist.GetTrainReader().NextBatch(trainCount);
            (var testImages, var testLabels)         = mnist.GetTestReader().NextBatch(testCount);

            Console.WriteLine($"MNIST 1NN");

            //建立一个图表示计算任务
            using (var graph = new TFGraph())
            {
                var session = new TFSession(graph);

                //用来feed数据的占位符。trainingInput表示N张用来进行训练的图片,N是一个变量,所以这里使用-1
                TFOutput trainingInput = graph.Placeholder(TFDataType.Float, new TFShape(-1, 784));

                //xte表示一张用来测试的图片
                TFOutput xte = graph.Placeholder(TFDataType.Float, new TFShape(784));

                //计算这两张图片的L1距离。这很简单,实际上就是把784个数字逐对相减,然后取绝对值,最后加起来变成一个总和
                var distance = graph.ReduceSum(graph.Abs(graph.Sub(trainingInput, xte)), axis: graph.Const(1));

                //这里只是用了最近的那个数据
                //也就是说,最近的那个数据是什么,那pred(预测值)就是什么
                TFOutput pred = graph.ArgMin(distance, graph.Const(0));

                var accuracy = 0f;

                //开始循环进行计算,循环trainCount次
                for (int i = 0; i < testCount; i++)
                {
                    var runner = session.GetRunner();

                    //每次,对一张新的测试图,计算它和trainCount张训练图的距离,并获得最近的那张
                    var result = runner.Fetch(pred).Fetch(distance)
                                 //trainCount张训练图(数据是trainingImages)
                                 .AddInput(trainingInput, trainingImages)
                                 //testCount张测试图(数据是从testImages中拿出来的)
                                 .AddInput(xte, Extract(testImages, i))
                                 .Run();

                    //最近的点的序号
                    var nn_index = (int)(long)result[0].GetValue();

                    //从trainingLabels中找到答案(这是预测值)
                    var prediction = ArgMax(trainingLabels, nn_index);

                    //正确答案位于testLabels[i]中
                    var real = ArgMax(testLabels, i);

                    //PrintImage(testImages, i);

                    Console.WriteLine($"测试 {i}: " +
                                      $"预测: {prediction} " +
                                      $"正确答案: {real} (最近的点的序号={nn_index})");
                    //Console.WriteLine(testImages);

                    if (prediction == real)
                    {
                        accuracy += 1f / testCount;
                    }
                }
                Console.WriteLine("准确率: " + accuracy);

                session.CloseSession();
            }
        }