2017-04-13 13 views
0

저는 R과 MATLAB을 사용하기 전에 비교적 새로운 스칼라를 사용했습니다. 나는 스칼라에 다음 코드를 작성했다. 나는 R과 MATLAB에서 같은 코드를 작성했으며 두 가지 모두 잘 동작한다. 그러나 스칼라에 대한 나의 경험 부족으로 아래의 코드는 작동하지 않는다. 다른 함수에서 참조 함수를 사용한 스칼라

import breeze.linalg._ 
import breeze.numerics.exp 
import scala.math.log 



val data = breeze.stats.distributions.Uniform(0, 1) 

val ep: DenseMatrix[Double] = DenseMatrix.rand(39, 3, data).t 

val a = DenseVector(1.0) 

val out: DenseMatrix[Double] = tile(a, 1, 39) 

val fout: DenseVector[Double] = out.toDenseVector 

val A: Double = 0.0 

val B: Double = 1.0 

val eta: Double = 2.0/Math.pow(B - A, 2.0) 

val nCol: Int = 39 

val nRow: Int = 3 

var gA = 0.0 

var gB = 0.0 

var gamma = 0.0 


def SubstFunction(predictions: DenseVector[Double], expertsPrediction: DenseVector[Double]): Double = { 

    gA = -(1/eta) * log(predictions dot exp(-eta * (expertsPrediction :- A)) :^ 2.0) 

    gB = -(1/eta) * log(predictions dot exp(-eta * (expertsPrediction :- B)) :^ 2.0) 

    gamma = (0.5 * (B + A)) - ((gB - gA)/2 * (B - A)) 

    gamma 

} 


def prediction(Input: DenseMatrix[Double], outcomes: DenseVector[Double]): DenseVector[Double] = { 

    var weights = DenseVector(1.0,1.0,1.0) 

    val AAprediction = DenseVector.fill(nCol)(0.0) 

    //DenseVector.ones[Double](nCol).t 

    for (l<-0 to Input.cols) { 

    val normalisedWeights = weights/sum(weights) 

    AAprediction(l) = SubstFunction(normalisedWeights, Input(::,l)) 

    weights = normalisedWeights :* exp(eta :* (Input(::,l) :- outcomes(l)) :^ 2.0).toDenseVector 
    } 
    AAprediction: DenseVector[Double] 
} 

prediction(ep,fout) 

나는 예측 그것에 sbstFunction를 호출 할 때 문제가있을 것 같아요. IntelliJ에서 스칼라 워크 시트를 사용하고 있습니다. 나는이 코드를 실행하면 나는 오류를 얻을하지만 난 숫자 출력을하지 않는 대신에 내가 얻을 :

<function1> res1: Unit =() 

업데이트 :

Column must be in bounds for slice!

: 나는 코드를 수정하고 지금은 다음과 같은 오류를 얻고있다

누군가 내가 잘못하고있는 것을 이해하도록 도와 줄 수 있습니까?

답변

0

스칼라의 기본 기능은 다음과 같이 정의된다 : 당신이 입력 유형 Int, 반환 형식 Int 및 함수 본문을 정의

def f(x:Int):Int = { 
    x 
} 

.

당신이합니다 (prediction 방법에 대한)입니다 무엇을 가지고 :

def f(x:Int) = (y:Int) => y + x 

f 반환 다른 기능 Int => Int. 귀하의 경우 prediction 함수는 아무 것도 실행되지 않는 이유 인 DenseVector[Double] => DenseVector[Double] 함수를 반환합니다.

+0

이렇게하면 코드에 몇 가지 문제가 발생합니다. 이는 내 코드가 올바르지 않다는 것을 의미합니다. 도와 주셔서 감사합니다. – Jamil

1
def prediction(Input: DenseMatrix[Double], outcomes: DenseMatrix[Double]) = 
    (AAprediction: DenseVector[Double]) => { 

이것은 prediction의 선언입니다. 이 함수는 두 개의 인수를 사용하여 함수를 반환하는 메서드입니다. 이 DenseVector[Double] => DenseVector[Double]를 입력 한 기능처럼 신속하게 코드를 찾고 보이는,보다 정확한 선언은 다음과 같습니다 본질적으로 그것이 무엇 prediction(ep,out)에서 일을하는 함수를 건설하고

def prediction(Input: DenseMatrix[Double], 
    outcomes: DenseMatrix[Double]): DenseVector[Double] => DenseVector[Double] 

. 훨씬 간단한 예제 :

scala> def addConst(x:Int):Int => Int = y => x + y 
addConst: (x: Int)Int => Int 

scala> addConst(10) 
res1: Int => Int = <function1> 

이 경우 다시 함수를 작성했습니다. 이 기능을 사용하려면 우리는 당신이 틀리거나 오해했던 부분을 단순화,하지만 난 그게 무엇이 잘못되었는지 이해하기에 충분한 것으로 판단 res1(5) 또는

scala> addConst(10)(5) 
res2: Int = 15 
+0

문제는 AAprediction이 루프 내부에 채워지지 않거나 출력이 없다는 것입니다. – Jamil

0

부를 수 중 당신은 그것을 해결하는 방법을 알 수 있습니다.

당신은 다음과 같습니다 예측 기능 정의 :

def prediction(addTo: Int, multiplyBy: Int) = (number: Int) => { (number + addTo) * multiplyBy }

을 그리고 당신은이 개 필요한 매개 변수를 사용하고,이 변수를 대체 할 해주기 경우, 그 결과는 다음과 같습니다 prediction(2, 3)

number => (number + 2) * 3 < -이 기능은 그렇지 않습니까?

최종 값을 계산하려면 anonymous function 안에있는 3 번째 인수를 사용해야합니다.

그래서 우리의 예에서 prediction(2, 3)(1)은 우리에게 실제 출력 9 을 줄 것이다 또는 당신은 def addTwoMultiplyByThree = prediction(2, 3) 를 정의하고 여러 다른 값으로 사용할 수 있습니다.

+0

이것은'(number + 2) * 3' – pedrofurla

+0

'number => (number + 2) * 3' 표현식입니다. 이것은 함수입니다. – pedrofurla