コード例 #1
0
        void _Create(GraphFactory graph, int inputSize, float[] memory, string memoryId)
        {
            _inputSize = inputSize;
            int hiddenLayerSize = memory.Length;

            _memory = new MemoryFeeder(memory, null, memoryId);
            _input  = new FlowThrough();

            var Wz = graph.Connect(inputSize, _input).AddFeedForward(hiddenLayerSize, "Wz");
            var Uz = graph.Connect(hiddenLayerSize, _memory).AddFeedForward(hiddenLayerSize, "Uz");

            var Wr = graph.Connect(inputSize, _input).AddFeedForward(hiddenLayerSize, "Wr");
            var Ur = graph.Connect(hiddenLayerSize, _memory).AddFeedForward(hiddenLayerSize, "Ur");

            // add sigmoids to the gates
            var Rt = graph.Add(Wr, Ur).AddBackwardAction(new ConstrainSignal()).Add(graph.SigmoidActivation("Rt")).LastNode;
            var Zt = graph.Add(Wz, Uz).AddBackwardAction(new ConstrainSignal()).Add(graph.SigmoidActivation("Zt")).LastNode;

            // h1 = tanh(Wh(x) + Uh(Ht1xRt))
            var Wh = graph.Connect(inputSize, _input).AddFeedForward(hiddenLayerSize, "Wh");
            var Uh = graph.Multiply(hiddenLayerSize, Rt, _memory).AddFeedForward(hiddenLayerSize, "Uh");
            var h1 = graph.Add(Wh, Uh).AddBackwardAction(new ConstrainSignal()).Add(graph.TanhActivation());

            // h2 = h1x(1-Zt)
            var h2 = graph.Multiply(h1, graph.Connect(hiddenLayerSize, Zt).Add(graph.CreateOneMinusInput()));

            // h = h1xh2
            var previous = graph.Multiply(hiddenLayerSize, Zt, _memory);

            _output = graph
                      .Add(h2, previous)
                      .AddForwardAction(_memory.SetMemoryAction)
                      //.Add(new HookErrorSignal(context => {
                      //    if (_lastBackpropagation != null) {
                      //        foreach (var item in _lastBackpropagation)
                      //            context.AppendErrorSignal(item.Value, item.Key);
                      //        _lastBackpropagation = null;
                      //    }
                      //}))
                      .LastNode
            ;
            _start = new OneToMany(SubNodes, bp => _lastBackpropagation = bp);
        }