-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
LightningModule should support Dataclass #12506
Comments
🐛 BugWhen add dataclass to a lightning module then build layers in
To Reproducehttps://colab.research.google.com/drive/1aqHDBUaEf7DWKWIo86uoTtxPqCybLLb9?usp=sharing @dataclass
class SimpleModel(pl.LightningModule):
num_class: int = 10
pixel_side_length: int = 28
num_heads: int = 28
def __post_init__(self):
pl.LightningModule.__init__(self)
# Layers
self.attn = nn.MultiheadAttention(embed_dim=self.pixel_side_length, num_heads=self.num_heads, batch_first=True)
self.norm = nn.LayerNorm(self.pixel_side_length)
# A very tight ffn
self.ffn = nn.Sequential(nn.Linear(self.pixel_side_length*self.pixel_side_length, self.pixel_side_length),
nn.ReLU(),
nn.Linear(self.pixel_side_length, self.num_class))
self.softmax = nn.Softmax()
def forward(self, x):
x = torch.squeeze(x, 1)
attn_out, _ = self.attn(x, x, x)
norm_out = self.norm(x + attn_out)
ffn_out = self.ffn(norm_out.reshape(-1, self.pixel_side_length*self.pixel_side_length))
return self.softmax(ffn_out)
def training_step(self, batch, batch_idx):
x, y = batch
out = self(x)
loss = F.cross_entropy(out, y)
acc = (out.argmax(dim=-1) == y).float().mean()
self.log("train_loss", loss)
self.log("train_acc", acc)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
out = self(x)
loss = F.cross_entropy(out, y)
acc = (out.argmax(dim=-1) == y).float().mean()
return acc
def validation_epoch_end(self, outputs):
self.log("val_acc", torch.stack(outputs).mean())
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=0.001) Environment
Additional context |
I also added my attempt using dataclass with lightning module in the form of Bug Report |
There are this post on PyTorch. When using So should I close this issue? However I think Lightning can support this feature to be consistent with LightningDataModule |
Hi, Have you tried with
Other than that, I think it's very unlikely that we will support this if it does override |
@justusschock I have tried it with I understand what you are saying. |
@justusschock I would like to propose something else. It's not off title. It's still about LightningModule working with dataclass .The users can write their config for the Lightning module derived class as follows from dataclasses import dataclass
from dataclass_wizard import YAMLWizard
@dataclass
class HyperConfig(YAMLWizard):
feature_in: int = 10
feature_out: int = 2 then they can define their model class BoringModel(LightningModule):
def __init__(self, hyper_config: HyperConfig = HyperConfig()):
super().__init__()
self.hyper_config = hyper_config
self.layer = torch.nn.Linear(hyper_config.feature_in, hyper_config.feature_out)
self.save_hyperconfig() # a new function to not conflict self.save_hyperparameters() When defining like this, Code Hint/Code Suggestion/Intelisense of Jupyter/Vscode will be able to know what
so what do you think? HOWEVER, I do think that it would be better if there is a way to make When I save hyper_config: !!python/object:__main__.HyperConfig
feature_in: 10
feature_out: 2 |
This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team! |
Please read this comment #12506 (comment)
🚀 Feature
In issue #8272 we can see that LightningDataModule is compatible with dataclass. LightningModule should also be compatible alo
Motivation
Code example
Alternatives
Additional context
If you enjoy Lightning, check out our other projects! ⚡
Metrics: Machine learning metrics for distributed, scalable PyTorch applications.
Lite: enables pure PyTorch users to scale their existing code on any kind of device while retaining full control over their own loops and optimization logic.
Flash: The fastest way to get a Lightning baseline! A collection of tasks for fast prototyping, baselining, fine-tuning, and solving problems with deep learning.
Bolts: Pretrained SOTA Deep Learning models, callbacks, and more for research and production with PyTorch Lightning and PyTorch.
Lightning Transformers: Flexible interface for high-performance research using SOTA Transformers leveraging Pytorch Lightning, Transformers, and Hydra.
cc @Borda
The text was updated successfully, but these errors were encountered: