This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTNRNetClusterChain.cs
114 lines (101 loc) · 3.88 KB
/
TNRNetClusterChain.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using RCNet.MathTools;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RCNet.Neural.Network.NonRecurrent
{
/// <summary>
/// Implements the chain of cooperating non-recurrent network clusters.
/// </summary>
/// <remarks>
/// <para>
/// The chain can contain one or more clusters. When the chain contains more than one cluster
/// then the next cluster gets except the predictors also the output from the previous cluster.
/// The pure cross-validation approach is still kept, no one network is tested on the data used
/// for the training of the network providing the inputs for this network (nor indirectly).
/// This approach sometime improves the results.
/// </para>
/// <para>
/// As the final result is considered the output of the last cluster in the chain.
/// </para>
/// </remarks>
[Serializable]
public class TNRNetClusterChain
{
//Attribute properties
/// <summary>
/// The name of the cluster chain.
/// </summary>
public string ChainName { get; }
/// <inheritdoc cref="TNRNet.OutputType"/>
public TNRNet.OutputType Output { get; }
//Attributes
private readonly List<TNRNetCluster> _clusterCollection;
//Constructor
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="chainName">The name of the cluster chain.</param>
/// <param name="outputType">The type of output.</param>
public TNRNetClusterChain(string chainName, TNRNet.OutputType outputType)
{
ChainName = chainName;
Output = outputType;
_clusterCollection = new List<TNRNetCluster>();
return;
}
//Properties
/// <summary>
/// Indicates the chain is finalized and ready to operate.
/// </summary>
public bool Ready { get { return _clusterCollection.Count > 0; } }
/// <summary>
/// Gets the number of outputs.
/// </summary>
public int NumOfOutputs { get { return Ready ? _clusterCollection.Last().NumOfOutputs : 0; } }
/// <summary>
/// Gets the last cluster in the chain.
/// </summary>
public TNRNetCluster MainCluster { get { return _clusterCollection.Last(); } }
/// <summary>
/// Gets the output data range.
/// </summary>
public Interval OutputDataRange { get { return TNRNet.GetOutputDataRange(Output); } }
//Methods
/// <summary>
/// Adds the cluster into the chain.
/// </summary>
/// <param name="cluster">The cluster to be added into the chain.</param>
public void AddCluster(TNRNetCluster cluster)
{
_clusterCollection.Add(cluster);
return;
}
/// <summary>
/// Computes the cluster chain.
/// </summary>
/// <param name="inputVector">The input vector.</param>
/// <param name="subResults">The collection of the main cluster's sub-results.</param>
public double[] Compute(double[] inputVector, out List<Tuple<int, double[]>> subResults)
{
if (!Ready)
{
throw new InvalidOperationException("Chain is not ready. There is no cluster in the chain.");
}
subResults = null;
double[] result = null;
foreach (TNRNetCluster cluster in _clusterCollection)
{
if (subResults == null)
{
result = cluster.Compute(inputVector, out subResults);
}
else
{
result = cluster.Compute(inputVector, new List<Tuple<int, double[]>>(subResults), out subResults);
}
}
return result;
}
}//TNRNetClusterChain
}//Namespace