public void AddConvLayers(TFGraph graph, ConvLayer[] layers, TFOutput cost)
        {
            TFOutput init = new TFOutput();

            foreach (ConvLayer layer in layers)
            {
                _variables.Add(layer.W);
                _variables.Add(layer.b);

                _gradients.Add(graph.AddGradients(new TFOutput[] { cost }, new TFOutput[] { layer.W })[0]);
                _gradients.Add(graph.AddGradients(new TFOutput[] { cost }, new TFOutput[] { layer.b })[0]);



                init = graph.Zeros(graph.GetTensorShape(layer.W), TFDataType.Float);
                _m.Add(graph.VariableV2(graph.GetTensorShape(layer.W), TFDataType.Float));
                InitM.Add(graph.Assign(_m.Last(), init));

                init = graph.Zeros(graph.GetTensorShape(layer.b), TFDataType.Float);
                _m.Add(graph.VariableV2(graph.GetTensorShape(layer.b), TFDataType.Float));
                InitM.Add(graph.Assign(_m.Last(), init));


                init = graph.Zeros(graph.GetTensorShape(layer.W), TFDataType.Float);
                _v.Add(graph.VariableV2(graph.GetTensorShape(layer.W), TFDataType.Float));
                InitV.Add(graph.Assign(_v.Last(), init));

                init = graph.Zeros(graph.GetTensorShape(layer.b), TFDataType.Float);
                _v.Add(graph.VariableV2(graph.GetTensorShape(layer.b), TFDataType.Float));
                InitV.Add(graph.Assign(_v.Last(), init));
            }
        }
Exemple #2
0
 public GradientDescentOptimizer(TFGraph graph, TFOutput grad, TFOutput w, TFOutput b)
 {
     _variables    = new TFOutput[4];
     _variables[0] = w;
     _variables[1] = b;
     _gradients    = graph.AddGradients(new TFOutput[] { grad }, new TFOutput[] { w, b });
     Updates       = new TFOutput[4];
 }
        public void ShouldAddGradients()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var x = graph.Const(3.0);

                    var y1 = graph.Square(x, "Square1");
                    var y2 = graph.Square(y1, "Square2");

                    var y3 = graph.Square(y2, "Square3");
                    var g  = graph.AddGradients(new TFOutput [] { y1, y3 }, new [] { x });

                    var r  = session.Run(new TFOutput [] { }, new TFTensor [] { }, g);
                    var dy = (double)r [0].GetValue();
                    Assert.Equal(17502.0, dy);
                }
        }
        public void CanBeOptimized()
        {
            var graph   = new TFGraph();
            var context = new ModelCompilationContext(graph);

            var input  = new Input(new long[] { 10 }, name: "Input0");
            var output = new Dense(2, input, name: "Dense0");

            var compiledOutput = output.Compile(context);

            var loss = new NegativeLogLikelihood();

            var compiledLoss = loss.Compile(context, compiledOutput,
                                            context.Graph.Placeholder(TFDataType.Double, new TFShape(-1, 2)));

            var gradients = graph.AddGradients(
                new[] { compiledLoss },
                context.Parameters.ToArray());
        }
        public void ShouldBeOptimizable()
        {
            var graph   = new TFGraph();
            var context = new ModelCompilationContext(graph);

            var input  = new Input(new long[] { 10 }, name: "Input0");
            var output = new Dense(2, input, name: "Dense0");

            var compiledInput  = input.Compile(context);
            var compiledOutput = output.Compile(context);

            var loss = new CategoricalCrossEntropy();

            var compiledLoss = loss.Compile(context, compiledOutput,
                                            context.Graph.Placeholder(TFDataType.Double, new TFShape(-1, 2)));

            var gradients = graph.AddGradients(
                new [] { compiledLoss },
                context.Parameters.ToArray());
        }
        public void ComputeGradientMSE()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var X = graph.Const(5.5f);
                    var Y = graph.Const(2.09f);

                    var W    = graph.Const(0.1078f);
                    var b    = graph.Const(0.1021f);
                    var pred = graph.Add(graph.Mul(X, W, "x_w"), b);

                    var cost = graph.Div(graph.ReduceSum(graph.Pow(graph.Sub(pred, Y), graph.Const(2f))), graph.Mul(graph.Const(2f), graph.Const((float)17), "2_n_samples"));

                    var g = graph.AddGradients(new TFOutput[] { cost }, new[] { W });

                    var r = session.Run(new TFOutput[] { }, new TFTensor[] {  }, new TFOutput[] { cost, g[0] });
                    var d = (float)r[0].GetValue();
                    Assert.InRange(d, 0.057236027 - _tolerance, 0.057236027 + _tolerance);
                    d = (float)r[1].GetValue();
                    Assert.InRange(d, -0.4513235 - _tolerance, -0.4513235 + _tolerance);
                }
        }
        static void Main(string[] args)
        {
            // 创建图
            var g = new TFGraph();

            // 创建一些变量
            var x = g.Const(3.0);  // x=3

            var y1 = g.Square(x);  // x^2=9
            var y2 = g.Square(y1); // x^4=81
            var y3 = g.Square(y2); // x^8=6561

            // 计算倾斜度d(y1+y3)=d(x^2 + x^8)=2*x+8*x^7=17502
            var a = g.AddGradients(new[] { y1, y3 }, new[] { x });

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

            // 计算结果
            var result = sess.Run(new TFOutput[] { }, new TFTensor[] { }, a);

            // 输出计算结果
            Console.WriteLine((double)result[0].GetValue()); // 17502
        }
        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());
            }
        }
        // .length = layers


        public void Train(double[][] input, double[][] output)
        {
            int InputSize  = input[0].Length;
            int OutputSize = output[0].Length;

            using (var graph = new TFGraph())
            {
                TFOutput X = graph.Placeholder(TFDataType.Float, new TFShape(new long[] { -1, InputSize }));
                TFOutput Y = graph.Placeholder(TFDataType.Float, new TFShape(new long[] { -1, OutputSize }));

                TFOutput[] weights = new TFOutput[NodeSize.Length + 1];
                TFOutput[] biases  = new TFOutput[NodeSize.Length + 1];

                int prevSize = InputSize;
                for (int i = 0; i < NodeSize.Length; i++)
                {
                    weights[i] = graph.VariableV2(new TFShape(new long[] { prevSize, NodeSize[i] }), TFDataType.Float, operName: "weight_" + i);
                    biases[i]  = graph.VariableV2(new TFShape(new long[] { NodeSize[i] }), TFDataType.Float, operName: "bias_" + i);
                    prevSize   = NodeSize[i];
                }

                weights[NodeSize.Length] = graph.VariableV2(new TFShape(new long[] { prevSize, OutputSize }), TFDataType.Float, operName: "weight_out");
                biases[NodeSize.Length]  = graph.VariableV2(new TFShape(new long[] { OutputSize }), TFDataType.Float, operName: "bias_out");

                TFOutput pred = Predict(X, weights, biases, graph);

                TFOutput cost = graph.ReduceMean(graph.SigmoidCrossEntropyWithLogits(Y, pred));

                TFOutput[] parameters = new TFOutput[weights.Length + biases.Length];
                weights.CopyTo(parameters, 0);
                biases.CopyTo(parameters, weights.Length);

                TFOutput[] grad = graph.AddGradients(new TFOutput[] { cost }, parameters);

                TFOperation[] optimize = new TFOperation[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    optimize[i] = graph.AssignSub(parameters[i], graph.Mul(grad[i], graph.Const(LearningRate))).Operation;
                }

                TFSession sess = new TFSession(graph);

                TFOperation[] InitParams = new TFOperation[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    InitParams[i] = graph.Assign(parameters[i], graph.RandomNormal(graph.GetTensorShape(parameters[i]))).Operation;
                }

                sess.GetRunner().AddTarget(InitParams);

                for (int i = 0; i < Epochs; i++)
                {
                    TFTensor result = sess.GetRunner()
                                      .AddInput(X, input)
                                      .AddInput(Y, output)
                                      .AddTarget(optimize)
                                      .Fetch(cost)
                                      .Run();

                    if (i % DisplaySteps == 0)
                    {
                        Console.WriteLine("Epoch - " + i + " | Cost - " + result.GetValue());
                    }
                }
            }
        }
Exemple #10
0
 public static TensorFlow.TFOutput[] AddGradients(this TFGraph graph, string prefix, TensorFlow.TFOutput[] y, TensorFlow.TFOutput[] x, TensorFlow.TFOutput[] dx = null, TensorFlow.TFStatus status = null)
 {
     return(graph.AddGradients(prefix, y, x, dx, status));
 }