示例#1
0
 Color RawNodeColor(float r) => TurboColorMap.Map(math.unlerp(_rawNodeMinMax.x, _rawNodeMinMax.y, r));
示例#2
0
 Color ActNodeColor(float a) => TurboColorMap.Map(math.unlerp(_actNodeMinMax.x, _actNodeMinMax.y, a));
        private void OnGenerateVisualContent(MeshGenerationContext cxt)
        {
            MeshParts            mp  = new MeshParts();
            MultiLayerPerception mlp = _TestMLP;

            AddRect(mp, 0, 1, Color.gray);
            if (mlp != null)
            {
                using (IWorker oneshotSyncWorker =
                           WorkerFactory.CreateWorker(_testMLP.model, _extraLayers, WorkerFactory.Device.GPU)) {
                    using (Tensor obsTensor = new Tensor(new TensorShape(1, mlp._shape.inputSize))) {
                        if (_observe.Length < mlp._shape.inputSize)
                        {
                            _observe = new float[mlp._shape.inputSize];
                        }
                        for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++)
                        {
                            obsTensor[iINode] = _observe[iINode];
                        }

                        oneshotSyncWorker.Execute(obsTensor).FlushSchedule();
                    }

                    for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++)
                    {
                        AddRect(mp, GetNodePos(0, iINode), NodeSize, ActNodeColor(_observe[iINode]));
                    }


                    using (Tensor hvr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Hidden)) {
                        using (Tensor hva = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.HiddenActive)) {
                            for (int iHNode = 0; iHNode < mlp._shape.hiddenSize; iHNode++)
                            {
                                AddRect(mp, GetNodePos(1, iHNode), NodeSize, RawNodeColor(hvr[iHNode]));
                                AddRect(mp, GetNodePos(1, iHNode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize,
                                        ActNodeColor(hva[iHNode]));
                            }
                        }
                    }

                    using (Tensor ovr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Output)) {
                        using (Tensor ova = oneshotSyncWorker.PeekOutput()) {
                            for (int iONode = 0; iONode < mlp._shape.outputSize; iONode++)
                            {
                                AddRect(mp, GetNodePos(2, iONode), NodeSize, RawNodeColor(ovr[iONode]));
                                AddRect(mp, GetNodePos(2, iONode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize,
                                        ActNodeColor(ova[iONode]));
                            }
                        }
                    }
                }

                string[] layerNames = new string[]
                { MultiLayerPerception.LayerNames.Hidden, MultiLayerPerception.LayerNames.Output };
                float2 xBuf = NodeSize / 2;
                xBuf.y = 0;
                int prvLayer = 0;
                int curLayer = 1;
                foreach (string layerName in layerNames)
                {
                    TensorShape tShape = _testMLP.GetLayerShape(layerName);
                    for (int iPNode = 0; iPNode < tShape.flatHeight; iPNode++)
                    {
                        for (int iCNode = 0; iCNode < tShape.flatWidth; iCNode++)
                        {
                            float2 posI = GetNodePos(prvLayer, iPNode) + NodeSize / 2;
                            float2 posW = GetNodePos(curLayer, iCNode) + NodeSize / 2;

                            float t = 0.5f + mlp.GetWeight(layerName, iPNode, iCNode);
                            DrawLine(mp, posI + xBuf, posW - xBuf, 0.025f, TurboColorMap.Map(t));
                        }
                    }

                    prvLayer = curLayer;
                    curLayer++;
                }
            }
            MeshWriteData meshData = cxt.Allocate(mp.vertices.Count, mp.IndicesCount);

            if (meshData.vertexCount > 0)
            {
                meshData.SetAllVertices(mp.vertices.ToArray());
                meshData.SetAllIndices(mp.GetIndices());
            }
        }