-
Notifications
You must be signed in to change notification settings - Fork 358
/
UINeuralNetworkPanel.cs
68 lines (56 loc) · 2.01 KB
/
UINeuralNetworkPanel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// Author: Samuel Arzt
/// Date: March 2017
#region Includes
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#endregion
/// <summary>
/// Class for displaying a neural networks topology.
/// </summary>
public class UINeuralNetworkPanel : MonoBehaviour
{
#region Members
// First (dummy) layer set by Unity editor
[SerializeField]
private List<UINeuralNetworkLayerPanel> Layers;
#endregion
#region Methods
/// <summary>
/// Displays the given neural network.
/// </summary>
/// <param name="neuralNet">The neural network to be displayed.</param>
public void Display(NeuralNetwork neuralNet)
{
UINeuralNetworkLayerPanel dummyLayer = Layers[0];
//Duplicate dummyLayer
for (int i = Layers.Count; i < neuralNet.Layers.Length + 1; i++)
{
UINeuralNetworkLayerPanel newPanel = Instantiate(dummyLayer);
newPanel.transform.SetParent(this.transform, false);
Layers.Add(newPanel);
}
//Destory all unnecessary layers
for (int i = this.Layers.Count-1; i >= neuralNet.Layers.Length + 1; i++)
{
UINeuralNetworkLayerPanel toBeDestroyed = Layers[i];
Layers.RemoveAt(i);
Destroy(toBeDestroyed);
}
//Set layer contents
for (int l = 0; l<this.Layers.Count - 1; l++)
this.Layers[l].Display(neuralNet.Layers[l]);
this.Layers[Layers.Count - 1].Display(neuralNet.Layers[neuralNet.Layers.Length - 1].OutputCount);
StartCoroutine(DrawConnections(neuralNet));
}
// Draw the connections (coroutine).
private IEnumerator DrawConnections(NeuralNetwork neuralNet)
{
yield return new WaitForEndOfFrame();
//Draw node connections
for (int l = 0; l < this.Layers.Count - 1; l++)
this.Layers[l].DisplayConnections(neuralNet.Layers[l], this.Layers[l + 1]);
this.Layers[this.Layers.Count - 1].HideAllConnections();
}
#endregion
}