package ml; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Random; import shared.Shared; import shared.Tools; import shared.Vector; import structures.ByteBuilder; import structures.FloatList; import structures.IntList; public class CellNet implements Cloneable, Comparable { /*--------------------------------------------------------------*/ /*---------------- Initialization ----------------*/ /*--------------------------------------------------------------*/ public CellNet() {//Dummy network seed=0; dims=null; density=1; density1=0; edgeBlockSize=1; layers=0; values=null; weightsIn=weightsOut=null; eOverNet=null; list=null; net=null; finalLayer=null; transposeCounter=null; layerStart=null; } public CellNet(int[] dims_, long seed_, float density_, float density1_, int edgeBlockSize_, ArrayList commands_) { seed=(seed_>=0 ? seed_ : new Random().nextLong()&Long.MAX_VALUE); commands=commands_; dims=dims_.clone(); density=density_; density1=density1_; edgeBlockSize=edgeBlockSize_; layers=dims.length; values=makeFloatMatrix(dims); eOverNet=makeFloatMatrix(dims); int cells=(int) shared.Vector.sum(dims); list=new ArrayList(cells+1); transposeCounter=new int[cells+1]; net=makeNodes(dims, list, values, eOverNet); finalLayer=net[layers-1]; layerStart=new int[dims.length]; layerStart[0]=1; for(int i=1; i0)) : Arrays.toString(Function.TYPE_RATES); if(Function.TYPE_RATES_CUM!=null || Cell.randomTypeRate>0) { Random randy=Shared.threadLocalRandom(seed); if(Function.TYPE_RATES_CUM!=null) { randomizeActivationA(net, list, randy, Function.TYPE_RATES_CUM); }else{ randomizeActivationB(net, list, randy, Cell.randomTypeRate); } } } static void randomizeActivationA(Cell[][] net, ArrayList list, Random randy, float[] cumRate) { assert(cumRate!=null && cumRate[cumRate.length-1]==1) : Arrays.toString(cumRate); for(int layerNum=1; layerNum list, Random randy, float rate) { assert(rate>0); // final int types=Cell.TYPES.length; final int types=Tools.min(Cell.MAX_TYPE+1, Function.TYPES.length); // assert(false) : types; // System.err.println("types="+types); for(int layerNum=1; layerNum list, float[][] values, float[][] eOverNext){ final Cell[][] net=new Cell[dims.length][]; assert(list.isEmpty()); list.add(null); // int prevWidth=-1, width=dims[0], nextWidth; int[] layerStart=new int[dims.length]; for(int i=0, sum=1; i=dims.length-1 ? -1 : layerStart[layerNum+1]); final int width=dims[layerNum]; final int type=(layerNum=layers+1 ? 1f : (layer==1 && density1>0) ? density1 : density; } //Fully-connected, but weight 0 edges are not used private static long makeEdgesDense(Cell[][] net, Random randy, float density, float density1, int edgeBlockSize){ long numEdges=0; for(int layerNum=1; layerNum=0; i=bs.nextSetBit(i+1)) { weights[i]=randomWeight(randy); numEdges++; } } // assert(false) : Arrays.toString(c.weights); } } return numEdges; } private static long makeEdgesSparse(Cell[][] net, Random randy, float density, float density1, int edgeBlockSize){ long numEdges=0; for(int layerNum=1; layerNum=0; i=bs.nextSetBit(i+1), j++) { array[j]=i; } return array; } static void makeOutputSets(Cell[][] net) { final int cells; { Cell[] lastLayer=net[net.length-1]; Cell c=lastLayer[lastLayer.length-1]; cells=c.id(); } IntList[] map=new IntList[cells+1]; for(int lnum=0; lnum0; lnum--) { final float[] valuesIn=values[lnum-1]; final float[] eOverNetNext=eOverNet[lnum+1]; Cell[] layer=net[lnum]; float[][] weightsOutLnum=weightsOut[lnum]; for(int i=0; i0; lnum--) { final float[] valuesIn=values[lnum-1]; final float[] eOverNetNext=eOverNet[lnum+1]; Cell[] layer=net[lnum]; float[][] weightsOutLnum=weightsOut[lnum]; if(SPECIAL_FMA) {Vector.backPropFma(layer, eOverNetNext, weightsOutLnum);} for(int i=0; i0.0001f) { if(NORMALIZE_BIAS) {normalizeBiasDense();} if(NORMALIZE_WEIGHTS) {normalizeWeightsDense();} normalization_factor*=normalization_shrink_rate; } } /** * Normalizes the biases of the dense layers in the network. */ public void normalizeBiasDense() { FloatList list=new FloatList(); for(int lnum=layers-2; lnum>0; lnum--) { list.clear(); Cell[] layer=net[lnum]; for(int i=0; i0 && density1!=density) {bb.append("#density1 ").append(density1, 8, true).nl();} bb.append("#blocksize ").append(edgeBlockSize).nl(); bb.append("#seed ").append(seed).nl(); // if(annealSeed!=seed) {bb.append("#annealseed ").append(annealSeed).nl();} bb.append("#layers ").append(layers).nl(); if(epochsTrained>0) {bb.append("#epochs ").append(epochsTrained).nl();} if(samplesTrained>0) {bb.append("#samples ").append(samplesTrained).nl();} bb.append("#dims"); for(int d : dims) {bb.space().append(d);} bb.nl(); for(String s : commands) {bb.append(s).nl();} if(lastStats!=null) {bb.append("##stats ").append(lastStats).nl();} bb.append("##fpr ").append(fpRate, 6).nl(); bb.append("##fnr ").append(fnRate, 6).nl(); bb.append("##err ").append(errorRate, 6).nl(); bb.append("##wer ").append(weightedErrorRate, 6).nl(); bb.append("##ctf ").append(cutoff, 6).nl(); return bb; } public ByteBuilder toBytes() { ByteBuilder bb=header(); bb.append("#edges ").append(countEdges()).nl(); lastLinesWritten=18; long edgeCount=0; assert(CONCISE); for(int lnum=1; lnum>4)&15))); } return bb; } static int[] fromHex(byte[] line, int start) { byte[] bytes=new byte[(line.length-start)/2]; for(int i=start, j=0; i list2=copy.list; for(int i=1; i list2=cn.list; for(int i=1; i list; final float[][] values; final float[][] eOverNet; /** [layer][lpos][input] */ float[][][] weightsIn; float[][][] weightsOut; int[][][] edgesIn; int[][][] edgesOut; final int[] transposeCounter;//?? final int[] layerStart;//?? /** * List of command lines used to produce this network. * As long as the same version is being used this can replicate a network. */ public ArrayList commands; long lastLinesWritten=0; /*--------------------------------------------------------------*/ public static final boolean SPECIAL_FMA=true;//~20% faster when true, but needs ml.Cell class public static boolean NORMALIZE_BIAS=false; public static boolean NORMALIZE_WEIGHTS=false; public static float normalization_factor=0.5f; public static float normalization_shrink_rate=0.999f; public static boolean CONCISE=true; public static boolean DENSE=true; public static boolean OUT_HEX=false; public static boolean OUT_DENSE=false; public static boolean OUT_SPARSE=false; public static boolean verbose=false; public static final int version=1; public static float PROB_FLAT=0.3f; public static float PROB_EXP=0.4f; public static float EXP_LAMDA=5f; public static float RAND_WEIGHT_CAP=2.0f; static int compareCode=0; final static int compareWER=0, compareERR=1, compareFNR=2, compareFPR=3; /*--------------------------------------------------------------*/ }