简体   繁体   English

将八度功能转换为Java

[英]Convert Octave Function to Java

I'm trying to convert some Octave functions to Java, but I'm not sure I'm this right. 我正在尝试将某些Octave函数转换为Java,但是不确定我是否正确。

function [y,a] = forwardProp(x, Thetas)
    a{1} = x;
    L = length(Thetas)+1;
        for i = 2:L,
            a{i-1} =[1; a{i-1}];
            z{i} =Thetas{i-1}*a{i-1};
            a{i} =sigmoid(z{i});
        end 
    y = a{L};
end

My Java Function 我的Java函数

public class ForwardProp {

public static DoubleMatrix ForwardProp(DoubleMatrix x, DoubleMatrix Thetas) 
{
    DoubleMatrix a = new DoubleMatrix();
    a = DoubleMatrix.concatHorizontally(DoubleMatrix.ones(a.rows, 1), x);

    int L = Thetas.length + 1;

    DoubleMatrix z = new DoubleMatrix();

    for (int i = 2; i <= L; i++) 
    {
        a.put(i - 1, a.get(i - 1));
        z.put(i, (Thetas.get(-1) * a.get(i - 1)));
        a.put(i, Sigmoid(z.get(i)));
    }
    return a;
    }
}

Can someone tell me if this is right??? 有人可以告诉我这是否正确吗???

As far as I can tell, you are committing a double-fencepost error here: 据我所知,您在此处犯了双栅栏错误:

int L = Thetas.length + 1;

L now equals 1 more than the number of elements in the matrix... L现在比矩阵中元素的数量大1 ...

for (int i = 2; i <= L; i++) ...and you are now looping with Thetas.get(i - 1) all the way up to an index that is 2 greater than the highest index available from Thetas.get(int) . for (int i = 2; i <= L; i++) ...并且您现在一直在使用Thetas.get(i - 1)循环, Thetas.get(i - 1)索引比Thetas.get(int)的最高索引大2 Thetas.get(int)

Remember, Thetas.get(int) directly accesses the internal array that stores this matrix's data. 请记住, Thetas.get(int)直接访问存储此矩阵数据的内部数组。 This can only accept indices from 0 to Thetas.length - 1 . 这只能接受从0到Thetas.length - 1索引。 So when you call Thetas.get(-1) you will always get an error because -1 is not a valid array index; 因此,当您调用Thetas.get(-1)时,总是会得到一个错误,因为-1不是有效的数组索引。 when you get to the end of the loop and call Thetas.get(i - 1) , you will get an error again because there is no element at that location. 当您到达循环末尾并调用Thetas.get(i - 1) ,将再次出现错误,因为该位置没有元素。

You are also initializing your output matrix with DoubleMatrix z = new DoubleMatrix(); 您还将使用DoubleMatrix z = new DoubleMatrix();初始化输出矩阵DoubleMatrix z = new DoubleMatrix(); , which returns a 0x0 empty matrix with no elements. ,它返回没有元素的0x0空矩阵。 That's not what you want either. 那也不是你想要的。

Try to make sure you know which indices your data is in, then rewrite it when you know how to reference the data you are using. 尝试确保您知道数据在哪个索引中,然后在知道如何引用正在使用的数据时将其重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM