<commit_before>NeuralNetwork = function(options) {
  this.learningRate = 0.5;
  this.growthRate = 0.5;
  this.setOptions(options);

  this.createLayers(this.hidden);
}

NeuralNetwork.prototype = {

  setOptions : function(options) {
    if(options) {
      for(option in options)
        this[option] = options[option];
    }
  },

  createLayers : function(hidden, json) {
    this.layers = [];

    var nlayers = 3; // one hidden layer is default
    if(hidden)
      nlayers = hidden.length + 2;
    else if(json)
      nlayers = json.layers.length;

    var prevLayer;
    for(var i = 0; i < nlayers; i++) {
      if(hidden)
        var nnodes = hidden[i - 1];
      if(json)
        var layerJSON = json.layers[i];
      var layer = new Layer(this, prevLayer, nnodes, layerJSON);
      this.layers.push(layer);
      prevLayer = layer;
    }

    this.inputLayer = this.layers[0];
    this.outputLayer = this.layers[nlayers - 1];
    if(!hidden && !json)
      this.hiddenLayer = this.layers[1];
    else
      this.hiddenLayer = null;
  },

  run : function(inputs) {
    this.inputLayer.createNodes(inputs);
    if(this.hiddenLayer)
      this.hiddenLayer.growLayer(this.inputLayer.size);

    this.inputLayer.outputs = inputs;

    for(var i = 1; i < this.layers.length; i++)
      this.layers[i].calcOutputs();

    var outputs = this.outputLayer.outputs;
    return this.formatOutput(outputs);
  },

  trainItem : function(inputs, targets) {
    this.outputLayer.createNodes(targets);

    this.run(inputs);

    this.outputLayer.calcErrors(targets);
    for(var i = this.layers.length - 2; i >= 0; i--)
      this.layers[i].calcErrors();

    for(var i = 1; i < this.layers.length; i++)
      this.layers[i].adjustWeights();

    return this.outputLayer.error;
  },

  train: function(data, iterations, errorThresh) {
    if(!iterations)
      iterations = 20000;
    if(!errorThresh)
      errorThresh = 0.01;
    var error = 1;
    for(var i = 0; i < iterations && error > errorThresh; i++) {
      var sum = 0;
      for(var j = 0; j < data.length; j++)
        sum += this.trainItem(data[j].input, data[j].output);
      error = sum / data.length;
    }
    return error;
  },

  formatOutput : function(outputs) {
    var values = [];
    for(var id in outputs) {
      if(parseInt(id) != id) // not an array
        return outputs;
      values.push(outputs[id]);
    }
    return values;
  },

  toJSON : function() {
    var json = {layers: []};
    for(var i = 0; i < this.layers.length; i++)
      json.layers.push(this.layers[i].toJSON());
    return json;
  },

  fromJSON : function(json) {
    this.layers = [];
    this.createLayers(null, json);
    return this;
  },

  toFunction: function() {
    var json = this.toJSON();
    // currying w/ closures won't do, this needs to be standalone
    return new Function("inputs",
 'var net = ' + JSON.stringify(json) + ';\n\n\
  for(var i = 1; i < net.layers.length; i++) {\n\
    var nodes = net.layers[i].nodes;\n\
    var outputs = {};\n\
    for(var id in nodes) {\n\
      var node = nodes[id];\n\
      var sum = node.bias;\n\
      for(var iid in node.weights)\n\
        sum += node.weights[iid] * inputs[iid];\n\
      outputs[id] = (1/(1 + Math.exp(-sum)));\n\
    }\n\
    inputs = outputs;\n\
  }\n\
return outputs;');
    // note: this doesn't handle never-been-seen before inputs
  },

  toString : function() {
    return JSON.stringify(this.toJSON());
  }
}
<commit_msg>more toFunction() formatting<commit_after>NeuralNetwork = function(options) {
  this.learningRate = 0.5;
  this.growthRate = 0.5;
  this.setOptions(options);

  this.createLayers(this.hidden);
}

NeuralNetwork.prototype = {

  setOptions : function(options) {
    if(options) {
      for(option in options)
        this[option] = options[option];
    }
  },

  createLayers : function(hidden, json) {
    this.layers = [];

    var nlayers = 3; // one hidden layer is default
    if(hidden)
      nlayers = hidden.length + 2;
    else if(json)
      nlayers = json.layers.length;

    var prevLayer;
    for(var i = 0; i < nlayers; i++) {
      if(hidden)
        var nnodes = hidden[i - 1];
      if(json)
        var layerJSON = json.layers[i];
      var layer = new Layer(this, prevLayer, nnodes, layerJSON);
      this.layers.push(layer);
      prevLayer = layer;
    }

    this.inputLayer = this.layers[0];
    this.outputLayer = this.layers[nlayers - 1];
    if(!hidden && !json)
      this.hiddenLayer = this.layers[1];
    else
      this.hiddenLayer = null;
  },

  run : function(inputs) {
    this.inputLayer.createNodes(inputs);
    if(this.hiddenLayer)
      this.hiddenLayer.growLayer(this.inputLayer.size);

    this.inputLayer.outputs = inputs;

    for(var i = 1; i < this.layers.length; i++)
      this.layers[i].calcOutputs();

    var outputs = this.outputLayer.outputs;
    return this.formatOutput(outputs);
  },

  trainItem : function(inputs, targets) {
    this.outputLayer.createNodes(targets);

    this.run(inputs);

    this.outputLayer.calcErrors(targets);
    for(var i = this.layers.length - 2; i >= 0; i--)
      this.layers[i].calcErrors();

    for(var i = 1; i < this.layers.length; i++)
      this.layers[i].adjustWeights();

    return this.outputLayer.error;
  },

  train: function(data, iterations, errorThresh) {
    if(!iterations)
      iterations = 20000;
    if(!errorThresh)
      errorThresh = 0.01;
    var error = 1;
    for(var i = 0; i < iterations && error > errorThresh; i++) {
      var sum = 0;
      for(var j = 0; j < data.length; j++)
        sum += this.trainItem(data[j].input, data[j].output);
      error = sum / data.length;
    }
    return error;
  },

  formatOutput : function(outputs) {
    var values = [];
    for(var id in outputs) {
      if(parseInt(id) != id) // not an array
        return outputs;
      values.push(outputs[id]);
    }
    return values;
  },

  toJSON : function() {
    var json = {layers: []};
    for(var i = 0; i < this.layers.length; i++)
      json.layers.push(this.layers[i].toJSON());
    return json;
  },

  fromJSON : function(json) {
    this.layers = [];
    this.createLayers(null, json);
    return this;
  },

  toFunction: function() {
    var json = this.toJSON();
    // currying w/ closures won't do, this needs to be standalone
    return new Function("inputs",
'  var net = ' + JSON.stringify(json) + ';\n\n\
  for(var i = 1; i < net.layers.length; i++) {\n\
    var nodes = net.layers[i].nodes;\n\
    var outputs = {};\n\
    for(var id in nodes) {\n\
      var node = nodes[id];\n\
      var sum = node.bias;\n\
      for(var iid in node.weights)\n\
        sum += node.weights[iid] * inputs[iid];\n\
      outputs[id] = (1/(1 + Math.exp(-sum)));\n\
    }\n\
    inputs = outputs;\n\
  }\n\
  return outputs;');
    // note: this doesn't handle never-been-seen before inputs
  },

  toString : function() {
    return JSON.stringify(this.toJSON());
  }
}

{'fraction_non_alphanumeric': 0.15517738678973175, 'fraction_numerical': 0.008941447937698297, 'mean_word_length': 2.4849246231155777, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7495351', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2320, 'n_words': 538}