Skip to content

Logging with tensorboard#

Logging utilities based on tensorboard`` are provided in the moduleANNarchy.extensions.tensorboard`, which must be explicitly imported:

from ANNarchy import *
from ANNarchy.extensions.tensorboard import Logger

The main object in that module is the Logger class.

ANNarchy.extensions.tensorboard.Logger #

:copyright: Copyright 2013 - now, see AUTHORS. :license: GPLv2, see LICENSE for details.

Logger #

Bases: object

Logger class to use tensorboard to visualize ANNarchy simulations. Requires the tensorboardX package (pip install tensorboardX).

The Logger class is a thin wrapper around tensorboardX.SummaryWriter, which you could also use directly. The doc is available at https://tensorboardx.readthedocs.io/. Tensorboard can read any logging data, as long as they are saved in the right format (tfevents), so it is not limited to tensorflow. TensorboardX has been developed to allow the use of tensorboard with pytorch.

The extension has to be imported explictly:

from ANNarchy.extensions.tensorboard import Logger

The Logger class has to be closed properly at the end of the script, so it is advised to use a context:

with Logger() as logger:
    logger.add_scalar("Accuracy", acc, trial)

You can also make sure to close it:

logger = Logger()
logger.add_scalar("Accuracy", acc, trial)
logger.close()

By default, the logs will be written in a subfolder of ./runs/ (which will be created in the current directory). The subfolder is a combination of the current datetime and of the hostname, e.g. ./runs/Apr22_12-11-22_machine. You can control these two elements by passing arguments to Logger():

with Logger(logdir="/tmp/annarchy", experiment="trial1"): # logs in /tmp/annarchy/trial1

The add_* methods allow you to log various structures, such as scalars, images, histograms, figures, etc.

A tag should be given to each plot. In the example above, the figure with the accuracy will be labelled "Accuracy" in tensorboard. You can also group plots together with tags such as "Global performance/Accuracy", "Global performance/Error rate", "Neural activity/Population 1", etc.

After (or while) logging data within your simulation, run tensorboard in the terminal by specifying the log directory:

tensorboard --logdir runs

TensorboardX enqueues the data in memory before writing to disk. You can force flushing with:

logger.flush()
Source code in ANNarchy/extensions/tensorboard/Logger.py
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
class Logger(object):
    """
    Logger class to use tensorboard to visualize ANNarchy simulations. Requires the `tensorboardX` package (pip install tensorboardX). 

    The Logger class is a thin wrapper around tensorboardX.SummaryWriter, which you could also use directly. The doc is available at <https://tensorboardx.readthedocs.io/>. Tensorboard can read any logging data, as long as they are saved in the right format (tfevents), so it is not limited to tensorflow. TensorboardX has been developed to allow the use of tensorboard with pytorch.

    The extension has to be imported explictly:

    ```python
    from ANNarchy.extensions.tensorboard import Logger
    ```

    The ``Logger`` class has to be closed properly at the end of the script, so it is advised to use a context:

    ```python
    with Logger() as logger:
        logger.add_scalar("Accuracy", acc, trial)
    ```

    You can also make sure to close it:

    ```python
    logger = Logger()
    logger.add_scalar("Accuracy", acc, trial)
    logger.close()
    ```

    By default, the logs will be written in a subfolder of ``./runs/`` (which will be created in the current directory). 
    The subfolder is a combination of the current datetime and of the hostname, e.g. ``./runs/Apr22_12-11-22_machine``. 
    You can control these two elements by passing arguments to ``Logger()``:

    ```python
    with Logger(logdir="/tmp/annarchy", experiment="trial1"): # logs in /tmp/annarchy/trial1
    ```

    The ``add_*`` methods allow you to log various structures, such as scalars, images, histograms, figures, etc.

    A tag should be given to each plot. In the example above, the figure with the accuracy will be labelled "Accuracy" in tensorboard. 
    You can also group plots together with tags such as "Global performance/Accuracy", "Global performance/Error rate", "Neural activity/Population 1", etc.

    After (or while) logging data within your simulation, run `tensorboard` in the terminal by specifying the log directory:

    ```bash
    tensorboard --logdir runs
    ```

    TensorboardX enqueues the data in memory before writing to disk. You can force flushing with:

    ```python
    logger.flush()
    ```

    """

    def __init__(self, logdir="runs/", experiment=None):
        """
        :param logdir: path (absolute or relative) to the logging directory. Subfolders will be created for each individual run. The default is "runs/"
        :param experiment: name of the subfolder for the current run. By default, it is a combination of the current time and the hostname (e.g. Apr22_12-11-22_machine). If you reuse an experiment name, the data will be appended.
        """
        self.logdir = logdir
        self.experiment = experiment

        # Create the logdir if it does not exist
        if not os.path.exists(self.logdir):
            os.makedirs(self.logdir)

        if not experiment:
            current_time = datetime.now().strftime('%b%d_%H-%M-%S')
            self.currentlogdir=os.path.join(
                self.logdir, current_time + '_' + socket.gethostname())
        else:
            self.currentlogdir = self.logdir + "/" + self.experiment

        print("Logging in", self.currentlogdir)

        self._create_summary_writer()

    def _create_summary_writer(self):

         self._summary = SummaryWriter(self.currentlogdir, comment="", purge_step=None, max_queue=10, flush_secs=10, filename_suffix='', write_to_disk=True)

    # Logging methods

    def add_scalar(self, tag, value, step=None):
        """
        Logs a single scalar value, e.g. a success rate at various stages of learning.

        Example:

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                accuracy = ...
                logger.add_scalar("Accuracy", accuracy, trial)
        ```

        :param tag: name of the figure in tensorboard.
        :param value: value.
        :param step: time index.
        """

        self._summary.add_scalar(tag=tag, scalar_value=value, global_step=step, walltime=None)

    def add_scalars(self, tag, value, step=None):
        """
        Logs multiple scalar values to be displayed in the same figure, e.g. several metrics or neural activities.

        Example:

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                act1 = pop.r[0]
                act2 = pop.r[1]
                logger.add_scalars(
                    "Accuracy", 
                    {'First neuron': act1, 'Second neuron': act2}, 
                    trial)
        ```

        :param tag: name of the figure in tensorboard.
        :param value: dictionary of values.
        :param step: time index.
        """

        self._summary.add_scalars(main_tag=tag, tag_scalar_dict=value, global_step=step, walltime=None)

    def add_image(self, tag, img, step=None, equalize=False):
        """
        Logs an image.

        The image must be a numpy array of size (height, width) for monochrome images or (height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter ``equalize`` forces the values to be between 0 and 1 by equalizing using the min/max values.

        Example::

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                img = pop.r.reshape((10, 10))
                logger.add_image("Population / Firing rate", img, trial, equalize=True)
        ```

        :param tag: name of the figure in tensorboard.
        :param img: array for the image.
        :param step: time index.
        :param equalize: rescales the pixels between 0 and 1 using the min and max values of the array.
        """
        if img.ndim ==2:
            if equalize:  
                img = img.astype(np.float)              
                img = (img - img.min())/(img.max() - img.min())

            self._summary.add_image(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='HW')

        elif img.ndim == 3:
            if not img.shape[2] == 3:
                Global._error("Logger.add_image: color images must be of shape (H, W, 3).")

            if equalize:   
                img = np.array(img).astype(np.float)         
                img = (img - img.min())/(img.max() - img.min())

            self._summary.add_image(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='HWC')

        else:
            Global._error("Logger.add_image: images must be of shape (H, W) or (H, W, 3).")

    def add_images(self, tag, img, step=None, equalize=False, equalize_per_image=False):
        """
        Logs a set of images (e.g. receptive fields).

        The numpy array must be of size (number, height, width) for monochrome images or (number, height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter ``equalize`` forces the values to be between 0 and 1 by equalizing using the min/max values.

        Example:

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                weights= proj.w.reshape(100, 10, 10) # 100 post neurons, 10*10 pre neurons
                logger.add_images("Projection/Receptive fields", weights, trial, equalize=True)
        ```

        :param tag: name of the figure in tensorboard.
        :param img: array for the images.
        :param step: time index.
        :param equalize: rescales the pixels between 0 and 1 using the min and max values of the array.
        :param equalize_per_image: whether the rescaling should be using the global min/max values of the array, or per image. Has no effect if equalize of False.

        """
        if img.ndim == 3:
            img = np.expand_dims(img, axis=3)

        if equalize:   
            img = np.array(img).astype(np.float) 
            if not equalize_per_image:        
                img = (img - img.min())/(img.max() - img.min())
            else:
                for i in range(img.shape[0]):
                    img[i,...] = (img[i,...] - img[i,...].min())/(img[i,...].max() - img[i,...].min())

        self._summary.add_images(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='NHWC')

    def add_parameters(self, params, metrics):
        """
        Logs parameters of a simulation.

        This should be run only once per simulation, generally at the end. 
        This allows to compare different runs of the same network using 
        different parameter values and study how they influence the global output metrics, 
        such as accuracy, error rate, reaction speed, etc.

        Example:

        ```python
        with Logger() as logger:
            # ...
            logger.add_parameters({'learning_rate': lr, 'tau': tau}, {'accuracy': accuracy})
        ```

        :param params: dictionary of parameters.
        :param metrics: dictionary of metrics.
        """

        self._summary.add_hparams(params, metrics)

    def add_histogram(self, tag, hist, step=None):
        """
        Logs an histogram.

        Example:

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                weights= proj.w.flatten()
                logger.add_histogram("Weight distribution", weights, trial)
        ```


        :param tag: name of the figure in tensorboard.
        :param hist: a list or 1D numpy array of values.
        :param step: time index.
        """

        self._summary.add_histogram(tag, hist, step)

    def add_figure(self, tag, figure, step=None, close=True):
        """
        Logs a Matplotlib figure.

        Example:

        ```python
        with Logger() as logger:
            for trial in range(100):
                simulate(1000.0)
                fig = plt.figure()
                plt.plot(pop.r)
                logger.add_figure("Activity", fig, trial)
        ```

        :param tag: name of the image in tensorboard.
        :param figure: a list or 1D numpy array of values.
        :param step: time index.
        :param close: whether the logger will close the figure when done (default: True).
        """

        import matplotlib.pyplot as plt
        import matplotlib.backends.backend_agg as plt_backend_agg
        canvas = plt_backend_agg.FigureCanvasAgg(figure)
        canvas.draw()
        data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
        w, h = figure.canvas.get_width_height()
        image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
        image_chw = np.moveaxis(image_hwc, source=2, destination=0)
        if close:
            plt.close(figure)
        self._summary.add_image(tag, image_chw, step)

    # Resource management
    def flush(self):
        "Forces the logged data to be flushed to disk."
        self._summary.flush()

    def close(self):
        "Closes the logger."
        self._summary.close()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

__init__(logdir='runs/', experiment=None) #

Parameters:

  • logdir

    path (absolute or relative) to the logging directory. Subfolders will be created for each individual run. The default is "runs/"

  • experiment

    name of the subfolder for the current run. By default, it is a combination of the current time and the hostname (e.g. Apr22_12-11-22_machine). If you reuse an experiment name, the data will be appended.

Source code in ANNarchy/extensions/tensorboard/Logger.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def __init__(self, logdir="runs/", experiment=None):
    """
    :param logdir: path (absolute or relative) to the logging directory. Subfolders will be created for each individual run. The default is "runs/"
    :param experiment: name of the subfolder for the current run. By default, it is a combination of the current time and the hostname (e.g. Apr22_12-11-22_machine). If you reuse an experiment name, the data will be appended.
    """
    self.logdir = logdir
    self.experiment = experiment

    # Create the logdir if it does not exist
    if not os.path.exists(self.logdir):
        os.makedirs(self.logdir)

    if not experiment:
        current_time = datetime.now().strftime('%b%d_%H-%M-%S')
        self.currentlogdir=os.path.join(
            self.logdir, current_time + '_' + socket.gethostname())
    else:
        self.currentlogdir = self.logdir + "/" + self.experiment

    print("Logging in", self.currentlogdir)

    self._create_summary_writer()

add_figure(tag, figure, step=None, close=True) #

Logs a Matplotlib figure.

Example:

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        fig = plt.figure()
        plt.plot(pop.r)
        logger.add_figure("Activity", fig, trial)

Parameters:

  • tag

    name of the image in tensorboard.

  • figure

    a list or 1D numpy array of values.

  • step

    time index.

  • close

    whether the logger will close the figure when done (default: True).

Source code in ANNarchy/extensions/tensorboard/Logger.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def add_figure(self, tag, figure, step=None, close=True):
    """
    Logs a Matplotlib figure.

    Example:

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            fig = plt.figure()
            plt.plot(pop.r)
            logger.add_figure("Activity", fig, trial)
    ```

    :param tag: name of the image in tensorboard.
    :param figure: a list or 1D numpy array of values.
    :param step: time index.
    :param close: whether the logger will close the figure when done (default: True).
    """

    import matplotlib.pyplot as plt
    import matplotlib.backends.backend_agg as plt_backend_agg
    canvas = plt_backend_agg.FigureCanvasAgg(figure)
    canvas.draw()
    data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
    w, h = figure.canvas.get_width_height()
    image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
    image_chw = np.moveaxis(image_hwc, source=2, destination=0)
    if close:
        plt.close(figure)
    self._summary.add_image(tag, image_chw, step)

add_histogram(tag, hist, step=None) #

Logs an histogram.

Example:

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        weights= proj.w.flatten()
        logger.add_histogram("Weight distribution", weights, trial)

Parameters:

  • tag

    name of the figure in tensorboard.

  • hist

    a list or 1D numpy array of values.

  • step

    time index.

Source code in ANNarchy/extensions/tensorboard/Logger.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
def add_histogram(self, tag, hist, step=None):
    """
    Logs an histogram.

    Example:

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            weights= proj.w.flatten()
            logger.add_histogram("Weight distribution", weights, trial)
    ```


    :param tag: name of the figure in tensorboard.
    :param hist: a list or 1D numpy array of values.
    :param step: time index.
    """

    self._summary.add_histogram(tag, hist, step)

add_image(tag, img, step=None, equalize=False) #

Logs an image.

The image must be a numpy array of size (height, width) for monochrome images or (height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter equalize forces the values to be between 0 and 1 by equalizing using the min/max values.

Example::

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        img = pop.r.reshape((10, 10))
        logger.add_image("Population / Firing rate", img, trial, equalize=True)

Parameters:

  • tag

    name of the figure in tensorboard.

  • img

    array for the image.

  • step

    time index.

  • equalize

    rescales the pixels between 0 and 1 using the min and max values of the array.

Source code in ANNarchy/extensions/tensorboard/Logger.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def add_image(self, tag, img, step=None, equalize=False):
    """
    Logs an image.

    The image must be a numpy array of size (height, width) for monochrome images or (height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter ``equalize`` forces the values to be between 0 and 1 by equalizing using the min/max values.

    Example::

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            img = pop.r.reshape((10, 10))
            logger.add_image("Population / Firing rate", img, trial, equalize=True)
    ```

    :param tag: name of the figure in tensorboard.
    :param img: array for the image.
    :param step: time index.
    :param equalize: rescales the pixels between 0 and 1 using the min and max values of the array.
    """
    if img.ndim ==2:
        if equalize:  
            img = img.astype(np.float)              
            img = (img - img.min())/(img.max() - img.min())

        self._summary.add_image(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='HW')

    elif img.ndim == 3:
        if not img.shape[2] == 3:
            Global._error("Logger.add_image: color images must be of shape (H, W, 3).")

        if equalize:   
            img = np.array(img).astype(np.float)         
            img = (img - img.min())/(img.max() - img.min())

        self._summary.add_image(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='HWC')

    else:
        Global._error("Logger.add_image: images must be of shape (H, W) or (H, W, 3).")

add_images(tag, img, step=None, equalize=False, equalize_per_image=False) #

Logs a set of images (e.g. receptive fields).

The numpy array must be of size (number, height, width) for monochrome images or (number, height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter equalize forces the values to be between 0 and 1 by equalizing using the min/max values.

Example:

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        weights= proj.w.reshape(100, 10, 10) # 100 post neurons, 10*10 pre neurons
        logger.add_images("Projection/Receptive fields", weights, trial, equalize=True)

Parameters:

  • tag

    name of the figure in tensorboard.

  • img

    array for the images.

  • step

    time index.

  • equalize

    rescales the pixels between 0 and 1 using the min and max values of the array.

  • equalize_per_image

    whether the rescaling should be using the global min/max values of the array, or per image. Has no effect if equalize of False.

Source code in ANNarchy/extensions/tensorboard/Logger.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def add_images(self, tag, img, step=None, equalize=False, equalize_per_image=False):
    """
    Logs a set of images (e.g. receptive fields).

    The numpy array must be of size (number, height, width) for monochrome images or (number, height, width, 3) for colored images. The values should either be integers between 0 and 255 or floats between 0 and 1. The parameter ``equalize`` forces the values to be between 0 and 1 by equalizing using the min/max values.

    Example:

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            weights= proj.w.reshape(100, 10, 10) # 100 post neurons, 10*10 pre neurons
            logger.add_images("Projection/Receptive fields", weights, trial, equalize=True)
    ```

    :param tag: name of the figure in tensorboard.
    :param img: array for the images.
    :param step: time index.
    :param equalize: rescales the pixels between 0 and 1 using the min and max values of the array.
    :param equalize_per_image: whether the rescaling should be using the global min/max values of the array, or per image. Has no effect if equalize of False.

    """
    if img.ndim == 3:
        img = np.expand_dims(img, axis=3)

    if equalize:   
        img = np.array(img).astype(np.float) 
        if not equalize_per_image:        
            img = (img - img.min())/(img.max() - img.min())
        else:
            for i in range(img.shape[0]):
                img[i,...] = (img[i,...] - img[i,...].min())/(img[i,...].max() - img[i,...].min())

    self._summary.add_images(tag=tag, img_tensor=img, global_step=step, walltime=None, dataformats='NHWC')

add_parameters(params, metrics) #

Logs parameters of a simulation.

This should be run only once per simulation, generally at the end. This allows to compare different runs of the same network using different parameter values and study how they influence the global output metrics, such as accuracy, error rate, reaction speed, etc.

Example:

with Logger() as logger:
    # ...
    logger.add_parameters({'learning_rate': lr, 'tau': tau}, {'accuracy': accuracy})

Parameters:

  • params

    dictionary of parameters.

  • metrics

    dictionary of metrics.

Source code in ANNarchy/extensions/tensorboard/Logger.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def add_parameters(self, params, metrics):
    """
    Logs parameters of a simulation.

    This should be run only once per simulation, generally at the end. 
    This allows to compare different runs of the same network using 
    different parameter values and study how they influence the global output metrics, 
    such as accuracy, error rate, reaction speed, etc.

    Example:

    ```python
    with Logger() as logger:
        # ...
        logger.add_parameters({'learning_rate': lr, 'tau': tau}, {'accuracy': accuracy})
    ```

    :param params: dictionary of parameters.
    :param metrics: dictionary of metrics.
    """

    self._summary.add_hparams(params, metrics)

add_scalar(tag, value, step=None) #

Logs a single scalar value, e.g. a success rate at various stages of learning.

Example:

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        accuracy = ...
        logger.add_scalar("Accuracy", accuracy, trial)

Parameters:

  • tag

    name of the figure in tensorboard.

  • value

    value.

  • step

    time index.

Source code in ANNarchy/extensions/tensorboard/Logger.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def add_scalar(self, tag, value, step=None):
    """
    Logs a single scalar value, e.g. a success rate at various stages of learning.

    Example:

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            accuracy = ...
            logger.add_scalar("Accuracy", accuracy, trial)
    ```

    :param tag: name of the figure in tensorboard.
    :param value: value.
    :param step: time index.
    """

    self._summary.add_scalar(tag=tag, scalar_value=value, global_step=step, walltime=None)

add_scalars(tag, value, step=None) #

Logs multiple scalar values to be displayed in the same figure, e.g. several metrics or neural activities.

Example:

with Logger() as logger:
    for trial in range(100):
        simulate(1000.0)
        act1 = pop.r[0]
        act2 = pop.r[1]
        logger.add_scalars(
            "Accuracy", 
            {'First neuron': act1, 'Second neuron': act2}, 
            trial)

Parameters:

  • tag

    name of the figure in tensorboard.

  • value

    dictionary of values.

  • step

    time index.

Source code in ANNarchy/extensions/tensorboard/Logger.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def add_scalars(self, tag, value, step=None):
    """
    Logs multiple scalar values to be displayed in the same figure, e.g. several metrics or neural activities.

    Example:

    ```python
    with Logger() as logger:
        for trial in range(100):
            simulate(1000.0)
            act1 = pop.r[0]
            act2 = pop.r[1]
            logger.add_scalars(
                "Accuracy", 
                {'First neuron': act1, 'Second neuron': act2}, 
                trial)
    ```

    :param tag: name of the figure in tensorboard.
    :param value: dictionary of values.
    :param step: time index.
    """

    self._summary.add_scalars(main_tag=tag, tag_scalar_dict=value, global_step=step, walltime=None)

close() #

Closes the logger.

Source code in ANNarchy/extensions/tensorboard/Logger.py
308
309
310
def close(self):
    "Closes the logger."
    self._summary.close()

flush() #

Forces the logged data to be flushed to disk.

Source code in ANNarchy/extensions/tensorboard/Logger.py
304
305
306
def flush(self):
    "Forces the logged data to be flushed to disk."
    self._summary.flush()