karpathy / karpathy/micrograd

Data type issue in "micrograd/micrograd /nn.py: class Neuron(Module)/__call__(self, x)"

Open
#106 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
17.6k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

source code in micrograd/micrograd /nn.py: class Neuron(Module)/__call__(self, x) is:

class Neuron(Module):

def __init__(self, nin, nonlin=True):
self.w = [Value(random.uniform(-1,1)) for _ in range(nin)]
self.b = Value(0)
self.nonlin = nonlin

def __call__(self, x):
act = sum((wi*xi for wi,xi in zip(self.w, x)), self.b)
return act.relu() if self.nonlin else act

when you ran it, it occurred:

Cell In[14], line 21, in Neuron.__call__(self, x)
19 def __call__(self, x):
20 act = sum(wi*xi for wi,xi in zip(self.w, x)), self.b
---> 21 return act.relu() if self.nonlin else act

AttributeError: 'tuple' object has no attribute 'relu'

so it should be changed as :

class Neuron(Module):

def __init__(self, nin, nonlin=True):
self.w = [Value(random.uniform(-1,1)) for _ in range(nin)]
self.b = Value(0)
self.nonlin = nonlin

def __call__(self, x):
act = sum(wi*xi for wi,xi in zip(self.w, x)) + self.b
return act.relu() if self.nonlin else act

Contributor guide

No contributing guide indexed for this repository

Research direction

Open micrograd/micrograd/nn.py and inspect Neuron.__call__(self, x), focusing on how act is computed before relu() is called. Correct the tuple-producing expression so the method returns a Value-compatible activation, then verify that the reported AttributeError no longer occurs when the neuron is run.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.