Exemplo n.º 1
0
  public static RealMatrix stochasticSubmatrix(RealMatrix data, int batch_size, Random rng) {
    // assume all data has the size number_samples by number_features
    int num_samples = data.getRowDimension();
    int num_features = data.getColumnDimension();
    int batch_num = num_samples / batch_size + 1;

    // randomly generate a batch index
    int batch_index = rng.nextInt(batch_num);
    List<Integer> rowIndex_tmp = new ArrayList<Integer>();

    for (int i = 0; i < batch_size; i++) {
      if (batch_size * batch_index + i >= num_samples) {
        break;
      } else {
        rowIndex_tmp.add(batch_size * batch_index + i);
      }
    }
    int[] rowIndex = TypeConvert.ArrayTointv(rowIndex_tmp);

    // System.out.println(rowIndex_tmp);
    int[] columnIndex = new int[num_features];
    for (int j = 0; j < num_features; j++) {
      columnIndex[j] = j;
    }

    // System.out.println(batch_index);

    // return null;
    return data.getSubMatrix(rowIndex, columnIndex);
  }
  protected void writeEventStream(Payload payload) throws IOException {
    PrintStream printStream = new PrintStream(response.outputStream());

    try (Stream<?> stream = (Stream<?>) payload.rawContent()) {
      stream.forEach(
          item -> {
            String jsonOrPlainString =
                (item instanceof String) ? (String) item : TypeConvert.toJson(item);

            printStream
                .append("data: ")
                .append(jsonOrPlainString.replaceAll("[\n]", "\ndata: "))
                .append("\n\n")
                .flush();
          });
    }
  }