Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle single input neuron returned by layer with single output #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jonasrauber
Copy link

The Layer class special cases single output neurons:

return out[0] if len(out) == 1 else out

This is nice for loss functions but currently breaks for intermediate layers with a single neuron.

from micrograd.nn import MLP
from micrograd.engine import Value

# works
model = MLP(2, [16, 16, 1])
x = [Value(1.0), Value(-2.0)]
print(model(x))

# fails: intermediate layer with 1 neuron
model = MLP(2, [16, 1, 1])
x = [Value(1.0), Value(-2.0)]
print(model(x))

This PR fixes this by correctly handling a single input Value.

Note that I currently only check for Value instances explicitly, which is good enough for intermediate layers.

To handle single network input neurons, we should also check for float and int scalars. I kept it as minimal as possible for now, but I can add that if you want. (Technically, it would probably be best to check if the input is iterable or not, but doing that right nowadays requires a try statement.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant