Repository: pytorch/ignite

Issue:
Inspect UserWarning: TrainsLogger output_handler can not log metrics value type <class 'torch.Tensor'>
## 🚀 Feature

Currently, when running pascal voc12 training with Trains, we have the following warnings:

```
/ignite/ignite/contrib/handlers/trains_logger.py:150: UserWarning: TrainsLogger output_handler can not log metrics value type <class 'torch.Tensor'>
  warnings.warn("TrainsLogger output_handler can not log " "metrics value type {}".format(type(value)))
```


Discussion:

Resolving patch:
diff --git a/ignite/contrib/handlers/trains_logger.py b/ignite/contrib/handlers/trains_logger.py
--- a/ignite/contrib/handlers/trains_logger.py
+++ b/ignite/contrib/handlers/trains_logger.py
@@ -1,3 +1,4 @@
+import numbers
 import os
 import tempfile
 import warnings
@@ -146,10 +147,15 @@ def __call__(self, engine, logger, event_name):
             )
 
         for key, value in metrics.items():
-            if isinstance(value, (float, int)):
+            if isinstance(value, numbers.Number) or isinstance(value, torch.Tensor) and value.ndimension() == 0:
                 logger.trains_logger.report_scalar(title=self.tag, series=key, iteration=global_step, value=value)
+            elif isinstance(value, torch.Tensor) and value.ndimension() == 1:
+                for i, v in enumerate(value):
+                    logger.trains_logger.report_scalar(
+                        title="{}/{}".format(self.tag, key), series=str(i), iteration=global_step, value=v.item()
+                    )
             else:
-                warnings.warn("TrainsLogger output_handler can not log " "metrics value type {}".format(type(value)))
+                warnings.warn("TrainsLogger output_handler can not log metrics value type {}".format(type(value)))
 
 
 class OptimizerParamsHandler(BaseOptimizerParamsHandler):


Test patch:
diff --git a/tests/ignite/contrib/handlers/test_trains_logger.py b/tests/ignite/contrib/handlers/test_trains_logger.py
--- a/tests/ignite/contrib/handlers/test_trains_logger.py
+++ b/tests/ignite/contrib/handlers/test_trains_logger.py
@@ -119,7 +119,7 @@ def test_output_handler_metric_names(dirname):
     mock_logger = MagicMock(spec=TrainsLogger)
     mock_logger.trains_logger = MagicMock()
 
-    with pytest.warns(UserWarning):
+    with pytest.warns(UserWarning, match=r"TrainsLogger output_handler can not log metrics value type"):
         wrapper(mock_engine, mock_logger, Events.ITERATION_STARTED)
 
     assert mock_logger.trains_logger.report_scalar.call_count == 1
@@ -147,6 +147,24 @@ def test_output_handler_metric_names(dirname):
         any_order=True,
     )
 
+    # log a torch vector
+    wrapper = OutputHandler("tag", metric_names="all")
+    mock_logger = MagicMock(spec=TrainsLogger)
+    mock_logger.trains_logger = MagicMock()
+
+    mock_engine = MagicMock()
+    vector = torch.tensor([0.1, 0.2, 0.1, 0.2, 0.33])
+    mock_engine.state = State(metrics={"vector": vector})
+    mock_engine.state.iteration = 5
+
+    wrapper(mock_engine, mock_logger, Events.ITERATION_STARTED)
+
+    assert mock_logger.trains_logger.report_scalar.call_count == 5
+    mock_logger.trains_logger.report_scalar.assert_has_calls(
+        [call(title="tag/vector", series=str(i), iteration=5, value=vector[i].item()) for i in range(5)],
+        any_order=True,
+    )
+
 
 def test_output_handler_both(dirname):