FullyConnectedLayer extends AbstractLayer { @Override public void applyWeightChanges() { if (batchMode) { deltaWeights.div(batchSize); Tensor.div(deltaBiases, batchSize); } Tensor.copy(deltaWeights, prevDeltaWeights); Tensor.copy(deltaBiases, prevDeltaBiases); weights.add(deltaWeights); Tensor.add(biases, deltaBiases); if (batchMode) { deltaWeights.fill(0); Tensor.fill(deltaBiases, 0); } } FullyConnectedLayer(int width);  FullyConnectedLayer(int width, ActivationType activationFunction); @Override void init(); @Override void forward(); @Override void backward(); @Override void applyWeightChanges();  }
@Test public void testApplyWeightChanges() { RandomGenerator.getDefault().initSeed(123); FullyConnectedLayer prevLayer = new FullyConnectedLayer(5); FullyConnectedLayer instance = new FullyConnectedLayer(10); instance.setPrevLayer(prevLayer); instance.init(); Tensor weights = new Tensor(5, 10); WeightsInit.initSeed(123); Tensor biases = new Tensor(10); WeightsInit.uniform(weights.getValues(), 5); WeightsInit.randomize(biases.getValues()); instance.setWeights(weights); instance.setBiases(biases.getValues()); instance.deltaBiases = new float[10]; instance.deltaWeights = new Tensor(5, 10); WeightsInit.randomize(instance.deltaWeights.getValues()); WeightsInit.randomize(instance.deltaBiases); instance.applyWeightChanges(); Tensor expectedWeights = new Tensor(-0.22512805f, -0.57834274f, 0.8858789f, -0.27705812f, -0.28378475f, 0.50557935f, 0.29112953f, -0.29929897f, 0.44997644f, 0.4380083f, 0.46456295f, 0.20707384f, 0.26794374f, -0.7713099f, -0.49491742f, 0.4379894f, -0.045887947f, -0.29116237f, -0.4028719f, -0.5448313f, -0.10340464f, 0.20406264f, 0.016387641f, -0.35627222f, 0.6651356f, 0.28818867f, 0.05292958f, -0.04253599f, -0.059095383f, -0.5151812f, -0.35183465f, 0.14134777f, 0.026516795f, 0.5094531f, 0.063139975f, 0.011747062f, -0.12965626f, -0.77672803f, 0.1024687f, 0.23364827f, -0.2879062f, 0.36031187f, 0.15492517f, 0.20544726f, 0.011152849f, -0.0014150143f, -0.19911501f, -0.64997375f, 0.40381932f, -0.3714426f); Tensor expectedBiases = new Tensor(10); expectedBiases.setValues(-0.30327034f, -0.23136496f, -0.1402145f, -0.65223693f, -0.27131938f, -0.10264724f, -0.09167038f, 0.0578903f, -0.20470357f, 0.63714188f); assertArrayEquals(weights.getValues(), expectedWeights.getValues(), 1e-7f); assertArrayEquals(biases.getValues(), expectedBiases.getValues(), 1e-7f); }