@@ -18,8 +18,9 @@ under the License.
18
18
* */
19
19
package io .github .mandar2812 .dynaml .evaluation
20
20
21
+ import com .quantifind .charts .Highcharts .{regression , title , xAxis , yAxis }
21
22
import io .github .mandar2812 .dynaml .tensorflow .dtf
22
- import org .platanios .tensorflow .api .{Tensor , :: , --- }
23
+ import org .platanios .tensorflow .api .{--- , :: , Tensor }
23
24
24
25
25
26
/**
@@ -29,22 +30,22 @@ import org.platanios.tensorflow.api.{Tensor, ::, ---}
29
30
*
30
31
* @param targets The actual output values.
31
32
* */
32
- abstract class MetricsTF (names : Seq [String ], preds : Tensor , targets : Tensor ) {
33
+ abstract class MetricsTF (val names : Seq [String ], val preds : Tensor , val targets : Tensor ) {
33
34
34
35
protected val scoresAndLabels : (Tensor , Tensor ) = (preds, targets)
35
36
36
37
protected var name = " Target"
37
38
38
39
lazy val results : Tensor = run()
39
40
40
- def _target_quantity = name
41
+ def _target_quantity : String = name
41
42
42
43
def target_quantity_ (n : String ): Unit = {
43
44
name = n
44
45
}
45
46
46
47
def print (): Unit = {
47
- println(" Model Performance: " + name)
48
+ println(" \n Model Performance: " + name)
48
49
println(" ============================" )
49
50
println()
50
51
@@ -76,24 +77,53 @@ abstract class MetricsTF(names: Seq[String], preds: Tensor, targets: Tensor) {
76
77
/**
77
78
* Implements a common use for Regression Task Evaluators.
78
79
* */
79
- class RegressionMetricsTF (preds : Tensor , targets : Tensor , num_partitions : Int = 4 )
80
- extends MetricsTF (Seq (" RMSE" , " MAE" , " Corr" ), preds, targets) {
80
+ class RegressionMetricsTF (preds : Tensor , targets : Tensor )
81
+ extends MetricsTF (Seq (" RMSE" , " MAE" , " Coefficient of Corr. " , " Yield " ), preds, targets) {
81
82
83
+ private val num_outputs = if (preds.shape.toTensor().size == 1 ) 1 else preds.shape(1 )
82
84
83
- private lazy val (_ , rmse , mae, corr) = RegressionMetricsTF .calculate(preds, targets, num_partitions )
85
+ private lazy val (_ , rmse , mae, corr) = RegressionMetricsTF .calculate(preds, targets)
84
86
85
- override protected def run (): Tensor = dtf.stack(Seq (rmse, mae, corr))
87
+ private lazy val modelyield =
88
+ (preds.max(axes = 0 ) - preds.min(axes = 0 )).divide(targets.max(axes = 0 ) - targets.min(axes = 0 ))
89
+
90
+ override protected def run (): Tensor = dtf.stack(Seq (rmse, mae, corr, modelyield))
91
+
92
+ override def generatePlots (): Unit = {
93
+ println(" Generating Plot of Fit for each target" )
94
+
95
+ if (num_outputs == 1 ) {
96
+ val (pr, tar) = (
97
+ scoresAndLabels._1.entriesIterator.map(_.asInstanceOf [Double ]),
98
+ scoresAndLabels._2.entriesIterator.map(_.asInstanceOf [Double ]))
99
+
100
+ regression(pr.zip(tar).toSeq)
101
+
102
+ title(" Goodness of fit: " + name)
103
+ xAxis(" Predicted " + name)
104
+ yAxis(" Actual " + name)
105
+
106
+ } else {
107
+ (0 until num_outputs).foreach(output => {
108
+ val (pr, tar) = (
109
+ scoresAndLabels._1(:: , output).entriesIterator.map(_.asInstanceOf [Double ]),
110
+ scoresAndLabels._2(:: , output).entriesIterator.map(_.asInstanceOf [Double ]))
111
+
112
+ regression(pr.zip(tar).toSeq)
113
+ })
114
+ }
115
+ }
86
116
}
87
117
88
118
/**
89
119
* Implements core logic of [[RegressionMetricsTF ]]
90
120
* */
91
121
object RegressionMetricsTF {
92
122
93
- protected def calculate (preds : Tensor , targets : Tensor , num_partitions : Int ) = {
123
+ protected def calculate (preds : Tensor , targets : Tensor ) : ( Tensor , Tensor , Tensor , Tensor ) = {
94
124
val error = targets.subtract(preds)
95
125
96
- println(" Shape of error tensor: " + error.shape.toString())
126
+ println(" Shape of error tensor: " + error.shape.toString()+ " \n " )
97
127
98
128
val num_instances = error.shape(0 )
99
129
val rmse = error.square.mean(axes = 0 ).sqrt
@@ -110,7 +140,9 @@ object RegressionMetricsTF {
110
140
111
141
val targets_c = targets.subtract(dtf.stack(Seq .fill(num_instances)(mean_targets)))
112
142
113
- preds_c.multiply(targets_c).mean(axes = 0 ).divide(preds_c.square.mean().sqrt).divide(targets_c.square.mean().sqrt)
143
+ val (sigma_t, sigma_p) = (targets_c.square.mean().sqrt, preds_c.square.mean().sqrt)
144
+
145
+ preds_c.multiply(targets_c).mean(axes = 0 ).divide(sigma_t.multiply(sigma_p))
114
146
}
115
147
116
148
(error, rmse, mae, corr)
0 commit comments