Beispiel #1
0
        public void TestDiscreteApply()
        {
            var actionSpec = ActionSpec.MakeDiscrete(3, 2);

            var applier  = new DiscreteActionOutputApplier(actionSpec, 2020, null);
            var agentIds = new List <int> {
                42, 1337
            };
            var actionBuffers = new Dictionary <int, ActionBuffers>();

            actionBuffers[42]   = new ActionBuffers(actionSpec);
            actionBuffers[1337] = new ActionBuffers(actionSpec);

            var actionTensor = new TensorProxy
            {
                data = new Tensor(
                    2,
                    2,
                    new[]
                {
                    2.0f,     // Agent 0, branch 0
                    1.0f,     // Agent 0, branch 1
                    0.0f,     // Agent 1, branch 0
                    0.0f      // Agent 1, branch 1
                }),
                shape     = new long[] { 2, 2 },
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            applier.Apply(actionTensor, agentIds, actionBuffers);
            Assert.AreEqual(2, actionBuffers[42].DiscreteActions[0]);
            Assert.AreEqual(1, actionBuffers[42].DiscreteActions[1]);

            Assert.AreEqual(0, actionBuffers[1337].DiscreteActions[0]);
            Assert.AreEqual(0, actionBuffers[1337].DiscreteActions[1]);
        }
Beispiel #2
0
        public void ApplyMemoryOutput()
        {
            var inputTensor = new TensorProxy()
            {
                Shape = new long[] { 2, 5 },
                Data  = new Tensor(2, 5, new[] { 0.5f, 22.5f, 0.1f, 5f, 1f,
                                                 4f, 5f, 6f, 7f, 8f })
            };
            var agentInfos = GetFakeAgentInfos();

            var applier = new MemoryOutputApplier();

            applier.Apply(inputTensor, agentInfos);
            var agents = agentInfos.Keys.ToList();
            var agent  = agents[0] as TestAgent;
            var action = agent.GetAction();

            Assert.AreEqual(action.memories[0], 0.5f);
            Assert.AreEqual(action.memories[1], 22.5f);
            agent  = agents[1] as TestAgent;
            action = agent.GetAction();
            Assert.AreEqual(action.memories[2], 6);
            Assert.AreEqual(action.memories[3], 7);
        }
Beispiel #3
0
        public void TestResizeTensor(int dimension)
        {
            if (dimension == 8)
            {
                // Barracuda 1.0.x doesn't support 8D tensors
                // Barracuda 1.1.x does but it initially broke ML-Agents support
                // Unfortunately, the PackageInfo methods don't exist in earlier versions of the editor,
                // so just skip that variant of the test then.
                // It's unlikely, but possible that we'll upgrade to a newer dependency of Barracuda,
                // in which case we should make sure this test is run then.
#if UNITY_2019_3_OR_NEWER
                var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(Tensor).Assembly);
                Assert.AreEqual("com.unity.barracuda", packageInfo.name);
                var barracuda8DSupport       = new Version(1, 1, 0);
                var strippedBarracudaVersion = packageInfo.version.Replace("-preview", "");
                var version = new Version(strippedBarracudaVersion);
                if (version <= barracuda8DSupport)
                {
                    return;
                }
#else
                return;
#endif
            }
            var alloc    = new TensorCachingAllocator();
            var height   = 64;
            var width    = 84;
            var channels = 3;

            // Set shape to {1, ..., height, width, channels}
            // For 8D, the ... are all 1's
            var shape = new long[dimension];
            for (var i = 0; i < dimension; i++)
            {
                shape[i] = 1;
            }

            shape[dimension - 3] = height;
            shape[dimension - 2] = width;
            shape[dimension - 1] = channels;

            var intShape = new int[dimension];
            for (var i = 0; i < dimension; i++)
            {
                intShape[i] = (int)shape[i];
            }

            var tensorProxy = new TensorProxy
            {
                valueType = TensorProxy.TensorType.Integer,
                data      = new Tensor(intShape),
                shape     = shape,
            };

            // These should be invariant after the resize.
            Assert.AreEqual(height, tensorProxy.data.shape.height);
            Assert.AreEqual(width, tensorProxy.data.shape.width);
            Assert.AreEqual(channels, tensorProxy.data.shape.channels);

            TensorUtils.ResizeTensor(tensorProxy, 42, alloc);

            Assert.AreEqual(height, tensorProxy.shape[dimension - 3]);
            Assert.AreEqual(width, tensorProxy.shape[dimension - 2]);
            Assert.AreEqual(channels, tensorProxy.shape[dimension - 1]);

            Assert.AreEqual(height, tensorProxy.data.shape.height);
            Assert.AreEqual(width, tensorProxy.data.shape.width);
            Assert.AreEqual(channels, tensorProxy.data.shape.channels);

            alloc.Dispose();
        }
 public void WriteToTensor(TensorProxy tensorProxy, int agentIndex)
 {
 }