Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Can you provide an example of a Simple neural network class in pure JavaScript with no…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Can you provide an example of a Simple neural network class in pure JavaScript with no libraries.

  2. Assistant

    class Neuron { constructor() { this.weights = []; }

    randomWeight() { return Math.random() * 2 - 1; }

    activate(input) { let sum = 0; for (let i = 0; i < input.length; i++) { sum += input[i] * this.weights[i]; } return 1 / (1 + Math.exp(-sum)); } }

    class NeuralNetwork { constructor(inputSize, hiddenSize, outputSize) { this.inputSize = inputSize; this.hiddenSize = hiddenSize; this.outputSize = outputSize;

    this.hiddenLayer = [];
    for (let i = 0; i < hiddenSize; i++) {
      let neuron = new Neuron();
      neuron.weights = Array(inputSize).fill(0).map(() => neuron.randomWeight());
      this.hiddenLayer.push(neuron);
    }
    
    this.outputLayer = [];
    for (let i = 0; i < outputSize; i++) {
      let neuron = new Neuron();
      neuron.weights = Array(hiddenSize).fill(0).map(() => neuron.randomWeight());
      this.outputLayer.push(neuron);
    }
    

    }

    feedForward(input) { let hiddenOutput = this.hiddenLayer.map(neuron => neuron.activate(input)); let output = this.outputLayer.map(neuron => neuron.activate(hiddenOutput)); return output; } }

  3. User

    class NeuralNetwork { constructor(inputSize, outputSize) { // Initialize weights and biases with random values this.weights = Array.from({ length: inputSize }, () => Math.random()); this.bias = Math.random(); this.outputSize = outputSize; }

    // Sigmoid activation function sigmoid(x) { return 1 / (1 + Math.exp(-x)); }

    // Feedforward calculation feedforward(inputs) { // Calculate weighted sum of inputs and weights, add bias, and apply sigmoid activation function const output = []; for (let i = 0; i < this.outputSize; i++) { let sum = 0; for (let j = 0; j < inputs.length; j++) { sum += inputs[j] * this.weights[j]; } sum += this.bias; output.push(this.sigmoid(sum)); } return output; }

    // Train the network on a set of input/output pairs using gradient descent train(inputs, expectedOutputs, learningRate, epochs) { for (let epoch = 0; epoch < epochs; epoch++) { for (let i = 0; i < inputs.length; i++) { const output = this.feedforward(inputs[i]);

        // Calculate error for each output node
        const errors = [];
        for (let j = 0; j < this.outputSize; j++) {
          errors.push(expectedOutputs[i][j] - output[j]);
        }
    
        // Update weights and bias for each input node
        for (let j = 0; j < inputs[i].length; j++) {
          for (let k = 0; k < this.outputSize; k++) {
            this.weights[j] += learningRate * errors[k] * output[k] * (1 - output[k]) * inputs[i][j];
          }
        }
        for (let j = 0; j < this.outputSize; j++) {
          this.bias += learningRate * errors[j] * output[j] * (1 - output[j]);
        }
      }
    }
    

    } }

  4. Assistant

    I'm sorry, but I do not understand what I am supposed to do with the given prompt. Do you want me to comment on the code, find bugs or try to integrate it with my previous response? Please provide more context.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.