FlexLoggers module

FlexLogger

Module for the FlexLogger Class

class pytorchart.flexilogger.FlexLogger(plot_args, meter_args, **kwargs)[source]

Base object for logging. It takes some specifications for meters and for plots, indexes them, and adds a hook to send data to plots on log.

Parameters:
  • plot_args – dictionary of definitions for plotters or None.
  • meter_args – dictionary of definitions for meters
  • kwargs – additional keyword arguments

Examples:

meters = {
    'mymetric': {'type': 'AverageValueMeter', 'target': 'misc'}
    'test_loss': {'type': 'AverageValueMeter', 'target': 'loss'}
    'train_loss': {'type': 'AverageValueMeter', 'target': 'loss'} # target is the plot key
}
plots = {'loss': {'type': 'line'}, 'misc': {'type': 'line'}}

TM = FlexLogger(plots, meters, env='bob') # initializes plots with meters to visdom env 'bob'

# sample expirement step - for more see unittests
TM(mymetric=69, test_loss=0.94)
TM.step()

TM.log(reset=True) # log and reset
add(kwargs={})[source]

Add a dictionary of values to meters.

Parameters:kwargs
Returns:
add_metrics_for(*args, plot=None, phases=None)[source]

Adds some metrics to hte meter, with an optional target plot. If the plot already exists, they will be added there.

Parameters:
  • args – list of strings corresponding to metrics
  • plot – string - name of plot
  • phases – list of strings corresponding to phases
Returns:

Example:

# create new flexlogger
TM = FlexLogger.from_presets('acc')

# ... do stuff

# latter on, need to add some stuff to a new plot
TM.add_metrics_for('loss', 'norm_loss', 'hinge_loss', 'demorgan_loss', plot='loss', phases=['test])
add_presets(*args)[source]
Parameters:args
Returns:
classmethod from_presets(*args, phases=None)[source]

Factory method to generate a logger from some preconfigured keys. see presets.preconfigured.Config for details

Parameters:
  • args
  • phases
Returns:

instance of Flexilogger

Example:
TM = FlexLogger.from_presets('acc', 'loss')
static load(file_path)[source]
log(keys=None, reset=False, step=False)[source]

Retrieves current values of all meters, and plots at current timestep. log is used to keep familiarity with torchnet interface.

if the reset keyword is set to True, calls self.reset() if the step keyword is set to True, calls self.step()

Parameters:
  • keys – list of names of plots or None. If None, plots all keys.
  • reset – reset meters after plotting
  • step – increment counter after plotting
Returns:

None

Example:
reset(keys=None)[source]

Resets all of own meters. If keys kwd is specified, only resets the meters with those key names.

Parameters:keys – list[str] list of keys which will be reset.
Returns:None
save(file_path, plots=False)[source]

saves this object, and the visdom state if plots is True todo implement lol :return: None

show(meta=False)[source]

Implementation for __repr__ with additional functionality. __repr__ shows only meters, but show gives options to show metadata for charts and meters.

Parameters:meta – (boolean)
Returns:(string) detailed representation of self.

Example:

# create Logger
Stat = FlexLogger('loss', 'acc')
Stat.show()

Output:

Plots:
    loss
        train_loss   - AverageValueMeter : nan
        test_loss    - AverageValueMeter : nan
    acc
        test_acc     - AverageValueMeter : nan
        train_acc    - AverageValueMeter : nan
    Not plotted:
step(step=None)[source]

Increments the internal counter, or sets to value of :step arg

Parameters:step – (int) if step is specified, sets the internal counter to that step
Returns:(int) updated step count
update_config(plot_args, meter_args)[source]

Api for adding meters and plots.

Parameters:
  • plot_args
  • meter_args
Returns:

value(keys=None)[source]
Parameters:keys
Returns:
vis

property: Retrieve the Visdom() object for fun or profit

Returns:Visdom() object

ModelLogger

class pytorchart.tooledmodel.FlexTooledModel(plot_args, metrics, model=None, **kwargs)[source]

Logging a nn.Module values to a Flexlogger by adding hooks.

nn.Modules natively give a forward and backward hook, which have a function signature of

forward_hook(module, input, output) -> None

backward_hook(module, grad_in, grad_out) -> grad

This module will
  • take a set of specifications for such functions,
  • create hooks for them to nn.Module
  • connect the hooks to meters, and updated without the FlexLogger(data=xx) calls.
  • meters will per normal when using the FlexLogger.log()

Example:

clear()[source]
Returns:
classmethod generate_model_dict(model, meter_args, **kwargs)[source]

turn a bunch of dictionaries

get_handles()[source]
register_model(model, step_on_first=True)[source]

Registers a model to its hook functions. The hooks specs must already be registered in self

Parameters:model – nn.Module -
Returns:
Usage:

my_spec = { 0:{‘grad_out’: [torch.mean], ‘weights’: [torch.std]}}

model = nn.Sequential(nn.Linear(20, 10), nn.Linear(10, 3)) TM = FlexTooledModel(model, spec=my_spec)

remove_hooks()[source]

Dereferences all hooks from pytorch nn.Module

Returns:None