Repository: RasaHQ/rasa
Pull request: Don't create TensorBoard logs during prediction
**Proposed changes**:
Don't create TensorBoard log files during prediction.

closes #5617

**Status (please check what you already did)**:
- [ ] added some tests for the functionality
- [ ] updated the documentation
- [x] updated the changelog (please check [changelog](https://github.com/RasaHQ/rasa/tree/master/changelog) for instructions)
- [x] reformat files using `black` (please check [Readme](https://github.com/RasaHQ/rasa#code-style) for instructions)


Problem statement:
Tensorboard log files are created during prediction as well
**Rasa version**: 1.9.5

**Python version**: 3.6.5

**Operating system** (windows, osx, ...): MacOS

**Issue**: Since the call to `self._set_up_tensorboard_writer(tensorboard_log_level, tensorboard_log_dir)` is made in the constructor of `RasaModel` class, these files are created during prediction time as well, for example when `rasa test` is run. 


**Command or request that led to error**: `rasa test nlu`

Hints from issue discussion:

Commit message: add changelog entry

Patch to review:
diff --git a/changelog/5617.bugfix.rst b/changelog/5617.bugfix.rst
new file mode 100644
index 000000000000..a3cf0560d2eb
--- /dev/null
+++ b/changelog/5617.bugfix.rst
@@ -0,0 +1 @@
+Don't create TensorBoard log files during prediction.
diff --git a/rasa/utils/tensorflow/models.py b/rasa/utils/tensorflow/models.py
index fd0a3f7bd57f..8a59ef813b06 100644
--- a/rasa/utils/tensorflow/models.py
+++ b/rasa/utils/tensorflow/models.py
@@ -48,37 +48,37 @@ def __init__(
 
         self.random_seed = random_seed
 
+        self.tensorboard_log_dir = tensorboard_log_dir
+        self.tensorboard_log_level = tensorboard_log_level
+
         self.train_summary_writer = None
         self.test_summary_writer = None
         self.model_summary_file = None
         self.tensorboard_log_on_epochs = True
 
-        self._set_up_tensorboard_writer(tensorboard_log_level, tensorboard_log_dir)
-
-    def _set_up_tensorboard_writer(
-        self, tensorboard_log_level: Text, tensorboard_log_dir: Optional[Text] = None
-    ) -> None:
-        if tensorboard_log_dir is not None:
-            if tensorboard_log_level not in TENSORBOARD_LOG_LEVELS:
+    def _set_up_tensorboard_writer(self) -> None:
+        if self.tensorboard_log_dir is not None:
+            if self.tensorboard_log_level not in TENSORBOARD_LOG_LEVELS:
                 raise ValueError(
-                    f"Provided '{TENSORBOARD_LOG_LEVEL}' ('{tensorboard_log_level}') "
+                    f"Provided '{TENSORBOARD_LOG_LEVEL}' ('{self.tensorboard_log_level}') "
                     f"is invalid! Valid values are: {TENSORBOARD_LOG_LEVELS}"
                 )
-
-            self.tensorboard_log_on_epochs = tensorboard_log_level == "epoch"
+            self.tensorboard_log_on_epochs = self.tensorboard_log_level == "epoch"
 
             current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
             class_name = self.__class__.__name__
 
-            train_log_dir = f"{tensorboard_log_dir}/{class_name}/{current_time}/train"
-            test_log_dir = f"{tensorboard_log_dir}/{class_name}/{current_time}/test"
+            train_log_dir = (
+                f"{self.tensorboard_log_dir}/{class_name}/{current_time}/train"
+            )
+            test_log_dir = (
+                f"{self.tensorboard_log_dir}/{class_name}/{current_time}/test"
+            )
 
             self.train_summary_writer = tf.summary.create_file_writer(train_log_dir)
             self.test_summary_writer = tf.summary.create_file_writer(test_log_dir)
 
-            self.model_summary_file = (
-                f"{tensorboard_log_dir}/{class_name}/{current_time}/model_summary.txt"
-            )
+            self.model_summary_file = f"{self.tensorboard_log_dir}/{class_name}/{current_time}/model_summary.txt"
 
     def batch_loss(
         self, batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]
@@ -103,6 +103,10 @@ def fit(
     ) -> None:
         """Fit model data"""
 
+        if not silent:
+            # don't setup tensorboard writers when training during loading
+            self._set_up_tensorboard_writer()
+
         tf.random.set_seed(self.random_seed)
         np.random.seed(self.random_seed)
 


Code review comments:
diff_hunk: @@ -103,6 +103,10 @@ def fit(
     ) -> None:
         """Fit model data"""
 
+        if not silent:
path: rasa/utils/tensorflow/models.py
text: @user1:
Why do we need this if construct? Isn't fit() called only during training?
Also, if its necessary then we should rename silent to something more meaningful?

@author:
It is also called during load, that's why I added the comment `# don't setup tensorboard writers when training during loading`. We can maybe rename it or add another parameter. 

@user1:
Ahh, right its called in load to pass through a dummy batch.
Yes, maybe adding a separate parameter for it is better.