Unsupported format string passed to list.__format__

Florentino: <p>when I save my checkpoint, the error occurs, how to fix it?<br>
the config file, notice CHECKPOINT</p>
<pre><code class="lang-auto">import torch

DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
CSV_DIR = 'C:/Users/PML/Documents/Florentino_space/cast_iron_preprocess/csv512_offset50'
IMG_DIR = 'C:/Users/PML/Documents/Florentino_space/cast_iron_preprocess/img512_offset50'
LEARNING_RATE = 2e-4
IMAGE_SIZE = 512
IN_CHANNELS = 1
OUT_CHANNELS = 1
BATCH_SIZE = 8
NUM_EPOCHS = 30
LOAD_MODEL = False
SAVE_MODEL = True
NUM_WORKER = 4
CHECKPOINT = 'sur_checkpoint.pth.tar'
</code></pre>
<p>this is my utils function:</p>
<pre><code class="lang-auto">def save_checkpoint(model, optimizer, filename="my_checkpoint.pth.tar"):
    print("=&gt; Saving checkpoint")
    checkpoint = {
        "state_dict": model.state_dict(),
        "optimizer": optimizer.state_dict(),
    }
    torch.save(checkpoint, filename)
</code></pre>
<pre><code class="lang-auto">from model import AutoEncoder
import torch
from dataset import SurDataset
from utils import save_checkpoint, load_checkpoint, save_some_examples
from torch.utils.data import DataLoader
from torchvision.utils import save_image
import torch.nn as nn
import torch.optim as optim
import config
from tqdm import tqdm

model = AutoEncoder(in_channels=1, out_channels=1).to(config.DEVICE)
optimizer = optim.Adam(list(model.parameters()),
        lr = config.LEARNING_RATE,
        betas=(0.5, 0.999))
mse = nn.MSELoss()
scalar = torch.cuda.amp.GradScaler()
dataset = SurDataset(csv_dir=config.CSV_DIR, img_dir=config.IMG_DIR)
train_set, val_set = torch.utils.data.random_split(dataset, [15000, len(dataset)-15000])
loader = DataLoader(dataset=train_set, batch_size=config.BATCH_SIZE, shuffle=True)
val_loader = DataLoader(dataset=val_set, batch_size=config.BATCH_SIZE)
loop = tqdm(loader)
loop = tqdm(loader)

if config.LOAD_MODEL:
    load_checkpoint(
        config.CHECKPOINT, model, optimizer, config.LEARNING_RATE,
    )
model.train()
for epoch in range(config.NUM_EPOCHS):
    print(f"epoch {epoch+1}/{config.NUM_EPOCHS}:")
    losses = []
    for idx, (csv_, target) in enumerate(loop):
        csv_ = csv_.to(config.DEVICE)
        target = target.to(config.DEVICE)
        predict = model(csv_)
        loss = mse(predict, target)
        losses.append(loss.item())
        optimizer.zero_grad()
        loss.backward()
        # gradient descent or adam step
        optimizer.step()
        if idx % 5 == 0:
            save_some_examples(model, val_loader, epoch, folder="evaluation")
    if config.SAVE_MODEL and epoch % 5 == 0:
        save_checkpoint(model, optimizer, filename=config.CHECKPOINT)  # here come the error!

    print(f"loss at epoch {epoch+1}/{config.NUM_EPOCHS} is {losses:.4f}.")




        
</code></pre>

ptrblck: <p>The error is raised, if you try to use a wrong format identifier in a <code>str</code> as seen here:</p>
<pre><code class="lang-python">print('{:3}'.format(['aa', 'bb'])) 
&gt; TypeError: unsupported format string passed to list.__format__
</code></pre>
<p>but I’m unsure where exactly the error is coming from.<br>
Could you post a minimal executable code snippet to reproduce this issue, please?</p>

Florentino: <p>Would these error help?</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\PML\.conda\envs\floren\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\PML\.conda\envs\floren\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\PML\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy\__main__.py", line 45, in &lt;module&gt;   
    cli.main()
  File "c:\Users\PML\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, 
in main
    run()
  File "c:\Users\PML\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, 
in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\PML\.conda\envs\floren\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\PML\.conda\envs\floren\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\PML\.conda\envs\floren\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\PML\Documents\Florentino_space\cast_iron_preprocess\autoencoder20210615\train.py", line 48, in &lt;module&gt;
    print(f"loss at epoch {epoch+1}/{config.NUM_EPOCHS} is {losses:.4f}.")
TypeError: unsupported format string passed to list.__format__
</code></pre>

Florentino: <p>I know what is wrong, that I pass an array rather than a value, thank you!</p>