Welcome to pytorchart’s documentation!

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

Loggers package

TraceLogger Class

class pytorchart.Loggers.tracelogger.TraceLogger(*args, opts={}, vis=None, legend=[], env=None, port=8097, **kwargs)[source]

Logging arbitrary data by keys at runtime to a metering operation.

TraceLogger has the same idea as tnt.VisdomLogger, but with more access to plotly properties directly, with the downside of being less flexible. Also, it bypasses the visdom.scatter interface, and directly sends json data, so a few things which are rather difficult to do like specifing lines with a certain color and line style.

Though this is not meant to be used directly, it can be by all means:

Examples:
log(X, Y)[source]

Same interface as torchnet.log(), applies metadata to the X,Y Values, and sends them to the visdom plot.

Parameters:
  • X – list of integers - X-axis values of size self.num_lines
  • Y – list of integers - X-axis values of size self.num_lines
Returns:

None

save(path)[source]

saves self, and saves the visdom enviornment.

Parameters:path – valid filepath
Returns:None
viz

retrieves the Visdom Object

Returns:visom.Visdom Object

pytorchart.moremeters package

pytorchart.moremeters.bestmeter module

class pytorchart.moremeters.bestmeter.BestValueMeter(metric=None, top=True)[source]

Keep track of best value for a metric. Compares metrics and discards wrose value

Options:-if top=True, then max(new, current) is kept -if top=False, then min(new, current) is kept
add(value, m=None)[source]
reset()[source]
value()[source]

pytorchart.moremeters.image_meter module

class pytorchart.moremeters.image_meter.ImageMeter(size=50, nrow=4)[source]
add(image)[source]
reset()[source]
value()[source]

Module contents

pytorchart.presets package

Submodules

pytorchart.presets.functional module

class pytorchart.presets.functional.LayerLegend[source]

Bases: object

keeps track of layers and assigned Colors

color_for(layer)[source]
register_model(model, fn=<function <lambda>>)[source]
pytorchart.presets.functional.gen_grad_wrt_w(fn)[source]
pytorchart.presets.functional.gen_module_wght(fn)[source]
pytorchart.presets.functional.generate_layers(model, colors=None, fn=None, targets=[])[source]
Parameters:
  • model
  • colors
  • fn
  • targets
Returns:

pytorchart.presets.functional.grad_mean(module, grad_in, grad_out)[source]
pytorchart.presets.functional.grad_norm(module, grad_in, grad_out)[source]
pytorchart.presets.functional.grad_norm_mean(module, grad_in, grad_out)[source]
pytorchart.presets.functional.grad_std(module, grad_in, grad_out)[source]
pytorchart.presets.functional.has_grad(name, module_type)[source]
pytorchart.presets.functional.summarize(model, fn=<function _identity>)[source]

Given a nn.Module, recursively walk its submodules and apply a function

Parameters:
  • model – nn.Module or superclass thereof
  • fn – function to be applied to any module to create a summary
Returns:

list of summaries generated by fn(module)

pytorchart.presets.preconfigured module

class pytorchart.presets.preconfigured.Config[source]

Bases: object

Static object for generating meter and plot configurations

classmethod default_cfg()[source]

return a the base configuration for plot and meter :return: dict

Usage:

Config.default_cfg()

classmethod gen_plot(*keys, phases=None, plot=None)[source]

Creates a plot from the specified keys.

Parameters:
  • keys – list of strings
  • phases – list of strings or None. If None, it will use default

phases of [‘test’, [‘train’] :param plot: name of plot

Returns:tuple of dicts for plots and meters
classmethod get_presets(*keys, phases=None)[source]

generate a dictionary of plots and meters given preconfigured keys. This will create a meter by key by phase.

Parameters:
  • keys – list of strings
  • phases – list of strings or None. If None, it will use default

phases of [‘test’, [‘train’]

Returns:tuple of dicts for plots and meters
pytorchart.presets.preconfigured.get_meters(*keys, phases=['train', 'test'])[source]
pytorchart.presets.preconfigured.preset_names()[source]

Get a list of configured plots

Returns:list of strings

Module contents

Indices and tables