ANN代码调试求助:移除scanf输入后无法得到正确输出结果
scanf Hey there! Let’s figure out why your ANN stops producing the expected output (1 1 1 0) once you remove scanf—this is a super common pitfall when swapping user input for hardcoded values, so let’s walk through the most likely issues you might have missed:
1. Your Hardcoded Input Values Don’t Match the Original Test Case
The biggest culprit here is usually mismatched hardcoded data. When you used scanf, you were probably feeding in specific values (like 4 samples, a set of input features, target outputs, and iteration count) that made the ANN converge to the right result. When you removed scanf, if you:
- Set the wrong sample size (
s) (e.g., using 3 instead of 4) - Typed incorrect input features or target outputs (e.g., mixing up
1.0and0.0in the target array) - Used an insufficient number of iterations (
ite) that doesn’t let the weights train properly
Your ANN won’t learn the pattern needed to output 1 1 1 0. For example, if your original test case was a NAND gate (which produces exactly that output), your hardcoded data should look like this:
// Hardcode the sample size (matches what you'd enter via scanf) int s = 4; // Hardcode input features (2 inputs per sample for NAND) float inputs[4][2] = {{0.0, 0.0}, {0.0, 1.0}, {1.0, 0.0}, {1.0, 1.0}}; // Hardcode target outputs (matches your expected 1 1 1 0) float targets[4] = {1.0, 1.0, 1.0, 0.0}; // Set a reasonable iteration count (e.g., 1000) to ensure convergence int ite = 1000;
2. Uninitialized Variables Are Causing Garbage Values
When you used scanf, variables like s, ite, or even weight arrays were being populated with user input. If you removed scanf but forgot to initialize these variables, they’ll hold random garbage values. For example:
- If
sis uninitialized, your training loop might run 0 times or a random number of times, leaving weights untrained. - If weight arrays aren’t initialized to small random values or 0.0, the ANN will start with nonsensical weights that can’t converge to the right output.
Always double-check that every variable used in your ANN (sample size, iteration count, weights, biases) has a clear initial value when you remove scanf.
3. You Accidentally Broke Training Loop Logic
It’s easy to tweak loop conditions when swapping out scanf for hardcoded values. For example:
- Changing
for (i = 0; i < s; i++)tofor (i = 0; i <= s; i++)(which would process an extra, invalid sample) - Misaligning the indices between your input array, target array, and training loop
Even a tiny off-by-one error here can throw off weight updates and lead to wrong outputs.
Quick Fix Example
Here’s how you might replace your scanf block with correct hardcoded values to match your expected output:
#include <stdio.h> #include <stdlib.h> int main() { int s = 4, m = 2, dot, i, j, k, ite = 1000; float iterationError, T = 0.5; // Hardcoded input and target data for NAND gate float inputs[4][2] = {{0.0, 0.0}, {0.0, 1.0}, {1.0, 0.0}, {1.0, 1.0}}; float targets[4] = {1.0, 1.0, 1.0, 0.0}; // Initialize weights (example: small random values or 0.0) float weights[2] = {0.1, 0.1}; float bias = 0.1; // Rest of your ANN training code (forward pass, error calculation, weight update) // ... // After training, run inference and print outputs for (i = 0; i < s; i++) { float output = (inputs[i][0] * weights[0] + inputs[i][1] * weights[1] + bias); // Apply threshold T printf("%d ", (output >= T) ? 1 : 0); } return 0; }
Start by verifying your hardcoded data matches exactly what you used to input via scanf, then check for uninitialized variables and loop logic issues. That should get your ANN back to producing the expected 1 1 1 0 output!
内容的提问来源于stack exchange,提问作者kingerick

