Files
pytorch-lightning/LightningModule/RequiredTrainerInterface/index.html
T

1343 lines
34 KiB
HTML

<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="Documentation for Pytorch LightningModule, the researcher version of keras.">
<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="en">
<meta name="lang:search.pipeline.stopwords" content="True">
<meta name="lang:search.pipeline.trimmer" content="True">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">
<link rel="shortcut icon" href="../../assets/images/favicon.png">
<meta name="generator" content="mkdocs-1.0.4, mkdocs-material-4.4.0">
<title>Lightning Module interface - Pytorch lightning Documentation</title>
<link rel="stylesheet" href="../../assets/stylesheets/application.0284f74d.css">
<script src="../../assets/javascripts/modernizr.74668098.js"></script>
<link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono&display=fallback">
<style>body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}</style>
<link rel="stylesheet" href="../../assets/fonts/material-icons.css">
</head>
<body dir="ltr">
<svg class="md-svg">
<defs>
<svg xmlns="http://www.w3.org/2000/svg" width="416" height="448" viewBox="0 0 416 448" id="__github"><path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z"/></svg>
</defs>
</svg>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<a href="#lightning-module-interface" tabindex="1" class="md-skip">
Skip to content
</a>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../.." title="Pytorch lightning Documentation" class="md-header-nav__button md-logo">
<i class="md-icon"></i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Pytorch lightning Documentation
</span>
<span class="md-header-nav__topic">
Lightning Module interface
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
&#xE5CD;
</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/williamFalcon/pytorch-lightning/" title="Go to repository" class="md-source" data-md-source="github">
<div class="md-source__icon">
<svg viewBox="0 0 24 24" width="24" height="24">
<use xlink:href="#__github" width="24" height="24"></use>
</svg>
</div>
<div class="md-source__repository">
GitHub
</div>
</a>
</div>
</div>
</div>
</nav>
</header>
<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0">
<label class="md-nav__title md-nav__title--site" for="__drawer">
<a href="../.." title="Pytorch lightning Documentation" class="md-nav__button md-logo">
<i class="md-icon"></i>
</a>
Pytorch lightning Documentation
</label>
<div class="md-nav__source">
<a href="https://github.com/williamFalcon/pytorch-lightning/" title="Go to repository" class="md-source" data-md-source="github">
<div class="md-source__icon">
<svg viewBox="0 0 24 24" width="24" height="24">
<use xlink:href="#__github" width="24" height="24"></use>
</svg>
</div>
<div class="md-source__repository">
GitHub
</div>
</a>
</div>
<ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item">
<a href="../.." title="PYTORCH-LIGHTNING DOCUMENTATION" class="md-nav__link">
PYTORCH-LIGHTNING DOCUMENTATION
</a>
</li>
<li class="md-nav__item">
<a href="../../Examples/" title="Examples" class="md-nav__link">
Examples
</a>
</li>
<li class="md-nav__item md-nav__item--active md-nav__item--nested">
<input class="md-toggle md-nav__toggle" data-md-toggle="nav-3" type="checkbox" id="nav-3" checked>
<label class="md-nav__link" for="nav-3">
LightningModule
</label>
<nav class="md-nav" data-md-component="collapsible" data-md-level="1">
<label class="md-nav__title" for="nav-3">
LightningModule
</label>
<ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item md-nav__item--active">
<input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">
<label class="md-nav__link md-nav__link--active" for="__toc">
Lightning Module interface
</label>
<a href="./" title="Lightning Module interface" class="md-nav__link md-nav__link--active">
Lightning Module interface
</a>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item">
<a href="#training_step" title="training_step" class="md-nav__link">
training_step
</a>
</li>
<li class="md-nav__item">
<a href="#validation_step" title="validation_step" class="md-nav__link">
validation_step
</a>
</li>
<li class="md-nav__item">
<a href="#validation_end" title="validation_end" class="md-nav__link">
validation_end
</a>
</li>
<li class="md-nav__item">
<a href="#configure_optimizers" title="configure_optimizers" class="md-nav__link">
configure_optimizers
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#get_save_dict" title="get_save_dict" class="md-nav__link">
get_save_dict
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_1" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#load_model_specific" title="load_model_specific" class="md-nav__link">
load_model_specific
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_2" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#tng_dataloader" title="tng_dataloader" class="md-nav__link">
tng_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_3" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#val_dataloader" title="val_dataloader" class="md-nav__link">
val_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_4" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#test_dataloader" title="test_dataloader" class="md-nav__link">
test_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_5" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#update_tng_log_metrics" title="update_tng_log_metrics" class="md-nav__link">
update_tng_log_metrics
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_6" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#add_model_specific_args" title="add_model_specific_args" class="md-nav__link">
add_model_specific_args
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_7" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="../methods/" title="Methods" class="md-nav__link">
Methods
</a>
</li>
<li class="md-nav__item">
<a href="../properties/" title="Properties" class="md-nav__link">
Properties
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item md-nav__item--nested">
<input class="md-toggle md-nav__toggle" data-md-toggle="nav-4" type="checkbox" id="nav-4">
<label class="md-nav__link" for="nav-4">
Trainer
</label>
<nav class="md-nav" data-md-component="collapsible" data-md-level="1">
<label class="md-nav__title" for="nav-4">
Trainer
</label>
<ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item">
<a href="../../Trainer/" title="Trainer" class="md-nav__link">
Trainer
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/Checkpointing/" title="Checkpointing" class="md-nav__link">
Checkpointing
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/Distributed training/" title="Distributed training" class="md-nav__link">
Distributed training
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/Logging/" title="Logging" class="md-nav__link">
Logging
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/SLURM Managed Cluster/" title="SLURM Managed Cluster" class="md-nav__link">
SLURM Managed Cluster
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/Training Loop/" title="Training Loop" class="md-nav__link">
Training Loop
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/Validation loop/" title="Validation loop" class="md-nav__link">
Validation loop
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/debugging/" title="Debugging" class="md-nav__link">
Debugging
</a>
</li>
<li class="md-nav__item">
<a href="../../Trainer/hooks/" title="Hooks" class="md-nav__link">
Hooks
</a>
</li>
</ul>
</nav>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item">
<a href="#training_step" title="training_step" class="md-nav__link">
training_step
</a>
</li>
<li class="md-nav__item">
<a href="#validation_step" title="validation_step" class="md-nav__link">
validation_step
</a>
</li>
<li class="md-nav__item">
<a href="#validation_end" title="validation_end" class="md-nav__link">
validation_end
</a>
</li>
<li class="md-nav__item">
<a href="#configure_optimizers" title="configure_optimizers" class="md-nav__link">
configure_optimizers
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#get_save_dict" title="get_save_dict" class="md-nav__link">
get_save_dict
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_1" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#load_model_specific" title="load_model_specific" class="md-nav__link">
load_model_specific
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_2" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#tng_dataloader" title="tng_dataloader" class="md-nav__link">
tng_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_3" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#val_dataloader" title="val_dataloader" class="md-nav__link">
val_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_4" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#test_dataloader" title="test_dataloader" class="md-nav__link">
test_dataloader
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_5" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#update_tng_log_metrics" title="update_tng_log_metrics" class="md-nav__link">
update_tng_log_metrics
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_6" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#add_model_specific_args" title="add_model_specific_args" class="md-nav__link">
add_model_specific_args
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#return_7" title="Return" class="md-nav__link">
Return
</a>
</li>
</ul>
</nav>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<a href="https://github.com/williamFalcon/pytorch-lightning/edit/master/docs/LightningModule/RequiredTrainerInterface.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>
<h1 id="lightning-module-interface">Lightning Module interface</h1>
<p>[<a href="https://github.com/williamFalcon/pytorch-lightning/blob/master/pytorch_lightning/root_module/root_module.py">Github Code</a>]</p>
<p>A lightning module is a strict superclass of nn.Module, it provides a standard interface for the trainer to interact with the model.</p>
<p>The easiest thing to do is copy <a href="../../examples/new_project_templates/lightning_module_template.py">this template</a> and modify accordingly. </p>
<p>Otherwise, to Define a Lightning Module, implement the following methods:</p>
<p><strong>Required</strong>: </p>
<ul>
<li><a href="./#training_step">training_step</a> </li>
<li><a href="./#validation_step">validation_step</a></li>
<li>
<p><a href="./#validation_end">validation_end</a></p>
</li>
<li>
<p><a href="./#configure_optimizers">configure_optimizers</a></p>
</li>
<li><a href="./#get_save_dict">get_save_dict</a></li>
<li>
<p><a href="./#load_model_specific">load_model_specific</a></p>
</li>
<li>
<p><a href="./#tng_dataloader">tng_dataloader</a></p>
</li>
<li><a href="./#tng_dataloader">tng_dataloader</a></li>
<li><a href="./#test_dataloader">test_dataloader</a></li>
</ul>
<p><strong>Optional</strong>: </p>
<ul>
<li><a href="./#update_tng_log_metrics">update_tng_log_metrics</a></li>
<li><a href="./#add_model_specific_args">add_model_specific_args</a></li>
</ul>
<hr />
<h3 id="training_step">training_step</h3>
<pre><code class="python">def training_step(self, data_batch, batch_nb)
</code></pre>
<p>In this step you'd normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something specific to your model.</p>
<p><strong>Params</strong> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>data_batch</td>
<td>The output of your dataloader. A tensor, tuple or list</td>
</tr>
<tr>
<td>batch_nb</td>
<td>Integer displaying which batch this is</td>
</tr>
</tbody>
</table>
<p><strong>Return</strong> </p>
<p>Dictionary or OrderedDict </p>
<table>
<thead>
<tr>
<th>key</th>
<th>value</th>
<th>is required</th>
</tr>
</thead>
<tbody>
<tr>
<td>loss</td>
<td>tensor scalar</td>
<td>Y</td>
</tr>
<tr>
<td>prog</td>
<td>Dict for progress bar display. Must have only tensors</td>
<td>N</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="python">def training_step(self, data_batch, batch_nb):
x, y, z = data_batch
# implement your own
out = self.forward(x)
loss = self.loss(out, x)
output = {
'loss': loss, # required
'prog': {'tng_loss': loss, 'batch_nb': batch_nb} # optional
}
# return a dict
return output
</code></pre>
<hr />
<h3 id="validation_step">validation_step</h3>
<pre><code class="python">def validation_step(self, data_batch, batch_nb)
</code></pre>
<p>In this step you'd normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something specific to your model.
This is most likely the same as your training_step. But unlike training step, the outputs from here will go to validation_end for collation.</p>
<p><strong>Params</strong> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>data_batch</td>
<td>The output of your dataloader. A tensor, tuple or list</td>
</tr>
<tr>
<td>batch_nb</td>
<td>Integer displaying which batch this is</td>
</tr>
</tbody>
</table>
<p><strong>Return</strong> </p>
<table>
<thead>
<tr>
<th>Return</th>
<th>description</th>
<th>optional</th>
</tr>
</thead>
<tbody>
<tr>
<td>dict</td>
<td>Dict of OrderedDict with metrics to display in progress bar. All keys must be tensors.</td>
<td>Y</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="python">def validation_step(self, data_batch, batch_nb):
x, y, z = data_batch
# implement your own
out = self.forward(x)
loss = self.loss(out, x)
# calculate acc
labels_hat = torch.argmax(out, dim=1)
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
# all optional...
# return whatever you need for the collation function validation_end
output = OrderedDict({
'val_loss': loss_val,
'val_acc': torch.tensor(val_acc), # everything must be a tensor
})
# return an optional dict
return output
</code></pre>
<hr />
<h3 id="validation_end">validation_end</h3>
<pre><code class="python">def validation_end(self, outputs)
</code></pre>
<p>Called at the end of the validation loop with the output of each validation_step.</p>
<p><strong>Params</strong> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>outputs</td>
<td>List of outputs you defined in validation_step</td>
</tr>
</tbody>
</table>
<p><strong>Return</strong> </p>
<table>
<thead>
<tr>
<th>Return</th>
<th>description</th>
<th>optional</th>
</tr>
</thead>
<tbody>
<tr>
<td>dict</td>
<td>Dict of OrderedDict with metrics to display in progress bar</td>
<td>Y</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="python">def validation_end(self, outputs):
&quot;&quot;&quot;
Called at the end of validation to aggregate outputs
:param outputs: list of individual outputs of each validation step
:return:
&quot;&quot;&quot;
val_loss_mean = 0
val_acc_mean = 0
for output in outputs:
val_loss_mean += output['val_loss']
val_acc_mean += output['val_acc']
val_loss_mean /= len(outputs)
val_acc_mean /= len(outputs)
tqdm_dic = {'val_loss': val_loss_mean.item(), 'val_acc': val_acc_mean.item()}
return tqdm_dic
</code></pre>
<hr />
<h3 id="configure_optimizers">configure_optimizers</h3>
<pre><code class="python">def configure_optimizers(self)
</code></pre>
<p>Set up as many optimizers as you need. Normally you'd need one. But in the case of GANs or something more esoteric you might have multiple.
Lightning will call .backward() and .step() on each one. If you use 16 bit precision it will also handle that.</p>
<h5 id="return">Return</h5>
<p>List - List of optimizers</p>
<p><strong>Example</strong></p>
<pre><code class="python"># most cases
def configure_optimizers(self):
opt = Adam(lr=0.01)
return [opt]
# gan example
def configure_optimizers(self):
generator_opt = Adam(lr=0.01)
disriminator_opt = Adam(lr=0.02)
return [generator_opt, disriminator_opt]
</code></pre>
<hr />
<h3 id="get_save_dict">get_save_dict</h3>
<pre><code class="python">def get_save_dict(self)
</code></pre>
<p>Called by lightning to checkpoint your model. Lightning saves current epoch, current batch nb, etc...
All you have to return is what specifically about your lightning model you want to checkpoint.</p>
<h5 id="return_1">Return</h5>
<p>Dictionary - No required keys. Most of the time as described in this example. </p>
<p><strong>Example</strong></p>
<pre><code class="python">def get_save_dict(self):
# 99% of use cases this is all you need to return
checkpoint = {'state_dict': self.state_dict()}
return checkpoint
</code></pre>
<hr />
<h3 id="load_model_specific">load_model_specific</h3>
<pre><code class="python">def load_model_specific(self, checkpoint)
</code></pre>
<p>Called by lightning to restore your model. This is your chance to restore your model using the keys you added in get_save_dict.
Lightning will automatically restore current epoch, batch nb, etc. </p>
<h5 id="return_2">Return</h5>
<p>Nothing </p>
<p><strong>Example</strong></p>
<pre><code class="python">def load_model_specific(self, checkpoint):
# you defined 'state_dict' in get_save_dict()
self.load_state_dict(checkpoint['state_dict'])
</code></pre>
<hr />
<h3 id="tng_dataloader">tng_dataloader</h3>
<pre><code class="python">@property
def tng_dataloader(self)
</code></pre>
<p>Called by lightning during training loop. Define it as a property.</p>
<h5 id="return_3">Return</h5>
<p>Pytorch DataLoader</p>
<p><strong>Example</strong></p>
<pre><code class="python">@property
def tng_dataloader(self):
if self._tng_dataloader is None:
try:
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
dataset = MNIST(root='/path/to/mnist/', train=True, transform=transform, download=True)
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=self.hparams.batch_size,
shuffle=True
)
self._tng_dataloader = loader
except Exception as e:
raise e
return self._tng_dataloader
</code></pre>
<hr />
<h3 id="val_dataloader">val_dataloader</h3>
<pre><code class="python">@property
def tng_dataloader(self)
</code></pre>
<p>Called by lightning during validation loop. Define it as a property.</p>
<h5 id="return_4">Return</h5>
<p>Pytorch DataLoader</p>
<p><strong>Example</strong></p>
<pre><code class="python">@property
def val_dataloader(self):
if self._val_dataloader is None:
try:
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
dataset = MNIST(root='/path/to/mnist/', train=False, transform=transform, download=True)
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=self.hparams.batch_size,
shuffle=True
)
self._val_dataloader = loader
except Exception as e:
raise e
return self._val_dataloader
</code></pre>
<hr />
<h3 id="test_dataloader">test_dataloader</h3>
<pre><code class="python">@property
def test_dataloader(self)
</code></pre>
<p>Called by lightning during test loop. Define it as a property.</p>
<h5 id="return_5">Return</h5>
<p>Pytorch DataLoader</p>
<p><strong>Example</strong></p>
<pre><code class="python">@property
def test_dataloader(self):
if self._test_dataloader is None:
try:
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
dataset = MNIST(root='/path/to/mnist/', train=False, transform=transform, download=True)
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=self.hparams.batch_size,
shuffle=True
)
self._test_dataloader = loader
except Exception as e:
raise e
return self._test_dataloader
</code></pre>
<hr />
<h3 id="update_tng_log_metrics">update_tng_log_metrics</h3>
<pre><code class="python">def update_tng_log_metrics(self, logs)
</code></pre>
<p>Called by lightning right before it logs metrics for this batch.
This is a chance to ammend or add to the metrics about to be logged.</p>
<h5 id="return_6">Return</h5>
<p>Dict </p>
<p><strong>Example</strong></p>
<pre><code class="python">def update_tng_log_metrics(self, logs):
# modify or add to logs
return logs
</code></pre>
<hr />
<h3 id="add_model_specific_args">add_model_specific_args</h3>
<pre><code class="python">@staticmethod
def add_model_specific_args(parent_parser, root_dir)
</code></pre>
<p>Lightning has a list of default argparse commands.
This method is your chance to add or modify commands specific to your model.
The <a href="https://williamfalcon.github.io/test-tube/hyperparameter_optimization/HyperOptArgumentParser/">hyperparameter argument parser</a> is available anywhere in your model by calling self.hparams.</p>
<h5 id="return_7">Return</h5>
<p>An argument parser</p>
<p><strong>Example</strong></p>
<pre><code class="python">@staticmethod
def add_model_specific_args(parent_parser, root_dir):
parser = HyperOptArgumentParser(strategy=parent_parser.strategy, parents=[parent_parser])
# param overwrites
# parser.set_defaults(gradient_clip=5.0)
# network params
parser.opt_list('--drop_prob', default=0.2, options=[0.2, 0.5], type=float, tunable=False)
parser.add_argument('--in_features', default=28*28)
parser.add_argument('--out_features', default=10)
parser.add_argument('--hidden_dim', default=50000) # use 500 for CPU, 50000 for GPU to see speed difference
# data
parser.add_argument('--data_root', default=os.path.join(root_dir, 'mnist'), type=str)
# training params (opt)
parser.opt_list('--learning_rate', default=0.001, type=float, options=[0.0001, 0.0005, 0.001, 0.005],
tunable=False)
parser.opt_list('--batch_size', default=256, type=int, options=[32, 64, 128, 256], tunable=False)
parser.opt_list('--optimizer_name', default='adam', type=str, options=['adam'], tunable=False)
return parser
</code></pre>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../../Examples/" title="Examples" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
Examples
</span>
</div>
</a>
<a href="../methods/" title="Methods" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
Methods
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
powered by
<a href="https://www.mkdocs.org">MkDocs</a>
and
<a href="https://squidfunk.github.io/mkdocs-material/">
Material for MkDocs</a>
</div>
</div>
</div>
</footer>
</div>
<script src="../../assets/javascripts/application.245445c6.js"></script>
<script>app.initialize({version:"1.0.4",url:{base:"../.."}})</script>
</body>
</html>