Esempio n. 1
0
 @Override
 public AVector asVector() {
   AVector v = Vector0.INSTANCE;
   for (INDArray a : slices) {
     v = v.join(a.asVector());
   }
   return v;
 }
Esempio n. 2
0
  /**
   * Pushes an input vector through the LSTM and gets an output. Cell state and recurrent result
   * value are updated.
   *
   * @param input The input vector to push through
   * @return The result vector
   */
  public AVector step(AVector input) {

    // concatenate result vector onto the end of input vector, dimension zero as it is 1-dimensional
    // AVector
    // System.out.println(input);
    // System.out.println(result);
    input = input.join(result);
    if (input.length() != weights[0].getShape(1)) {
      throw new RuntimeException(
          "Input was the wrong shape! Input ( + last out) was "
              + input.length()
              + "but weights were "
              + weights[0].getShape(1));
    }
    // System.out.println(input);
    // System.out.println(input.rows());
    /* There are 4 layers in LSTM, in order of sig(0) sig(1) sig(2) tanh(3) */
    // For each sigmoid layer, multiply its weight matrix by the input vector, add its bias vector,
    // and perform sigmoid operation on resultant vector
    for (int i = 0; i < 3; i++) {
      // System.out.println(weights[i].columns());
      // The "columns" of the weights 3d matrix should represent the two-dimensional matrices for
      // each of the activations.
      sigmoidMult1[i] = weights[i].innerProduct(input);
      // System.out.println(sigmoidMult1[i]);
      sigmoidMult1[i].add(biases[i]);
      // System.out.println(sigmoidMult1[i]);
      sigmoidLayers[i] = Operations.Sigmoid.operate(sigmoidMult1[i]);
      // System.out.println(sigmoidLayers[i]);

    }
    // Calculate tanh layer in same fashion as sigmoid layer, but with tanh activation function
    tanhLayer = weights[3].innerProduct(input);
    tanhLayer.add(biases[3]);
    tanhLayer = Operations.Tanh.operate(tanhLayer);

    // do the first element-wise multiplication: sigmoid layer 1 and the current cell state
    sigmoidLayers[0].multiply(cellState);
    // System.out.println("multiplication of forget and cell state: " + sigmoidLayers[0]);
    // do the second element-wise multiplication: sigmoid layer 2 and the tanh layer
    sigmoidLayers[1].multiply(tanhLayer);
    // System.out.println("multiplication of input and activation layer: " + sigmoidLayers[1]);
    sigmoidLayers[0].add(sigmoidLayers[1]);
    // System.out.println("addition of " + sigmoidLayers[1]);
    cellState = sigmoidLayers[0];
    sigmoidLayers[0] = null;
    sigmoidLayers[1] = null;

    AVector tanhOp = Operations.Tanh.operate(cellState.copy());
    sigmoidLayers[2].multiply(tanhOp);
    result = sigmoidLayers[2];
    sigmoidLayers[2] = null;

    return result.copy();
  }