How to use the numjs.array function in numjs

To help you get started, we’ve selected a few numjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github grimmer0125 / alphago-zero-tictactoe-js / src / tictactoe / TicTacToeGame.js View on Github external
getNextState(boardNdArray, player, action) {
    // # if player takes action on board, return next (board,player)
    // # action must be a valid move
    if (action === this.n * this.n) {
      // return (board, -player)
      console.log('invalid action');
      return { boardNdArray, player: -player };
    }

    const b = new Board(this.n);
    // b.pieces = np.copy(board), Python
    b.pieces = boardNdArray.tolist();

    const move = { x: Math.floor(action / this.n), y: (action % this.n) };
    b.execute_move(move, player);
    return { boardNdArray: nj.array(b.pieces), curPlayer: -player };
  }
github eimg / hello-nn-js / hello.dnn.gd.js View on Github external
}
    }

    return result;
}

/* === Training === */
train(inputs, test_result, 60000);

/* === Testing === */
var test_data = [
    [1, 0, 0],
    [1, 1, 0]
];

console.log( think( nj.array(test_data) ) );
github eimg / burmese-text-classifier / train.js View on Github external
function train(X, y, hidden_neurons, alpha, epochs, dropout, dropout_percent) {
    var start_time = new Date();

    var X_arr = X.tolist();

    console.log("training with " + hidden_neurons + " neurons, alpha: " + alpha);
    console.log("input matrix: " + X_arr.length + "x" + X_arr[0].length);
    console.log("output matrix: 1x" + classes.length);
    console.log('------');

    var last_mean_error = 1;

    var synapse_0 = nj.array( rand(X_arr[0].length, hidden_neurons) );
    var synapse_1 = nj.array( rand(hidden_neurons, classes.length) );

    var prev_synapse_0_weight_update = nj.zeros(synapse_0.shape);
    var prev_synapse_1_weight_update = nj.zeros(synapse_1.shape);

    var synapse_0_direction_count = nj.zeros(synapse_0.shape);
    var synapse_1_direction_count = nj.zeros(synapse_1.shape);

    for(var j = 0; j < epochs + 1; j++) {

        var layer_0 = X;
        var layer_1 = nj.sigmoid(nj.dot(layer_0, synapse_0));

        if(dropout) {
            // I don't understand what this does yet
            // layer_1 *= nj.random.binomial([np.ones((len(X),hidden_neurons))], 1-dropout_percent)[0] * (1.0/(1-dropout_percent));
        }
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
function update(x, s, y) {
        // y is an optional vector parameter, used for conditional mode only.

        var x_ = nj.array([x[0] / scale_factor, x[1] / scale_factor, x[2], x[3], x[4]]);

        var lstm_input, rnn_state;

        if (y) {
            var z = nj.array(y);
            lstm_input = nj.concatenate([x_, z]);
        } else {
            lstm_input = x_;
        }

        rnn_state = dec_lstm.forward(lstm_input, s[0], s[1]);

        return rnn_state;
    };
github eimg / burmese-text-classifier / train.js View on Github external
var arr = matrix.tolist();
    var nx = arr.length;
    var ny = arr[0].length;

    // Loop over all cells
    for (var i = 0; i < nx; ++i) {
        for (var j = 0; j < ny; ++j) {
            if( arr[i][j] > 0 ) {
                arr[i][j] = 1;
            } else {
                arr[i][j] = 0;
            }
        }
    }

    return nj.array(arr);
}
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
info = model_raw_data[0];
        dimensions = model_raw_data[1];
        num_blobs = dimensions.length;
        var weightsIn = model_raw_data[2];
        weights = Array(weightsIn.length)
        max_weight = 10.0;
        N_mixture = 20;

        max_seq_len = info.max_seq_len;

        pixel_factor = 2.0; // maybe should be 1.0 for non-retina screens.
        scale_factor = info.scale_factor / pixel_factor; // divide by 2 for macbooks?

        for (i = 0; i < num_blobs; i++) {
            weights[i] = nj.array(new Float32Array(string_to_array(weightsIn[i])), 'float32');
            weights[i] = weights[i].divide(32767);
            weights[i] = weights[i].multiply(max_weight);
            if (dimensions[i].length == 2) {
                var d = dimensions[i];
                var d1 = d[0],
                    d2 = d[1];
                weights[i] = weights[i].reshape(d1, d2);
            }
        }

        if (info.mode === 2 || info.mode === "gen") { // 0 or 1 - vae, 2 - gen
            dec_output_w = weights[0];
            dec_output_b = weights[1];
            dec_lstm_W_xh = weights[2];
            dec_lstm_W_hh = weights[3];
            dec_lstm_bias = weights[4];
github eimg / hello-nn-js / hello.dnn.js View on Github external
function curve(nums) {
    nums = nums.tolist();
    var result = [];
    for(var i = 0; i < nums.length; i++) {
        result[i] = [];
        for(var ii=0; ii
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
var softmax_temp = 0.5 + temp * 0.5;
        if (typeof(softmax_temperature) === "number") {
            softmax_temp = softmax_temperature;
        }

        var z = nj.array(y);

        var init_state = nj.tanh(nj.add(nj.dot(z, enc_w), enc_b));

        var c = init_state.slice([0, dec_num_units]).clone();
        var h = init_state.slice([dec_num_units, 2 * dec_num_units]).clone();

        var rnn_state;
        var dx, dy, pen_down, pen_up, pen_end;
        var pdf;
        var x = nj.array([0, 0, 0, 0, 0]);
        var result = [];
        var lstm_input;

        for (var i = 0; i < max_seq_len; i++) {
            lstm_input = nj.concatenate([x, z]);
            rnn_state = dec_lstm.forward(lstm_input, h, c);
            pdf = get_pdf(rnn_state);
            [dx, dy, pen_down, pen_up, pen_end] = sample(pdf, temp, softmax_temp);
            result.push([dx, dy, pen_down, pen_up, pen_end]);
            if (pen_end === 1) {
                return result;
            }
            x = nj.array([dx / scale_factor, dy / scale_factor, pen_down, pen_up, pen_end]);
            h = rnn_state[0];
            c = rnn_state[1];
        }
github SeldonIO / seldon-core / incubating / wrappers / s2i / nodejs / microservice.js View on Github external
const rest_data_to_array = function(data) {
  let features = null;
  if (data["tensor"]) {
    features = nj
      .array(data["tensor"]["values"])
      .reshape(data["tensor"]["shape"]);
  } else if (data["ndarray"]) {
    features = nj.array(data["ndarray"]);
  } else {
    features = nj.array([]);
  }
  return features.tolist();
};