TypeError: 'numpy.float32' object is not iterable when logging in mlflow
I am trying a machine learning model and logging metrics using mlflow. But I am getting TypeError: 'numpy.float32' object is not iterable. I have tried using .tolist() and dict() but nothing seems to work.
def train(max_epochs, model, optimizer, scheduler, train_loader, valid_loader, project_name):
    best_val_loss = 100
    for epoch in range(max_epochs):
        model.train()
        running_loss = []
        tq_loader = tqdm(train_loader)
        o = {}
        for samples in tq_loader:
            optimizer.zero_grad()
            outputs, interaction_map = model(
                [samples[0].to(device), samples[1].to(device), torch.tensor(samples[2]).to(device),
                 torch.tensor(samples[3]).to(device)])
            l1_norm = torch.norm(interaction_map, p=2) * 1e-4
            loss = loss_fn(outputs, torch.tensor(samples[4]).to(device).float()) + l1_norm
            loss.backward()
            optimizer.step()
            loss = loss - l1_norm
            running_loss.append(loss.cpu().detach())
            tq_loader.set_description(
                &quot;Epoch: &quot; + str(epoch + 1) + &quot;  Training loss: &quot; + str(np.mean(np.array(running_loss))))
        model.eval()
        val_loss, mae_loss = get_metrics(model, valid_loader)
        scheduler.step(val_loss)
        
        #metrics mlflow
        mlflow.log_metrics('train_loss',(np.mean(np.array(running_loss))).tolist())
        mlflow.log_metrics('validation_loss',(val_loss).tolist())
        mlflow.log_metrics('MAE Val_loss', (mae_loss).tolist())

        print(&quot;Epoch: &quot; + str(epoch + 1) + &quot;  train_loss &quot; + str(np.mean(np.array(running_loss))) + &quot; Val_loss &quot; + str(
            val_loss) + &quot; MAE Val_loss &quot; + str(mae_loss))
        if val_loss &lt; best_val_loss:
            best_val_loss = val_loss
            torch.save(model.state_dict(), &quot;./runs/run-&quot; + str(project_name) + &quot;/models/best_model.tar&quot;)

mlflow.set_experiment('CIGIN_V2')
mlflow.start_run(nested=True)
train(max_epochs, model, optimizer, scheduler, train_loader, valid_loader, project_name)
mlflow.end_run()

Error
Epoch: 1  Training loss: 6770.575: 100%|██████████| 1/1 [00:04&lt;00:00,  4.35s/it]
100%|██████████| 1/1 [00:03&lt;00:00,  3.86s/it]

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-96-8c3a6eb822c3&gt; in &lt;module&gt;()
      1 mlflow.set_experiment('CIGIN_V2')
      2 mlflow.start_run(nested=True)
----&gt; 3 train(max_epochs, model, optimizer, scheduler, train_loader, valid_loader, project_name)
      4 mlflow.end_run()

&lt;ipython-input-95-ab0a6c80b65b&gt; in train(max_epochs, model, optimizer, scheduler, train_loader, valid_loader, project_name)
     55 
     56         #metrics mlflow
---&gt; 57         mlflow.log_metrics('train_loss',dict(np.mean(np.array(running_loss))).tolist())
     58         mlflow.log_metrics('validation_loss',dict(val_loss).tolist())
     59         mlflow.log_metrics('MAE Val_loss', dict(mae_loss).tolist())

TypeError: 'numpy.float32' object is not iterable


Youre logging a single value into log_metrics and i dont think thats correct based on the implementation of log_metric and log_metrics in the documentation:
https://www.mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_metric and
https://www.mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_metrics
So i would suggest to maybe change the &quot;log_metrics&quot; to &quot;log_metric&quot; and leave the tolist out
