Ejemplo n.º 1
0
        public override List <Tensor> Call(List <Array> inputs)
        {
            var feed_dict = new Dictionary <Tensor, Array>();

            foreach (var(tensor, value) in Enumerable.Zip(this.inputs, inputs, (a, b) => (a, b)))
            {
                // if (is_sparse(tensor))
                // {
                //     sparse_coo = value.tocoo()
                //     indices = np.concatenate((np.expand_dims(sparse_coo.row, 1),
                //                               np.expand_dims(sparse_coo.col, 1)), 1)
                //     value = (indices, sparse_coo.data, sparse_coo.shape)
                // }
                feed_dict[tensor] = value;
            }

            var session = K._SESSION;

            var init = tf.GetGlobalVariablesInitializer();

            if (init.Length > 0)
            {
                Console.WriteLine("Initializing variables:");
                foreach (var op in init)
                {
                    Console.WriteLine(" - " + op.Name);
                    session.Run(new TFOutput[0], new TFTensor[0], new TFOutput[0], new[] { op });
                }

                Console.WriteLine("Operations:");
                foreach (var op in tf.GetEnumerator())
                {
                    Console.WriteLine(" - " + op.Name);
                }
                Console.WriteLine();
            }

            //Console.WriteLine("Before:");
            //PrintVariables(feed_dict, session);
            // Console.ReadKey();

            var runner = session.GetRunner();

            foreach (var o in this.outputs)
            {
                runner.Fetch(K.In(o).output);
            }

            foreach (var op in this.updates_op)
            {
                runner.AddTarget(op);
            }

            foreach (KeyValuePair <Tensor, Array> pair in feed_dict)
            {
                TensorFlowTensor t = K.In(pair.Key);
                runner.AddInput(t.output, pair.Value);
            }



            var updated = runner.Run();

            //Console.WriteLine();

            //foreach (var v in updated)
            //{
            //    object obj = v.GetValue();
            //    if (obj is float[,])
            //        Console.WriteLine((obj as float[,]).ToCSharp());
            //    else if (obj is float[])
            //        Console.WriteLine((obj as float[]).ToCSharp());
            //    else
            //        Console.WriteLine(obj);
            //}

            //Console.WriteLine();
            //Console.WriteLine();

            //Console.WriteLine("After:");
            //PrintVariables(feed_dict, session);

            return(updated.Get(0, this.outputs.Count).Select(t => K.Out(t)).ToList());

            // Console.ReadKey();
        }
Ejemplo n.º 2
0
        private void PrintVariables(Dictionary <Tensor, Array> feed_dict, TFSession session)
        {
            string[] ops =
            {
                //"SGD/grad/dense_1/dense_1/kernel/var",
                //"SGD/grad/dense_2/dense_2/kernel/var",
                //"SGD/grad/dense_2/dense_2/bias/var",
                //"loss/dense_1_loss/y_true",
                //"loss/dense_1_loss/y_pred",
                //"loss/dense_1_loss/weights",
                //"iterations/var",
                //"lr/var",
                //"lr_t",
                //"p_t",
                //"metrics/binary_accuracy/Round0",
                //"metrics/binary_accuracy/Cast0",
                //"metrics/binary_accuracy/Mean0",
                //"metrics/binary_accuracy/Equal0",
                //"metrics/binary_accuracy/value",
                //"metrics/score_array/mean"
                //"beta_1/var",
                //"beta_2/var",
                //"decay/var",
                //"adam/grad/dense_1/dense_1/kernel/var",
                //"dense_1/variance_scaling/1/scaled",
                //"dense_1/dense_1/kernel/var",
                //"dense_1/call/dot",
                //"dense_1/call/Sigmoid0",
            };

            foreach (var op in ops)
            {
                try
                {
                    var debugRunner = session.GetRunner();
                    foreach (KeyValuePair <Tensor, Array> pair in feed_dict)
                    {
                        TensorFlowTensor t = K.In(pair.Key);
                        debugRunner.AddInput(t.output, pair.Value);
                    }

                    Console.WriteLine(op);
                    debugRunner.Fetch(op);

                    var v = debugRunner.Run();

                    object obj = v[0].GetValue();

                    if (obj is float[, ])
                    {
                        Console.WriteLine((obj as float[, ]).ToCSharp());
                    }
                    else if (obj is float[])
                    {
                        Console.WriteLine((obj as float[]).ToCSharp());
                    }
                    else if (obj is bool[, ])
                    {
                        Console.WriteLine((obj as bool[, ]).ToCSharp());
                    }
                    else if (obj is bool[])
                    {
                        Console.WriteLine((obj as bool[]).ToCSharp());
                    }
                    else if (obj is sbyte[, ])
                    {
                        Console.WriteLine((obj as sbyte[, ]).ToCSharp());
                    }
                    else if (obj is sbyte[])
                    {
                        Console.WriteLine((obj as sbyte[]).ToCSharp());
                    }
                    else
                    {
                        Console.WriteLine(obj);
                    }
                }
                catch
                {
                }
            }
        }