Declare neural networks clearly

Declare neural networks clearly

Declarar redes neuronales claramentelink image 1

Disclaimer: This post has been translated to English using a machine translation model. Please, let me know if you find any mistakes.

When in PyTorch a neural network is created as a list of layers

	
< > Input
Python
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
Copied

Then iterating through it in the forward method is not so clear

	
< > Input
Python
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
Copied

However, when creating a neural network as a dictionary of layers

	
< > Input
Python
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList({
'linear': nn.Linear(1, 10),
'activation': nn.ReLU(),
'output': nn.Linear(10, 1)
})
Copied

Then iterating through it in the forward method is clearer

	
< > Input
Python
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1, 10),
nn.ReLU(),
nn.Linear(10, 1)
])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList({
'linear': nn.Linear(1, 10),
'activation': nn.ReLU(),
'output': nn.Linear(10, 1)
})
import torch
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layers = nn.ModuleList({
'linear': nn.Linear(1, 10),
'activation': nn.ReLU(),
'output': nn.Linear(10, 1)
})
def forward(self, x):
x = self.layers['linear'](x)
x = self.layers['activation'](x)
x = self.layers['output'](x)
return x
Copied

Frequently asked questions

Why does the forward method access layers with self.layers['linear'](x) instead of looping with for layer in self.layers?

When the layers are stored as a list (nn.ModuleList with square brackets [...]), you can only iterate with for layer in self.layers: x = layer(x), losing each layer's name. When they're stored as a dictionary (curly braces {...} with keys like 'linear', 'activation', 'output'), forward can call them explicitly by name — x = self.layers['linear'](x) — which makes it far clearer which transformation is applied at each step.

I get TypeError: str is not a Module subclass when passing a dict to nn.ModuleList — how do I fix it?

nn.ModuleList expects an iterable of modules; passing it a dict like nn.ModuleList({'linear': nn.Linear(1,10), ...}) makes Python iterate over the dict's keys (strings) instead of its values, so PyTorch tries to register the string 'linear' as a layer and raises TypeError: str is not a Module subclass. The fix is to use nn.ModuleDict({...}) instead of nn.ModuleList({...}) — it's built to hold named submodules and lets you index them as self.layers['linear'].

Continue reading

Last posts -->

Have you seen these projects?

Gymnasia

Gymnasia Gymnasia
Expo
React Native
TypeScript
OpenAI
Anthropic

Fitness app with two agents that run entirely on the device, with no backend, so the user's data never leaves the phone. A BYOK conversational coach with local tools and a remote system prompt with offline fallback, plus a food estimator that pulls macronutrients out of a photo of the plate, with barcode scanning against OpenFoodFacts.

LangGraph Deep Researcher

LangGraph Deep Researcher LangGraph Deep Researcher
Python
LangGraph
FastAPI
React
TypeScript
Docker

Multi-agent research system built with LangGraph. A supervisor breaks your question down into topics and launches search sub-agents in parallel; each one compresses its findings before handing them to a writer agent that produces the final sourced markdown report. Live streaming over WebSockets, a configurable model per role and bring-your-own API keys that are never persisted server-side.

Tau

Tau Tau
Python
LangChain

Multi-agent tutoring system for secondary school students, with one agent per subject and course material written and validated by a team of teachers. It was used with real students at a private school in Spain and at a secondary school in Colombia.

View all projects -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to watch any talk?

Last talks -->

Do you want to improve with these tips?

Last tips -->

Use this locally

Hugging Face spaces allow us to run models with very simple demos, but what if the demo breaks? Or if the user deletes it? That's why I've created docker containers with some interesting spaces, to be able to use them locally, whatever happens. In fact, if you click on any project view button, it may take you to a space that doesn't work.

Flow edit

Flow edit Flow edit

FLUX.1-RealismLora

FLUX.1-RealismLora FLUX.1-RealismLora
View all containers -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to train your model with these datasets?

short-jokes-dataset

HuggingFace

Dataset with jokes in English

Use: Fine-tuning text generation models for humor

231K rows 2 columns 45 MB
View on HuggingFace →

opus100

HuggingFace

Dataset with translations from English to Spanish

Use: Training English-Spanish translation models

1M rows 2 columns 210 MB
View on HuggingFace →

netflix_titles

HuggingFace

Dataset with Netflix movies and series

Use: Netflix catalog analysis and recommendation systems

8.8K rows 12 columns 3.5 MB
View on HuggingFace →
View more datasets -->