Java脚本:分配运算符不适用于矩阵

时间:2018-07-28 07:48:32

标签: javascript arrays matrix

我正在尝试使用随机变量填充我声明的矩阵,但是出于某些明确的原因,并未将随机值分配给Matrix中的单元格。 这是所述矩阵的声明:

this.weights_ih= new Matrix(this.hidden_nodes,this.input_nodes);

请记住,此声明是类的构造函数的一部分,如下所示:

    class NeuralNetwork
    {
        constructor (input_nodes,hidden_nodes,output_nodes)
        {
            this.input_nodes=input_nodes;
            this.output_nodes=output_nodes;
            this.hidden_nodes=hidden_nodes;
            this.weights_ih= new Matrix(this.hidden_nodes,this.input_nodes);
            this.weights_ho= new Matrix(this.output_nodes,this.hidden_nodes);
            this.weights_ih.randomize();
            this.weights_ho.randomize();
            this.bias_h= new Matrix(this.hidden_nodes,1);
            this.bias_0= new Matrix(this.output_nodes,1);
            this.bias_h.randomize();
            this.bias_0.randomize();
            this.learning_rate=0.1;

        }
  }

randomize()函数返回的值介于1到-1之间,定义如下:

randomize()
    {
        for(let i=0;i<this.rows;i++)
        {

            for(let j=0;j<this.cols;j++)
            {
                this.data[i][j]=Math.floor(Math.random()*2 -1);
            }
        }

    } 

我已确保randomize()可以正常工作,就像我控制台记录变量this.data[i][j]一样。这是该屏幕截图: OutPut 因此,我假设不应该对randomize()负责,因为它会按预期生成随机值,并且已将数据分配给this.data[i][j]。 但是当我列出矩阵this.weights_ih时。这是我收到的输出: Matrix 我真的不明白为什么未将所有randomize()值中的单元格都定义为变量this.data[i][j]的原因。 让我知道我是否犯了一个错误。 如果可以通过任何方式对您有所帮助,这是Matrix类的定义:

class Matrix
{
    constructor (rows,cols)
    {
        this.rows=rows;
        this.cols=cols;
        this.data=[];
        for(let i=0;i<this.rows;i++)
        {
            this.data[i]=[];
            for(let j=0;j<this.cols;j++)
            {
                let temp=this.data[i];
                temp[j]=0;
                //this.data[i][j]=0;
            }
        }
    }
    static transpose(matrix)
    {
        let result = new Matrix(matrix.cols,matrix.rows)

        for (let i=0;i<matrix.rows;i++)
        {
            for(let j=0;j<matrix.cols;j++)
            {
                result.data[j][i]=matrix.data[i][j];
            }
        }
        return result;

    }
    toArray()
    {
        let arr=[];
        for(let i=0;i<this.rows;i++)
        {

            for(let j=0;j<this.cols;j++)
            {
                arr.push(this.data[i][j]);
            }

        }
        return arr;

    }
    static fromArray(arr)
    {
        let m= new Matrix(arr.length,1);
        let v= arr.length;
        for(let i=0;i<v;i++)
        {
            m.data[i][0]=arr[i];
        }
        return m;
    }
    randomize()
    {
        console.log("Rows:"+this.rows+"cols"+this.cols);
        for(let i=0;i<this.rows;i++)
        {

            for(let j=0;j<this.cols;j++)
            {
                this.data[i][j]=Math.floor(Math.random()*2 -1);
            }
        }

    }
    static subtract(a,b)
    {
        let result= new Matrix(a.rows,a.cols);
        for( let i=0;i<result.rows;i++)
            {
                for(let j=0;j<result.cols;j++)
                {
                    result.data[i][j]=a.data[i][j]-b.data[i][j];
                }
            }
        return result;
    }
    add(n) 
    {
        if(n instanceof Matrix)
        {
            for( let i=0;i<this.rows;i++)
            {
                for(let j=0;j<this.cols;j++)
                {
                    this.data[i][j]+=n.data[i][j];
                }
            }
        }
        else
        {
            for( let i=0;i<this.rows;i++)
            {
                for(let j=0;j<this.cols;j++)
                {
                    this.data[i][j]+=n;
                }
            }
        }
    }
    static multiply(a,b)
    {
        if(a.cols!==b.rows)
            {
                console.log("Error 001");
                return undefined;
            }
            let result = new Matrix(a.rows,b.cols);
            let sum=0;

            for(let i=0;i<result.rows;i++)
            {
                for(let j=0;j<result.cols;j++)
                {
                    sum=0;
                    for(let k=0;k<a.cols;k++)
                    {
                        sum+=a.data[i][k]*b.data[k][j];
                    }
                    result.data[i][j]=sum;
                }

            }
            return result;
    }
    multiply(n)
    {
        for( let i=0;i<this.rows;i++)
        {
            for(let j=0;j<this.cols;j++)
            {
                this.data[i][j]*=n;
            }
        }
    }
    map(func)
    {
        //Apply a fucntion to every element 
        for( let i=0;i<this.rows;i++)
        {
            for(let j=0;j<this.cols;j++)
            {
                let val = this.data[i][j];
                this.data[i][j]=func(val);
            }
        }
    }
    static map(matrix,func)
    {
        let result= new Matrix(matrix.rows, matrix.cols);
        for(let i=0;i<matrix.rows;i++)
        {
            for(let j=0;j<matrix.cols;j++)
            {
                let val= matrix.data[i][j];
                result.data[i][j]=func(val);
            }
        }
        return result;
    }
    print()
    {
        console.table(this.data);
    }
}

神经网络声明:

function setup()
{
    let nn= new NeuralNetwork(2,2,2);
    let input=[1,0];
    let targets=[1,1];
    nn.train(input,targets);
}

预先感谢!

0 个答案:

没有答案