simple_hierarchy.hierarchal_model

A module for a hierarchal model in pytorch.

class simple_hierarchy.hierarchal_model.HierarchalModel(hierarchy, size=None, output_order=None, base_model=None, model=None, k=0, dim_to_concat=None, feed_from=None, join_layers=None)[source]

Bases: torch.nn.modules.module.Module

Creates a model for hierarchal class structures.

Creates a model that is designed to handle hierarchal classes. It is targeted towards image hierarchal classification problems, but can be used for any finite hierarchy and network. The concept is to work for classes where the certain classes are children of other classes. For example, consider classifying cities and districts. The districts of city are depedent on the city classifcation. The network architecure is quite simple in this class’s solution, nsimply take the ouput of each parent and feed it into the last k layers of all its children. In other words, k is a hyperparemeter that illusrates how many layers should be distinct for each class. If any of the arguments are confusing, the examples should help indicate how to use this class.

Parameters
  • hierarchy (Dict[Tuple[str, int], List[Tuple[str, int]]]) – A defined hierarchy through a dictionary definition. This is defined as names for each grouping (arbitrary but distinct names are fine) and the number of classes within each group. The format is (name, n_classes) for each tuple. The dictionary defines the relationship between childrena and parents where hierarchy[parent] is a list of children in the same format as the parent which is defined above.

  • size (Optional[Tuple[int, …]]) – A tuple of the (ouput size of the base_model or model[len(model) - k - 1], input size of model[len(model) - k], output size of model[len(model) - 1]. If feed_from is given then a fourth size must be given of the size of the feed_from index output size. If join_layers parameter is provided then this size parameter is unneeded.

  • output_order (Optional[List]) – The output order of the classes returned by forward by their tupled keys in the hierarchy dictionary.

  • base_model (Optional[Module]) – A torch network that will serve as the base model.

  • model (Optional[ModuleList]) – A torch module list that will be considered the list of network layers. If both base_model and this list are provided then base_model is used as base with this being considred for the latter layers (depending on k).

  • k (int) – A integer representing the number of last layers that are distinct.

  • dim_to_concat (Optional[int]) – The dimension to combine parent ouput and the base model’s ouput. Typically this is 1.

  • feed_from (Optional[int]) – The reverse index to feed outputs from parent classes to their children. If not given, the last layer is used. For example, if 1 is provided than the output from the layer before the final layer is forwarded from parent to child. This output enters the child at the layers that are indepedent (determined by the k and model paramaters). feed_from must be less than k.

  • join_layers (Optional[Dict[Tuple[str, int], List[Module]]]) – A dictionary with same keys as hierarchy dict. Each key contains a list of layers to join connections for that class. This is because each network “section” per class is join togther with layers to manage different sizes for different classes (since sizes are depedenet are on parent sizes). Without it being supplied, two linear layers are used per class. The first joins parent outputs into a child’s inputs. The second ouputs into number of classes for child. If join_layers is provided then size is unnecessary.

base_model

The portion of the model that is the same for all classes.

feed_from

The index to feed outputs from parent. classes to their children. If not given, the last layer is used.

last_layers

A dict of the last layers of the model. Since each class has its own last layers, a dictionary is used. The dictionary’s keys are the class tuples (name followed by number of classes), and the values are the layers for each of those classes.

tree

The tree of the class hierarchies.

output_order

The output order of the classes returned by forward by their tupled keys in the hierarchy dictionary.

dim_to_concat

The dimension to combine parent ouput and the base model’s ouput. Typically this is 1.

Examples

>>> import torch.nn as nn
>>> import torch
>>> from simple_hierarchy.hierarchal_model import HierarchalModel
>>> hierarchy = {("A", 2) : [("B", 5), ("C", 7)], ("H", 2) :
>>>     [("A", 2), ("K", 7), ("L", 10)]}
>>> base_m = nn.ModuleList([nn.Linear(10, 10) for i in range(2)])
>>> model = HierarchalModel(model=base_m, k=1, hierarchy=hierarchy, size=(10,10,10))
>>> len(model(torch.rand(10,10)))
6
>>> model.tree
H 2 [A 2 [B 5 [], C 7 []], K 7 [], L 10 []]
forward(x)[source]

Overriding forward method for pytorch nn.Module.

Return type

Tuple[Tensor, …]

training: bool
class simple_hierarchy.hierarchal_model.SimpleHierarchalModel(hierarchy, base_model, size=None, output_order=None, dim_to_concat=None)[source]

Bases: torch.nn.modules.module.Module

Creates a simple model for hierarchal class structures.

Creates a model that is designed to handle hierarchal classes. It is targeted towards image hierarchal classification problems, but can be used for any finite hierarchy and network. The concept is to work for classes where the certain classes are children of other classes. For example, consider classifying cities and districts. The districts of city are depedent on the city classifcation. The network architecure is quite simple in this class’s solution, nsimply take the ouput of each parent and feed it into the last k layers of all its children. In other words, k is a hyperparemeter that illusrates how many layers should be distinct for each class. If any of the arguments are confusing, the examples should help indicate how to use this class.

Parameters
  • hierarchy (Dict[Tuple[str, int], List[Tuple[str, int]]]) – A defined hierarchy through a dictionary definition. This is defined as names for each grouping (arbitrary but distinct names are fine) and the number of classes within each group. The format is (name, n_classes) for each tuple. The dictionary defines the relationship between childrena and parents where hierarchy[parent] is a list of children in the same format as the parent which is defined above.

  • size (Optional[Tuple[int, …]]) – A tuple of the (ouput size of the base_model or model[len(model) - k - 1], input size of model[len(model) - k], output size of model[len(model) - 1]. If feed_from is given then a fourth size must be given of the size of the feed_from index output size. If join_layers parameter is provided then this size parameter is unneeded.

  • output_order (Optional[List]) – The output order of the classes returned by forward by their tupled keys in the hierarchy dictionary.

  • base_model (Module) – A torch network that will serve as the base model.

  • dim_to_concat (Optional[int]) – The dimension to combine parent ouput and the base model’s ouput. Typically this is 1.

base_model

The portion of the model that is the same for all classes.

feed_from

The index to feed outputs from parent. classes to their children. If not given, the last layer is used.

last_layers

A dict of the last layers of the model. Since each class has its own last layers, a dictionary is used. The dictionary’s keys are the class tuples (name followed by number of classes), and the values are the layers for each of those classes.

tree

The tree of the class hierarchies.

output_order

The output order of the classes returned by forward by their tupled keys in the hierarchy dictionary.

dim_to_concat

The dimension to combine parent ouput and the base model’s ouput. Typically this is 1.

Examples

>>> import torch.nn as nn
>>> import torch
>>> from simple_hierarchy.hierarchal_model import SimpleHierarchalModel
>>> hierarchy = {("A", 2): [("B", 3), ("C", 5)], ("B", 3): [("D", 3)]}
>>> base_m = nn.ModuleList([nn.Linear(10, 10) for i in range(4)])
>>> model_b = nn.Sequential(*base_m)
>>> model = SimpleHierarchalModel(base_model=model_b, hierarchy=hierarchy,
>>>     size=(10,10,10))
>>> len(model(torch.rand(10,10)))
4
>>> model.tree
A 2 [B 3 [D 3 []], C 5 []]
forward(x)[source]

Overriding forward method for pytorch nn.Module.

Return type

Tuple[Tensor, …]

training: bool