Skip to content

Commit 8eafeaa

Browse files
committed
Added coregionalization component for Cauchy Kernel, added multivariate gaussian scaling of attributes
1 parent 25d0f3f commit 8eafeaa

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ packageDescription := "DynaML is a scala library/repl for implementing and worki
1212
"which can be extended easily to implement advanced models for small and large scale applications.\n\n"+
1313
"But the library can also be used as an educational/research tool for data analysis."
1414

15-
val mainVersion = "v1.4-beta.28"
15+
val mainVersion = "v1.4-beta.29"
1616

1717
val dataDirectory = settingKey[File]("The directory holding the data files for running example scripts")
1818

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/DynaMLPipe.scala

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import io.github.mandar2812.dynaml.models.ParameterizedLearner
2525
import io.github.mandar2812.dynaml.models.gp.AbstractGPRegressionModel
2626
import io.github.mandar2812.dynaml.optimization.{CoupledSimulatedAnnealing, GPMLOptimizer, GloballyOptWithGrad, GridSearch}
2727
import io.github.mandar2812.dynaml.pipes.{DataPipe, ReversibleScaler, Scaler, StreamDataPipe}
28-
import io.github.mandar2812.dynaml.utils.{GaussianScaler, MinMaxScaler}
28+
import io.github.mandar2812.dynaml.utils.{GaussianScaler, MinMaxScaler, MVGaussianScaler}
2929
import org.apache.log4j.Logger
3030

3131
/**
@@ -340,6 +340,33 @@ object DynaMLPipe {
340340
(scaler(trainTest), (featuresScaler, targetsScaler))
341341
})
342342

343+
/**
344+
* Scale a data set which is stored as a [[Stream]],
345+
* return the scaled data as well as a [[MVGaussianScaler]] instance
346+
* which can be used to reverse the scaled values to the original
347+
* data.
348+
* */
349+
val multivariateGaussianScaling =
350+
DataPipe((trainTest: Stream[(DenseVector[Double], DenseVector[Double])]) => {
351+
352+
val (num_features, num_targets) = (trainTest.head._1.length, trainTest.head._2.length)
353+
354+
val (m, sigma) = utils.getStatsMult(trainTest.map(tup =>
355+
DenseVector(tup._1.toArray ++ tup._2.toArray)).toList)
356+
357+
val featuresScaler = new MVGaussianScaler(
358+
m(0 until num_features),
359+
sigma(0 until num_features, 0 until num_features))
360+
361+
val targetsScaler = new MVGaussianScaler(
362+
m(num_features until num_features + num_targets),
363+
sigma(num_features until num_features + num_targets, num_features until num_features + num_targets))
364+
365+
val scaler: ReversibleScaler[(DenseVector[Double], DenseVector[Double])] = featuresScaler * targetsScaler
366+
367+
(scaler(trainTest), (featuresScaler, targetsScaler))
368+
})
369+
343370

344371
/**
345372
* Perform gaussian normalization on a data stream which
@@ -371,6 +398,36 @@ object DynaMLPipe {
371398
(scaler(trainTest._1), scaler(trainTest._2), (featuresScaler, targetsScaler))
372399
})
373400

401+
/**
402+
* Scale a data set which is stored as a [[Stream]],
403+
* return the scaled data as well as a [[MVGaussianScaler]] instance
404+
* which can be used to reverse the scaled values to the original
405+
* data.
406+
* */
407+
val multivariateGaussianScalingTrainTest =
408+
DataPipe((trainTest: (Stream[(DenseVector[Double], DenseVector[Double])],
409+
Stream[(DenseVector[Double], DenseVector[Double])])) => {
410+
411+
val (num_features, num_targets) = (trainTest._1.head._1.length, trainTest._1.head._2.length)
412+
413+
val (m, sigma) = utils.getStatsMult(trainTest._1.map(tup =>
414+
DenseVector(tup._1.toArray ++ tup._2.toArray)).toList)
415+
416+
val featuresScaler = new MVGaussianScaler(
417+
m(0 until num_features),
418+
sigma(0 until num_features, 0 until num_features))
419+
420+
val targetsScaler = new MVGaussianScaler(
421+
m(num_features until num_features + num_targets),
422+
sigma(num_features until num_features + num_targets, num_features until num_features + num_targets))
423+
424+
val scaler: ReversibleScaler[(DenseVector[Double], DenseVector[Double])] = featuresScaler * targetsScaler
425+
426+
(scaler(trainTest._1), scaler(trainTest._2), (featuresScaler, targetsScaler))
427+
428+
})
429+
430+
374431
/**
375432
* Scale a data set which is stored as a [[Stream]],
376433
* return the scaled data as well as a [[MinMaxScaler]] instance

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/kernels/CauchyKernel.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,18 @@ class CauchyCovFunc(private var sigma: Double)
4242
override def gradient(x: Double, y: Double): Map[String, Double] = {
4343
Map("sigma" -> 2.0*math.pow(evaluate(x,y),2)*math.pow(x-y, 2)/math.pow(state("sigma"), 3))
4444
}
45-
}
45+
}
46+
47+
class CoRegCauchyKernel(bandwidth: Double) extends LocalSVMKernel[Int] {
48+
49+
override val hyper_parameters: List[String] = List("coRegSigma")
50+
51+
state = Map("coRegLB" -> bandwidth)
52+
53+
override def gradient(x: Int, y: Int): Map[String, Double] =
54+
Map("CoRegSigma" -> 2.0*math.pow(evaluate(x,y), 2)*math.pow(x-y, 2)/math.pow(state("CoRegSigma"), 3))
55+
56+
override def evaluate(x: Int, y: Int): Double = {
57+
1/(1 + math.pow(x-y, 2)/math.pow(state("coRegSigma"), 2))
58+
}
59+
}

0 commit comments

Comments
 (0)