[Home]NRLIBJ

Robo Home | Changes | Preferences | AllPages

A library for creating NeuralNetworks.

Used by ScruchiPu.

You can download it from https://www.dcs.napier.ac.uk/~denaro/


I have for a while now assumed that nrlibj wants values between 0 and 1 for inputs and produces values between 0 and 1 for outputs. But reading some pages on the subject of NeuralNetworks? I see that often values between -1 and 1 are assumed. Is this the case for nrlibj as well? -- PEZ

No. NRLIBJ uses values between 0 and 1. But you can create new node class and use a function between -1 and 1. -- Albert

Just some code to save the network with DataStream? put it in the NNet and the Layer class : (use the same method name)

 Saving

    IN NNet

    /**
     *  Description of the Method
     *
     *@param  dos              Description of the Parameter
     *@exception  IOException  Description of the Exception
     */
    public void save(DataOutputStream? dos) throws IOException {
        dos.writeInt(this.lyr.length);
        for (int i = 0; i < lyr.length; i++) {
            this.lyr[i].saveLayer(dos);
        }
        for (int i = 0; i < lyr.length; i++) {
            this.lyr[i].saveLink(dos);
        }
        dos.flush();
    }

    Class Layer
       /**
     *  Description of the Method
     *
     *@param  dos              Description of the Parameter
     *@exception  IOException  Description of the Exception
     */
    void saveLayer(DataOutputStream? dos) throws IOException {
        int i;
        int x;
        int y;
        int n;
        int mbuff = -1;
        String SClass;
        if (this.mbuff != null) {
            mbuff = this.mbuff.nlyr;
        }
        if (this.node.length > 0) {
            SClass = this.node[0].getClass().getName();
        } else {
            SClass = "unknown";
        }
        dos.writeInt(this.nlyr);
        dos.writeInt(this.nx);
        dos.writeInt(this.ny);
        dos.writeUTF(SClass);
        dos.writeInt(mbuff);
    }

        /**
     *  Description of the Method
     *
     *@param  dos              Description of the Parameter
     *@exception  IOException  Description of the Exception
     */
    void saveLink(DataOutputStream? dos) throws IOException {
        int i;
        int n;
        for (n = 0; n < this.node.length; n++) {
            dos.writeInt(n);
            dos.writeInt(this.node[n].lnk.length - 1);
            dos.writeFloat(this.node[n].lnk[0].wgt);
            for (i = 1; i < this.node[n].lnk.length; i++) {
                dos.writeInt(this.node[n].lnk[i].nfrom.nlyr);
                dos.writeInt(this.node[n].lnk[i].nfrom.nn);
                dos.writeFloat(this.node[n].lnk[i].wgt);
            }
        }
    }

    LOADING

  In NNet

      public NNet(DataInputStream? dis) throws IOException
    {
    	this.loadNNet(dis);
    }

        /**
     *  Description of the Method
     *
     *@param  dis              Description of the Parameter
     *@exception  IOException  Description of the Exception
     */
    void loadNNet(DataInputStream? dis) throws IOException {
        int i;
        int nlayer;
        nlayer = dis.readInt();
        this.lyr = new Layer[nlayer];
        for (i = 0; i < this.lyr.length; i++) {
            this.lyr[i] = loadLayer(dis, this.lyr);
        }
        for (i = 0; i < this.lyr.length; i++) {
            this.lyr[i].loadLink(dis, this.lyr);
        }
    } 

      /**
     *  Description of the Method
     *
     *@param  dis              Description of the Parameter
     *@param  layer            Description of the Parameter
     *@return                  Description of the Return Value
     *@exception  IOException  Description of the Exception
     */
    Layer loadLayer(DataInputStream? dis, Layer layer[]) throws IOException {
        int nlyr;
        int nx;
        int ny;
        int lbuff;
        String Cnode;
        Layer lay;
        nlyr = dis.readInt();
        nx = dis.readInt();
        ny = dis.readInt();
        Cnode = dis.readUTF();
        lbuff = dis.readInt();
        if (ny > 0) {
            lay = new Layer(nlyr, nx, ny, Cnode);
        } else {
            lay = new Layer(nlyr, nx, Cnode);
        }
        if (lbuff >= 0) {
            lay.mbuff = layer[lbuff];
        }
        return lay;
    }

    In Layer

        /**
     *  Description of the Method
     *
     *@param  dis              Description of the Parameter
     *@param  layer            Description of the Parameter
     *@exception  IOException  Description of the Exception
     */
    void loadLink(DataInputStream? dis, Layer layer[]) throws IOException {
        int i;
        int l;
        int nn;
        int nlink;
        int lfrom;
        int nnfrom;
        float wgt;
        for (i = 0; i < this.node.length; i++) {
            nn = dis.readInt();
            nlink = dis.readInt();
            this.node[i].lnk = new Link[nlink + 1];
            this.node[i].lnk[0] = new Link(dis.readFloat(), null);
            for (l = 1; l < this.node[i].lnk.length; l++) {
                lfrom = dis.readInt();
                nnfrom = dis.readInt();
                wgt = dis.readFloat();
                this.node[i].lnk[l] = new Link(wgt, layer[lfrom].node[nnfrom]);
            }
        }
    }
 Synnalagma

Some other Node class Tanh is for output between -1 and 1 and Gaussian output 0 1:

public class NodeGaussian? extends NodeLin? {

	float x=1f;

	public NodeGaussian?(int nn,int nly,float b)
	{
		super(nn,nly,b);
	}

	protected float afn(float x)
	{
		this.x=x;
		return (float) Math.exp(-x*x);
	}
	protected float df(float y)
	{
		return -2f*x*y;
	}

}

public class NodeTanh? extends NodeLin? {

	public NodeTanh?(int nn,int nly,float b)
	{
		super(nn,nly,b);
	}

	protected float afn(float x)
	{
		return (float)  2f / (1f + (float)Math.exp(-2 * x)) - 1f;
	}
	protected float df(float y)
	{
		return 1-y*y;
	}
}

Synnalagma


Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited July 3, 2003 10:13 EST by 212.109.72.xxx (diff)
Search: