Skip to content

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

@Ericlee180

Description

@Ericlee180

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions