关于Brain.js神经网络输入输出、权重及输出参数的技术咨询
Hey there! Let's walk through your questions about using Brain.js for neural networks, using typical library examples as a reference.
1. Input Neurons vs. Output Neurons: Do [1, 0] Inputs Corresponding to [1, 0] Outputs?
Short answer: Not automatically—it all depends on how you define your training dataset.
In Brain.js, the number of input neurons is set by the length of the input arrays in your training data, and the number of output neurons is determined by the length of the output arrays. For example:
- If your training data looks like this:
You’re using 2 input neurons to predict a single binary output.const trainingData = [ { input: [1, 0], output: [1] }, // 2 input neurons, 1 output neuron { input: [0, 1], output: [0] } ]; - If your example uses
output: [1, 0], that means you’ve defined a model with 2 output neurons—maybe for multi-class classification or a direct 2-to-2 value mapping.
The input values [1, 0] don’t inherently "match" output values [1, 0]; the model learns the specific mapping you teach it through training data.
2. What Are Weights, and Where Do They Live in Brain.js?
Weights are the core parameters of a neural network—they’re numerical values that represent the strength of connections between neurons. A positive weight amplifies the signal passed between neurons, while a negative weight dampens it. The model adjusts these weights during training to minimize prediction errors.
In Brain.js, you don’t need to manually define or set weights upfront—the library handles initialization and updates automatically. But if you want to inspect or export them:
- After training, convert the model to JSON, which includes all weight data:
The weights are stored as nested arrays, representing connections between each layer (input → hidden, hidden → output, etc.). Each entry corresponds to the weight between a specific neuron in one layer and a neuron in the next.const net = new brain.NeuralNetwork(); net.train(trainingData); const modelDetails = net.toJSON(); console.log(modelDetails.weights); // Logs weight matrices between layers
3. What Parameters Do You Pass for Output/Inference?
Once your model is trained, use the run() method to get predictions. The parameter you pass must match the format and length of the input arrays used in training.
For example, if you trained with input arrays of length 2:
// Get prediction for input [1, 0] const prediction = net.run([1, 0]); console.log(prediction); // Output matches your training output format (e.g., [0.98, 0.02] for 2 output neurons)
If your training outputs were binary (like [1] or [0]), the prediction will be a value between 0 and 1 representing the model’s confidence. For multi-output models, you’ll get an array of values corresponding to each output neuron.
内容的提问来源于stack exchange,提问作者Kyrylo Kalashnikov

