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

readable text representaton #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions micrograd/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def parameters(self):

def __repr__(self):
return f"{'ReLU' if self.nonlin else 'Linear'}Neuron({len(self.w)})"

def __str__(self):
fstr = f"{'ReLU' if self.nonlin else 'Linear'}Neuron({len(self.w)}) -> "
for idx, wi in enumerate(self.w):
fstr += f"w{idx} ={wi.data:7.4f}, "
fstr += f"b ={self.b.data:7.4f}"
return fstr

class Layer(Module):

Expand All @@ -40,7 +47,13 @@ def parameters(self):
return [p for n in self.neurons for p in n.parameters()]

def __repr__(self):
return f"Layer of [{', '.join(str(n) for n in self.neurons)}]"
return f"Layer of [{', '.join(str(n.__repr__()) for n in self.neurons)}]"

def __str__(self):
fstr = f"Shape of the layer is: {len(self.neurons[0].w)} X {len(self.neurons)} (nin X nout)\n"
for idx, neuron in enumerate(self.neurons):
fstr += f" Neuron {idx+1}: {neuron.__str__()} \n"
return fstr

class MLP(Module):

Expand All @@ -57,4 +70,10 @@ def parameters(self):
return [p for layer in self.layers for p in layer.parameters()]

def __repr__(self):
return f"MLP of [{', '.join(str(layer) for layer in self.layers)}]"
return f"MLP of [{', '.join(str(layer.__repr__()) for layer in self.layers)}]"

def __str__(self):
fstr = "Multi-Layer Perceptron Structure:\n"
for idx, layer in enumerate(self.layers):
fstr += f" Layer {idx+1}/{len(self.layers)} - {layer}\n"
return fstr