public FieldVector<Complex> calculate(FieldVector<Complex> input) throws SeviException{ this.layerResults.clear(); LayerResult result = new LayerResult(); result.setInput(input); for(int i = 0; i < net.getLayers().size() -1; i++) { Layer l = net.getLayers().get(i); FieldMatrix<Complex> matrix = l.getMatrixtoNextLayer(); FieldVector<Complex> output = matrix.operate(result.getInput()); output = activationFunction(output, l); result.setOutput(output); this.layerResults.add(result); result = new LayerResult(); result.setInput(output); } return result.getInput(); }
@Test public void calcWeightDeltaTest() { double eta = 0.1; Complex[] d = {new Complex(0.1),new Complex(0.2)}; Complex[] in = {new Complex(0.1),new Complex(0.2), new Complex(0.3)}; FieldVector<Complex> delta = MatrixUtils.createFieldVector(d); FieldVector<Complex> input = MatrixUtils.createFieldVector(in); FieldMatrix<Complex> weightDiff = this.calcWeightDelta(delta,input,eta); assertEquals(2,weightDiff.getRowDimension()); assertEquals(3,weightDiff.getColumnDimension()); assertEquals(0.001,weightDiff.getEntry(0,0).getReal(),0.00001); assertEquals(0.002,weightDiff.getEntry(0,1).getReal(),0.00001); assertEquals(0.003,weightDiff.getEntry(0,2).getReal(),0.00001); assertEquals(0.002,weightDiff.getEntry(1,0).getReal(),0.00001); assertEquals(0.004,weightDiff.getEntry(1,1).getReal(),0.00001); assertEquals(0.006, weightDiff.getEntry(1, 2).getReal(), 0.00001); }
@Test public void layerpropagateTest() throws Exception { FeedForwardNet net = getNetWith1Weights(); SingleThreadPropagation prop = new SingleThreadPropagationTest(); prop.setNetWork(net); prop.setEta(10); SingleThreadCalculation calc = new SingleThreadCalculation(net); Complex[] in = {new Complex(1),new Complex(1)}; Complex[] tar = {new Complex(1)}; FieldVector<Complex> input = MatrixUtils.createFieldVector(in); FieldVector<Complex> target = MatrixUtils.createFieldVector(tar); FieldVector<Complex> output = calc.calculate(input); FieldVector<Complex> error = target.subtract(output); prop.propagate(error, calc.getLayerResults()); assertEquals(1.161527544, net.getLayers().get(1).getNeurons().get(0).getOutputs().get(0).getWeight().getReal(), 0.00001); assertEquals(1.01925455, net.getLayers().get(0).getNeurons().get(0).getOutputs().get(0).getWeight().getReal(), 0.00001); }
/** * Convert commons-math <code>FieldElement</code> to JAS * <code>RingElem</code> to. * @param <C> ring element type * @param v Commons-math vector of CMFieldElement objects * @return array of ring elements */ @SuppressWarnings("unchecked") public static <C extends RingElem<C>> C[] fromCMFieldVector(FieldVector<CMFieldElement<C>> v) { if (v == null) { return null; } C[] va = (C[]) new RingElem[v.getDimension()]; for (int i = 0; i < va.length; i++) { CMFieldElement<C> e = v.getEntry(i); if (e != null) { va[i] = e.val; } else { va[i] = null; } } return va; }
/** * Convert commons-math <code>FieldElement</code> to JAS * <code>RingElem</code> to. * * @param <C> ring element type * @param v commons-math vector of CMFieldElement objects * @return Java list of ring elements */ public static <C extends RingElem<C>> ArrayList<C> listFromCMFieldVector(FieldVector<CMFieldElement<C>> v) { if (v == null) { return null; } ArrayList<C> vv = new ArrayList<C>(v.getDimension()); for (int i = 0; i < v.getDimension(); i++) { CMFieldElement<C> e = v.getEntry(i); if (e != null) { vv.add(e.val); } else { vv.add(null); } } return vv; }
public static double[] transform(FieldVector<Complex> vector){ double[] ret = new double[vector.getDimension()]; for(int i = 0; i < ret.length; i++) { ret[i] = transform(vector.getEntry(i)); } return ret; }
public static FieldVector transform(double[] vector){ Complex[] ret = new Complex[vector.length]; for(int i = 0; i < ret.length; i++) { ret[i] = transform(vector[i]); } return MatrixUtils.createFieldVector(ret); }
protected FieldVector<Complex> activationFunction(FieldVector<Complex> vector, Layer layer){ FieldVector<Complex> ret = vector; for(int i = 0; i < vector.getDimension(); i++){ ret.setEntry(i,layer.getActivationFunction().calc(ret.getEntry(i))); } return ret; }
protected FieldVector<Complex> OneMinus(FieldVector<Complex> vector1){ FieldVector<Complex> res = vector1.copy(); for(int i = 0; i < vector1.getDimension(); i ++) { res.setEntry(i, one.subtract(vector1.getEntry(i))); } return res; }
private FieldVector<Complex> conjugate(FieldVector<Complex> vec){ FieldVector<Complex> ret = vec.copy(); for(int i=0; i < vec.getDimension(); i++){ ret.setEntry(i,vec.getEntry(i).conjugate()); } return ret; }
public void propagate(FieldVector<Complex> error, ArrayList<LayerResult> results) throws SeviException{ ArrayList<LayerPropagation> layerPropagations = calcLayerPropagation(error,results); for(LayerPropagation lp:layerPropagations){ lp.ajustWeights(this.eta); } }
protected ArrayList<LayerPropagation> calcLayerPropagation(FieldVector<Complex> error, ArrayList<LayerResult> results) throws SeviException { ArrayList<LayerPropagation> layerPropagations = new ArrayList<LayerPropagation>(); LayerPropagation lp = new LayerPropagation(error,net.getLayers().get(net.getLayers().size()-1), results.get(results.size()-1)); layerPropagations.add(lp); for(int i = results.size() -2; i >= 0 ; i--) { LayerResult result = results.get(i); lp = lp.getNext(result); layerPropagations.add(lp); } return layerPropagations; }
public void iteration(int count) throws SeviException{ int id; FieldVector<Complex> out, error; for(int i = 0; i < count; i++) { id = random.nextInt(dataset.getPairs().size()); ComplexPair pair = dataset.getPairs().get(id); FieldVector<Complex> verrauscht = addRauschen(pair.getInput()); out = calculation.calculate(verrauscht); error = pair.getOutput().subtract(out); this.setError(error); propagation.propagate(error, calculation.getLayerResults()); } }
private void setError(FieldVector<Complex> error){ double sum = 0.0; for(int i = 0; i < error.getDimension(); i++) { double temp = Helper.transform(error.getEntry(i)); sum += temp*temp; } this.sqrerror = sum; }
private FieldVector<Complex> addRauschen(FieldVector<Complex> vector){ FieldVector<Complex> ret = vector.copy(); for(int i = 0; i < vector.getDimension(); i++){ Complex temp = ret.getEntry(i).multiply(getRandom()); ret.setEntry(i,ret.getEntry(i).add(temp)); } return ret; }
@Test public void calculationTest() throws SeviException { FeedForwardNet net = new FeedForwardNet(); net.addLayer(2, ActivationFunction.Sigmoid); net.addLayer(2, ActivationFunction.Sigmoid); net.addLayer(1, ActivationFunction.Sigmoid); Neuron outputn = net.getLayers().get(2).getNeurons().get(0); outputn.getInputs().get(0).setWeight(new Complex(0.5)); outputn.getInputs().get(1).setWeight(new Complex(0.7)); Neuron input1 = net.getLayers().get(0).getNeurons().get(0); input1.getOutputs().get(0).setWeight(new Complex(1)); input1.getOutputs().get(1).setWeight(new Complex(0.9)); Neuron input2 = net.getLayers().get(0).getNeurons().get(1); input2.getOutputs().get(0).setWeight(new Complex(0.1)); input2.getOutputs().get(1).setWeight(new Complex(0.2)); Complex[] input = {new Complex(1), new Complex(2)}; FieldVector<Complex> in = MatrixUtils.createFieldVector(input); SingleThreadCalculation calc = new SingleThreadCalculation(net); FieldVector<Complex> out = calc.calculate(in); assertEquals(0.7685247, calc.getLayerResults().get(0).getOutput().getEntry(0).getReal(),0.00001); assertEquals(0.7858349, calc.getLayerResults().get(0).getOutput().getEntry(1).getReal(),0.00001); assertEquals(0.717956, out.getEntry(0).getReal(),0.00001); }
@Test public void calcDeltaTest() { Complex[] err = {new Complex(1), new Complex(2)}; Complex[] out = {new Complex(3), new Complex(4)}; Complex[] sh = {new Complex(-6), new Complex(-24)}; FieldVector<Complex> error = MatrixUtils.createFieldVector(err); FieldVector<Complex> output = MatrixUtils.createFieldVector(out); FieldVector<Complex> should = MatrixUtils.createFieldVector(sh); FieldVector<Complex> result = this.calcDelta(error, output); System.out.println(should.equals(result)); assertEquals(should,result); }
@Test public void calcErrorTest() { Complex[][] weights = {{new Complex(1), new Complex(2)},{new Complex(1), new Complex(2)}}; Complex[] delta = {new Complex(1), new Complex(2)}; Complex[] should = {new Complex(3), new Complex(6)}; FieldMatrix<Complex> w = MatrixUtils.createFieldMatrix(weights); FieldVector<Complex> d = MatrixUtils.createFieldVector(delta); FieldVector<Complex> s = MatrixUtils.createFieldVector(should); assertEquals(s,this.calcError(w,d)); }
private void testen(double[] testinput, ICalculationStrategy calculation) throws SeviException{ FieldVector<Complex> output = calculation.calculate(Helper.transform(testinput)); double[] out = Helper.transform(output); for(int i = 0; i < out.length; i++) { System.out.print(out[i]); } System.out.println(); }
/** * Solve a linear system: a x = b. * @param a matrix * @param b vector of right hand side * @return a solution vector x */ public GenVector<C> solve(GenMatrix<C> a, GenVector<C> b) { FieldMatrix<CMFieldElement<C>> am = CMFieldElementUtil.<C> toCMFieldMatrix(a); FieldVector<CMFieldElement<C>> bv = CMFieldElementUtil.<C> toCMFieldElementVector(b); final FieldLUDecomposition<CMFieldElement<C>> lu = new FieldLUDecomposition<CMFieldElement<C>>(am); FieldDecompositionSolver<CMFieldElement<C>> fds = lu.getSolver(); FieldVector<CMFieldElement<C>> xv = fds.solve(bv); GenVector<C> xa = CMFieldElementUtil.<C> vectorFromCMFieldVector(b.modul, xv); return xa; }
/** * Convert commons-math <code>FieldElement</code> to JAS * <code>RingElem</code> to. * * @param <C> ring element type * @param v commons-math FieldVector of CMFieldElement objects * @return JAS vector of ring elements */ public static <C extends RingElem<C>> GenVector<C> vectorFromCMFieldVector(GenVectorModul<C> fac, FieldVector<CMFieldElement<C>> v) { if (v == null) { return null; } List<C> list = listFromCMFieldVector(v); GenVector<C> vv = new GenVector<C>(fac, list); return vv; }
/** * Convert commons-math <code>FieldElement</code> to JAS * <code>RingElem</code> to. * @param <C> ring element type * @param v commons-math FieldMatrix of CMFieldElement objects * @return java.util.List of ring elements */ public static <C extends RingElem<C>> List<List<C>> listFromCMFieldMatrix(FieldMatrix<CMFieldElement<C>> v) { if (v == null) { return null; } ArrayList<List<C>> vv = new ArrayList<List<C>>(v.getRowDimension()); for (int i = 0; i < v.getRowDimension(); i++) { FieldVector<CMFieldElement<C>> e = v.getRowVector(i + 1); List<C> l = listFromCMFieldVector(e); vv.add(l); } return vv; }
public FieldVector<Complex> operate(final String qubits) { if (!valid_qubit_sequence(qubits)) throw new IllegalArgumentException("Provided qubit sequence is invalid"); return evaluate_circuit_matrix().operate(get_start_state(qubits)); }
private FieldVector<Complex> get_start_state(final String qubits) { FieldVector<Complex> state = new ArrayFieldVector<Complex>(ComplexField.getInstance(), (int) Math.pow(2, qubits.length())); state.setEntry(Integer.parseInt(qubits, 2), new Complex(1)); return state; }
private void add_item_simulate(final JMenu circuit_menu) { JMenuItem item_simulate = new JMenuItem(new AbstractAction("Simulate") { private static final long serialVersionUID = 8549028014281850661L; @Override public void actionPerformed(ActionEvent arg0) { try { Circuit circuit = new Circuit(gate_table.get_table()); FieldVector<Complex> results = circuit.operate(qubit_table.get_qubits()); StringBuilder text = new StringBuilder("<html>Simulation results:<table cellspacing=\"0\" cellpadding=\"0\">"); final int qubits_number = Integer.toBinaryString(results.getDimension() - 1).length(); for (int index = 0; index < results.getDimension(); index++) { final Complex current = results.getEntry(index); final double current_magnitude = current.abs(); if (!show_all_checkbox.isSelected() && Tools.equal(current_magnitude, 0)) continue; double current_percentage = Math.pow(current_magnitude, 2) * 100; current_percentage = Tools.round(current_percentage); StringBuilder qubits_values = new StringBuilder(Integer.toBinaryString(index)); for (int length = qubits_values.length(); length < qubits_number; length++) qubits_values.insert(0, '0'); text.append("<tr><td>" + Tools.round(current.getReal()) + (current.getImaginary() < 0 ? "" : "+") + Tools.round(current.getImaginary()) + "i |" + qubits_values + ">  </td><td>" + current_percentage + "% chance</td></tr>"); } text.append("</table></html>"); result_text_pane.setText(text.toString()); } catch (RuntimeException ex) { Tools.error(frame, "A runtime exception has been caught:\n" + ex.getMessage()); ex.printStackTrace(); } } }); item_simulate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0)); circuit_menu.add(item_simulate); }
public ComplexPair transform() { FieldVector<Complex> input = Helper.transform(this.input); FieldVector<Complex> output = Helper.transform(this.output); return new ComplexPair(input,output); }
public ComplexPair(FieldVector<Complex> input, FieldVector<Complex> output){ setInput(input); setOutput(output); }
public FieldVector<Complex> getInput() { return input; }
public void setInput(FieldVector<Complex> input) { this.input = input; }
public FieldVector<Complex> getOutput() { return output; }
public void setOutput(FieldVector<Complex> output) { this.output = output; }
public LayerResult(FieldVector<Complex> input, FieldVector<Complex> output) { this.input = input; this.output = output; }
public LayerPropagation(FieldVector<Complex> error, Layer layer, LayerResult result) throws SeviException{ this.error = error; this.result = result; this.layer = layer; this.calcDeltas(); }
public LayerPropagation(Layer layer, LayerResult result,FieldVector<Complex> lastdelta) throws SeviException{ this.lastdelta = lastdelta; this.result = result; this.layer = layer; this.calcDeltas(); }
protected FieldMatrix<Complex> calcWeightDelta(FieldVector<Complex> delta, FieldVector<Complex> input, double eta) { return delta.outerProduct(input).scalarMultiply(new Complex(eta)); }
protected FieldVector<Complex> calcError(FieldMatrix<Complex> weights, FieldVector<Complex> lastdelta){ FieldMatrix<Complex> transposed = weights.transpose(); return transposed.operate(lastdelta); }