예제 #1
0
        public void TestEvalBatching()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                Data = new Tensor(2, 3, new []
                {
                    Mathf.Log(0.1f) - 50, Mathf.Log(0.2f) - 50, Mathf.Log(0.7f) - 50,
                    Mathf.Log(0.3f) - 25, Mathf.Log(0.4f) - 25, Mathf.Log(0.3f) - 25
                }),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            TensorProxy dst = new TensorProxy
            {
                Data      = new Tensor(2, 3),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 2, 0, 1, 0 };
            for (var i = 0; i < dst.Data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.Data[i]);
                ++i;
            }
        }
예제 #2
0
        public void TestEvalBatching()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                Data = new float[2, 3]
                {
                    { Mathf.Log(0.1f) - 50, Mathf.Log(0.2f) - 50, Mathf.Log(0.7f) - 50 },
                    { Mathf.Log(0.3f) - 25, Mathf.Log(0.4f) - 25, Mathf.Log(0.3f) - 25 },
                },
                ValueType = Tensor.TensorType.FloatingPoint
            };

            Tensor dst = new Tensor
            {
                Data      = new float[2, 3],
                ValueType = Tensor.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 2, 0, 1, 0 };
            int     i         = 0;

            foreach (var f in dst.Data)
            {
                Assert.AreEqual(reference[i], f);
                ++i;
            }
        }
예제 #3
0
        public void TestEvalP()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                Data      = new Tensor(1, 3, new[] { 0.1f, 0.2f, 0.7f }),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            TensorProxy dst = new TensorProxy
            {
                Data      = new Tensor(1, 3),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 1 };
            for (var i = 0; i < dst.Data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.Data[i]);
                ++i;
            }
        }
예제 #4
0
        public void TestEvalP()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                Data = new float[1, 3] {
                    { 0.1f, 0.2f, 0.7f }
                },
                ValueType = Tensor.TensorType.FloatingPoint
            };

            Tensor dst = new Tensor
            {
                Data      = new float[1, 3],
                ValueType = Tensor.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 1 };
            int     i         = 0;

            foreach (var f in dst.Data)
            {
                Assert.AreEqual(reference[i], f);
                ++i;
            }
        }
예제 #5
0
        public void Apply(TensorProxy tensorProxy, Dictionary <Agent, AgentInfo> agentInfo)
        {
            //var tensorDataProbabilities = tensorProxy.Data as float[,];
            var batchSize          = agentInfo.Keys.Count;
            var actions            = new float[batchSize, _actionSize.Length];
            var startActionIndices = Utilities.CumSum(_actionSize);

            for (var actionIndex = 0; actionIndex < _actionSize.Length; actionIndex++)
            {
                var nBranchAction = _actionSize[actionIndex];
                var actionProbs   = new TensorProxy()
                {
                    ValueType = TensorProxy.TensorType.FloatingPoint,
                    Shape     = new long[] { batchSize, nBranchAction },
                    Data      = _allocator.Alloc(new TensorShape(batchSize, nBranchAction))
                };

                for (var batchIndex = 0; batchIndex < batchSize; batchIndex++)
                {
                    for (var branchActionIndex = 0;
                         branchActionIndex < nBranchAction;
                         branchActionIndex++)
                    {
                        actionProbs.Data[batchIndex, branchActionIndex] =
                            tensorProxy.Data[batchIndex, startActionIndices[actionIndex] + branchActionIndex];
                    }
                }

                var outputTensor = new TensorProxy()
                {
                    ValueType = TensorProxy.TensorType.FloatingPoint,
                    Shape     = new long[] { batchSize, 1 },
                    Data      = _allocator.Alloc(new TensorShape(batchSize, 1))
                };

                _multinomial.Eval(actionProbs, outputTensor);

                for (var ii = 0; ii < batchSize; ii++)
                {
                    actions[ii, actionIndex] = outputTensor.Data[ii, 0];
                }
            }
            var agentIndex = 0;

            foreach (var agent in agentInfo.Keys)
            {
                var action = new float[_actionSize.Length];
                for (var j = 0; j < _actionSize.Length; j++)
                {
                    action[j] = actions[agentIndex, j];
                }
                agent.UpdateVectorAction(action);
                agentIndex++;
            }
        }
예제 #6
0
        public void TestSrcInt()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.Integer
            };

            Assert.Throws <NotImplementedException>(() => m.Eval(src, null));
        }
예제 #7
0
        public void TestSrcDataNull()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.FloatingPoint
            };
            TensorProxy dst = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            Assert.Throws <ArgumentNullException>(() => m.Eval(src, dst));
        }
예제 #8
0
        public void TestDstInt()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                ValueType = Tensor.TensorType.FloatingPoint
            };
            Tensor dst = new Tensor
            {
                ValueType = Tensor.TensorType.Integer
            };

            Assert.Throws <ArgumentException>(() => m.Eval(src, dst));
        }
예제 #9
0
        public void TestDstDataNull()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                ValueType = Tensor.TensorType.FloatingPoint,
                Data      = new float[1]
            };
            Tensor dst = new Tensor
            {
                ValueType = Tensor.TensorType.FloatingPoint
            };

            Assert.Throws <ArgumentNullException>(() => m.Eval(src, dst));
        }
예제 #10
0
        public void TestUnequalBatchSize()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.FloatingPoint,
                Data      = new Tensor(1, 1)
            };
            TensorProxy dst = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.FloatingPoint,
                Data      = new Tensor(2, 1)
            };

            Assert.Throws <ArgumentException>(() => m.Eval(src, dst));
        }
예제 #11
0
        public void TestUnequalBatchSize()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                ValueType = Tensor.TensorType.FloatingPoint,
                Data      = new float[1, 1]
            };
            Tensor dst = new Tensor
            {
                ValueType = Tensor.TensorType.FloatingPoint,
                Data      = new float[2, 1]
            };

            Assert.Throws <ArgumentException>(() => m.Eval(src, dst));
        }