[英]Optimizing Flink transformation
我有以下方法來計算DataSet
值的概率:
/**
* Compute the probabilities of each value on the given [[DataSet]]
*
* @param x single colum [[DataSet]]
* @return Sequence of probabilites for each value
*/
private[this] def probs(x: DataSet[Double]): Seq[Double] = {
val counts = x.groupBy(_.doubleValue)
.reduceGroup(_.size.toDouble)
.name("X Probs")
.collect
val total = counts.sum
counts.map(_ / total)
}
問題是,當我提交使用此方法的flink作業時,由於任務TimeOut
導致flink殺死了該作業。 我對只有40.000個實例和9個屬性的DataSet
上的每個屬性執行此方法。
有什么辦法可以使我的代碼更有效嗎?
經過幾次嘗試,我使其與mapPartition
一起mapPartition
,該方法是InformationTheory
類的一部分,該類進行一些計算以計算熵,互信息等。因此,例如, SymmetricalUncertainty
的計算方法如下:
/**
* Computes 'symmetrical uncertainty' (SU) - a symmetric mutual information measure.
*
* It is defined as SU(X, y) = 2 * (IG(X|Y) / (H(X) + H(Y)))
*
* @param xy [[DataSet]] with two features
* @return SU value
*/
def symmetricalUncertainty(xy: DataSet[(Double, Double)]): Double = {
val su = xy.mapPartitionWith {
case in ⇒
val x = in map (_._2)
val y = in map (_._1)
val mu = mutualInformation(x, y)
val Hx = entropy(x)
val Hy = entropy(y)
Some(2 * mu / (Hx + Hy))
}
su.collect.head.head
}
這樣,我可以有效地計算entropy
,互信息等。問題是,它僅在並行度為1的情況下工作,問題出在mapPartition
。
有什么方法可以與我在SymmetricalUncertainty
執行的操作類似,但是可以在任何並行度下進行操作嗎?
我終於做到了,不知道它是否是最好的解決方案,但是可以在n個並行級別上工作:
def symmetricalUncertainty(xy: DataSet[(Double, Double)]): Double = {
val su = xy.reduceGroup { in ⇒
val invec = in.toVector
val x = invec map (_._2)
val y = invec map (_._1)
val mu = mutualInformation(x, y)
val Hx = entropy(x)
val Hy = entropy(y)
2 * mu / (Hx + Hy)
}
su.collect.head
}
您可以在InformationTheory.scala中檢查整個代碼,並對其進行測試InformationTheorySpec.scala
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.