Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 };
}
}
}
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) ) );
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));
}
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;
};
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);
}
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];
function curve(nums) {
nums = nums.tolist();
var result = [];
for(var i = 0; i < nums.length; i++) {
result[i] = [];
for(var ii=0; ii
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];
}
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();
};