genos


Namegenos JSON
Version 0.1.12 PyPI version JSON
download
home_pagehttps://github.com/Neural-Space/genos
SummaryInstantiate objects and call functions using dictionary configs in Python using Genos.
upload_time2023-06-19 11:13:53
maintainer
docs_urlNone
authorKushal Jain
requires_python>=3.7, <4
licenseMIT
keywords instantiation objects recursive instantiation functio call config instantiate
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            <p align="center"><img src="https://docs.google.com/drawings/d/e/2PACX-1vQ65pCWYymvOtQXCUSsWfq0xeaE6fFmQ-_QT003eZRbeLiwKoE7xvDe6fCeuBx_ha7aCjpN3mu_WLl9/pub?w=1536&h=480" alt="logo" width="70%" /></p>  
<p align="center">
  <a href="https://app.circleci.com/pipelines/github/Neural-Space">
    <img src="https://circleci.com/gh/Neural-Space/genos.svg?style=shield&circle-token=3658f580f8183f441023a1a4234716410bd74338" alt="CircleCI" />
  </a>
  <a href="https://lgtm.com/projects/g/Neural-Space/genos/alerts/">
    <img src="https://img.shields.io/lgtm/alerts/g/Neural-Space/genos.svg?logo=lgtm&logoWidth=18" alt="Total alerts" />
  </a>
  <a href="https://lgtm.com/projects/g/Neural-Space/genos/context:python">
    <img src="https://img.shields.io/lgtm/grade/python/g/Neural-Space/genos.svg?logo=lgtm&logoWidth=18" alt="Language grade: Python" />
  </a>
  <a href="https://opensource.org/licenses/MIT">
    <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="mit" />
  </a>
</p>

--------------------------------------



Instantiate objects and call functions using dictionary configs in Python using Genos. 
This package was originally developed to help python developers in making configurable software components. 

While [Hydra](https://github.com/facebookresearch/hydra) lets you instantiate objects and functions, it doesn't support recursive instantiation. 
Plus, Hydra is mostly used for config management. 
So, we decided to build Genos by referring to Hydra and added the functionality of recursive instantiation. E.g.,

**Install Genos**
```bash
pip install genos
```

Instantiate a Python Class using Genos

```python
from genos import recursive_instantiate

ned = {
    "cls": "genos.examples.King",
    "params":{
        "name": "Eddard Stark",
        "queen": "Catelyn Stark",
        "allegiance": "Robert Baratheon"
    }
}

obj = recursive_instantiate(ned)
print(obj)
```   

# Contributors

[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/0)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/0)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/1)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/1)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/2)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/2)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/3)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/3)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/4)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/4)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/5)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/5)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/6)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/6)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/7)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/7)

# Install

### Pip
```bash
pip install genos
```

### Poetry
```bash
poetry add genos
```

# Basic Usage

The following examples will show how this library can make your life easier by letting you instantiate python objects from dictionaries.
First, let's consider a basic example where we simply instantiate a single class. 

### Single Class Instantiation
```python
class King:
    def __init__(self, name:str, queen:str, allegiance:str):
        self.name = name
        self.queen = queen
        self.allegiance = allegiance

    def __repr__(self):
        return f"Name:{self.name}\nQueen:{self.queen}\nAllegiance:{self.allegiance}"
        
    def print_name(self):
        print(self.name)
```

We need to pass 3 parameters to instantiate this class. 
Note that these classes are located in the `genos.examples.*` subpackage. 
So, let's say we wish to instantiate a `King` object for Eddard Stark because, afterall, _Winter is coming._

```python
from genos import recursive_instantiate

ned = {
    "cls": "genos.examples.King",
    "params":{
        "name": "Eddard Stark",
        "queen": "Catelyn Stark",
        "allegiance": "Robert Baratheon"
    }
}

obj = recursive_instantiate(ned)
print(obj)
# Name:Eddard Stark
# Queen:Catelyn Stark
# Allegiance:Robert Baratheon
```

### Recursive Class Instantiation

Well, this seemed quite simple. 
But rarely are things so simple in life. 
Consider another class that takes an instance of `King` as a parameter.

```python
class House:
    def __init__(self, king:King, home:str, sigil:str):
        self.king = king
        self.home = home
        self.sigil = sigil

    def __repr__(self):
        return f"King:{self.king.name}\nHome:{self.home}\nSigil:{self.sigil}"

```

This is where recursive instantiation comes into action. 
To initialize an object for this class, we can very easily create a nested dictionary and pass it to our magic method. 
Of course, we'll be instantiating an object for House Stark.
```python
from genos import recursive_instantiate

stark = {
    "cls": "genos.examples.House",
    "params": {
        "king":{
            "cls": "genos.examples.King",
            "params":{
                "name": "Eddard Stark",
                "queen": "Catelyn Stark",
                "allegiance": "Robert Baratheon"
                }
        },
        "home":"Winterfell",
        "sigil":"Direwolf"
    }
}

obj = recursive_instantiate(stark)
print(obj)
# output
# King:Eddard Stark
# Home:Winterfell
# Sigil:Direwolf
```

### Instantiation Using Positional Arguments  
The examples shown above always use keyword arguments to instantiate the classes. But we can also choose to simply pass in the positional arguments as shown below.  

```python
from genos import recursive_instantiate

stark = {
    "cls": "genos.examples.House",
    "args": [
        {
            "cls": "genos.examples.King",
            "params":{
                "name": "Eddard Stark",
                "queen": "Catelyn Stark",
                "allegiance": "Robert Baratheon"
                }
        },
        "Winterfell",
        "Direwolf"
    ]
}

obj = recursive_instantiate(stark)
print(obj)
# output
# King:Eddard Stark
# Home:Winterfell
# Sigil:Direwolf
```

### Instantiation Using Positional and Keyword Arguments
The following example makes use of both positional and keyword arguments together to instantiate the `House` class. We do not pass the keyword for the `king` parameter but we do so for the following parameters:`home` and `sigil`.
```python
from genos import recursive_instantiate

stark = {
    "cls": "genos.examples.House",
    "args": [
        {
            "cls": "genos.examples.King",
            "params":{
                "name": "Eddard Stark",
                "queen": "Catelyn Stark",
                "allegiance": "Robert Baratheon"
                }
        }
    ],
    "params": {
        "home":"Winterfell",
        "sigil":"Direwolf"
    }
}

obj = recursive_instantiate(stark)
print(obj)
# output
# King:Eddard Stark
# Home:Winterfell
# Sigil:Direwolf
```

### Call A Function
Just like we classes, we can also instantiate functions by calling `recursive_instantiate`. The following example shows how we can instantiate and call a simple `multiply` function using `genos`.
```python
from genos import recursive_instantiate

function_call = {
    "cls": "genos.examples.multiply",
    "args": [12, 1.3]
}

result = recursive_instantiate(function_call)
print(result)
# output
# 15.600000000000001
```

# Advanced Usage

### Deep Learning Example using PyTorch

For running the following examples you will need to install `Pytorch`.

```shell script
pip install torch
```

Such workflows where we need to instantiate multiple classes recursively is more evident in Deep Learning and related fields. 
NeuralSpace has been actively working in this space, building tools for Natural Language Processing (NLP). We have created this tool to make things easier for us. 
The following example shows a scenario where you need different components/modules to create your own custom neural network for some specific task. The individual classes are merely wrappers around `PyTorch` functions. Let's get started.

The following example classes can be found in `genos.examples.complex_examples.py`.

```python
from torch import nn

class ActivationLayer(nn.Module):
    '''
    Gives two choices for activation function: ReLU or Tanh.
    Introduces non-linearity in neural-networks. Usually applied 
    after an affine transformation.
    '''
    def __init__(self, activation:str):
        super().__init__()
        
        if activation == 'relu':
            self.activation = nn.ReLU()
        else:
            self.activation = nn.Tanh()
    
    def forward(self, x):
        return self.activation(x)
    

class AffineLayer(nn.Module):
    '''
    Performs an affine transformation on the input tensor.
    For an input tensor "x", the output is W.x + b, where W 
    is a trainable weight matrix and b is the bias vector.
    '''
    def __init__(self, in_features:int, out_features:int, activation: ActivationLayer):
        super().__init__()
        self.transform = nn.Linear(in_features=in_features, out_features=out_features)
        self.activation = activation
    
    def forward(self, x):
        return self.activation(self.transform(x))
        
class LSTMLayer(nn.Module):
    '''
    A wrapper over LSTM layer.
    '''
    def __init__(self, input_size, hidden_size, batch_first, dropout):
        super().__init__()
        self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=batch_first)
        self.dropout = nn.Dropout(dropout)
    
    def forward(self, x):
        output, _ = self.lstm(x)
        return self.dropout(output)        
```
The three classes above will now be used to create a custom neural network. 
Note carefully that in order to instantiate an `AffineLayer`, we need to pass an object of `ActivationLayer`. 
The `CustomModel` will comprise of two components: `AffineLayer` and `LSTMLayer`.

```python

class CustomModel(nn.Module):
    
    def __init__(self, affine_layer:AffineLayer, lstm_layer:LSTMLayer):
        super().__init__()
        self.affine_layer = affine_layer
        self.lstm_layer = lstm_layer
    
    def forward(self, x):
        return self.affine_layer(self.lstm_layer(x))
```

The instantiation of this class using genos will be as follows.

```python
from genos import recursive_instantiate

custom_obj = \
{
    "cls": "genos.examples.CustomModel",
    "params": {
        "affine_layer": {
            {
                "cls": "examples.example.AffineLayer",
                "params": {
                    "in_features": 256,
                    "out_features": 256,
                    "activation": {
                        "cls": "genos.examples.ActivationLayer",
                        "params": {
                            "activation": "relu"
                        }
                    }
                }
            },
            {
                "cls": "genos.examples.LSTMLayer",
                "params":{
                    "input_size": 256,
                    "hidden_size":256,
                    "batch_first":True,
                }
            }
        }
        
    }
}


model = recursive_instantiate(custom_obj)
x = torch.randn(32, 100, 256)
out = model(x)
print(out.shape)
# [32, 100, 256]
```

### Get Class Reference from Class Path
If you do not wish to use `genos` for instantiating your functions or classes, you can still use it to find and load different classes within your project structure. The following example shows the usage of `get_class()` function from `genos` to locate the `King` class.
```python
from genos import get_class

class_path = "genos.examples.King"
class_reference = get_class(class_path)

eddard_stark = class_reference(name="Eddard Stark", queen="Catelyn Stark", 
                               allegiance="Robert Baratheon")
print(eddard_stark)
# Name:Eddard Stark
# Queen:Catelyn Stark
# Allegiance:Robert Baratheon
```


### Get Function Reference from Class Path
Similar to the `get_class()` method above, you can also use `get_method()` function from `genos` to find functions in your project structure and instantiate them normally.
```python
from genos import get_method

method_path = "genos.examples.multiply"
method_reference = get_method(method_path)

result = method_reference(2, 3.5)
print(result)
# 7.0
```


# Dev Setup

## Prerequisites

- Python >=3.7, <4
- Tested on Mac 10.15.6 Catalina, Ubuntu 18.04


## Install Bleeding Edge Version 

```shell script
# clone the repo
$ git clone https://github.com/Neural-Space/genos.git
# Install system-level dependencies
$ make install-system-deps
 # Install environment level dependencies
$ make install-deps
```

### Testing and Code formatting

```shell script
# run the tests to make sure everything works
$ make unit-test

# check coverage of the code
$ make test-coverage
```

# Contribution guide
Read the contribution guideline over [here](https://github.com/Neural-Space/genos/blob/main/CONTRIBUTING.md).

# Attribution
Icons made by <a href="https://www.flaticon.com/authors/skyclick" title="Skyclick">Skyclick</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Neural-Space/genos",
    "name": "genos",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7, <4",
    "maintainer_email": "",
    "keywords": "instantiation,objects,recursive instantiation,functio call,config instantiate",
    "author": "Kushal Jain",
    "author_email": "kushal@neuralspace.ai",
    "download_url": "https://files.pythonhosted.org/packages/b8/0c/4af5a7834a5e2c89e524c60f789dab4507dde5510a939577fc6fde6d925b/genos-0.1.12.tar.gz",
    "platform": null,
    "description": "<p align=\"center\"><img src=\"https://docs.google.com/drawings/d/e/2PACX-1vQ65pCWYymvOtQXCUSsWfq0xeaE6fFmQ-_QT003eZRbeLiwKoE7xvDe6fCeuBx_ha7aCjpN3mu_WLl9/pub?w=1536&h=480\" alt=\"logo\" width=\"70%\" /></p>  \n<p align=\"center\">\n  <a href=\"https://app.circleci.com/pipelines/github/Neural-Space\">\n    <img src=\"https://circleci.com/gh/Neural-Space/genos.svg?style=shield&circle-token=3658f580f8183f441023a1a4234716410bd74338\" alt=\"CircleCI\" />\n  </a>\n  <a href=\"https://lgtm.com/projects/g/Neural-Space/genos/alerts/\">\n    <img src=\"https://img.shields.io/lgtm/alerts/g/Neural-Space/genos.svg?logo=lgtm&logoWidth=18\" alt=\"Total alerts\" />\n  </a>\n  <a href=\"https://lgtm.com/projects/g/Neural-Space/genos/context:python\">\n    <img src=\"https://img.shields.io/lgtm/grade/python/g/Neural-Space/genos.svg?logo=lgtm&logoWidth=18\" alt=\"Language grade: Python\" />\n  </a>\n  <a href=\"https://opensource.org/licenses/MIT\">\n    <img src=\"https://img.shields.io/badge/License-MIT-yellow.svg\" alt=\"mit\" />\n  </a>\n</p>\n\n--------------------------------------\n\n\n\nInstantiate objects and call functions using dictionary configs in Python using Genos. \nThis package was originally developed to help python developers in making configurable software components. \n\nWhile [Hydra](https://github.com/facebookresearch/hydra) lets you instantiate objects and functions, it doesn't support recursive instantiation. \nPlus, Hydra is mostly used for config management. \nSo, we decided to build Genos by referring to Hydra and added the functionality of recursive instantiation. E.g.,\n\n**Install Genos**\n```bash\npip install genos\n```\n\nInstantiate a Python Class using Genos\n\n```python\nfrom genos import recursive_instantiate\n\nned = {\n    \"cls\": \"genos.examples.King\",\n    \"params\":{\n        \"name\": \"Eddard Stark\",\n        \"queen\": \"Catelyn Stark\",\n        \"allegiance\": \"Robert Baratheon\"\n    }\n}\n\nobj = recursive_instantiate(ned)\nprint(obj)\n```   \n\n# Contributors\n\n[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/0)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/0)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/1)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/1)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/2)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/2)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/3)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/3)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/4)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/4)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/5)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/5)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/6)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/6)[![](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/images/7)](https://sourcerer.io/fame/kushalj001/Neural-Space/genos/links/7)\n\n# Install\n\n### Pip\n```bash\npip install genos\n```\n\n### Poetry\n```bash\npoetry add genos\n```\n\n# Basic Usage\n\nThe following examples will show how this library can make your life easier by letting you instantiate python objects from dictionaries.\nFirst, let's consider a basic example where we simply instantiate a single class. \n\n### Single Class Instantiation\n```python\nclass King:\n    def __init__(self, name:str, queen:str, allegiance:str):\n        self.name = name\n        self.queen = queen\n        self.allegiance = allegiance\n\n    def __repr__(self):\n        return f\"Name:{self.name}\\nQueen:{self.queen}\\nAllegiance:{self.allegiance}\"\n        \n    def print_name(self):\n        print(self.name)\n```\n\nWe need to pass 3 parameters to instantiate this class. \nNote that these classes are located in the `genos.examples.*` subpackage. \nSo, let's say we wish to instantiate a `King` object for Eddard Stark because, afterall, _Winter is coming._\n\n```python\nfrom genos import recursive_instantiate\n\nned = {\n    \"cls\": \"genos.examples.King\",\n    \"params\":{\n        \"name\": \"Eddard Stark\",\n        \"queen\": \"Catelyn Stark\",\n        \"allegiance\": \"Robert Baratheon\"\n    }\n}\n\nobj = recursive_instantiate(ned)\nprint(obj)\n# Name:Eddard Stark\n# Queen:Catelyn Stark\n# Allegiance:Robert Baratheon\n```\n\n### Recursive Class Instantiation\n\nWell, this seemed quite simple. \nBut rarely are things so simple in life. \nConsider another class that takes an instance of `King` as a parameter.\n\n```python\nclass House:\n    def __init__(self, king:King, home:str, sigil:str):\n        self.king = king\n        self.home = home\n        self.sigil = sigil\n\n    def __repr__(self):\n        return f\"King:{self.king.name}\\nHome:{self.home}\\nSigil:{self.sigil}\"\n\n```\n\nThis is where recursive instantiation comes into action. \nTo initialize an object for this class, we can very easily create a nested dictionary and pass it to our magic method. \nOf course, we'll be instantiating an object for House Stark.\n```python\nfrom genos import recursive_instantiate\n\nstark = {\n    \"cls\": \"genos.examples.House\",\n    \"params\": {\n        \"king\":{\n            \"cls\": \"genos.examples.King\",\n            \"params\":{\n                \"name\": \"Eddard Stark\",\n                \"queen\": \"Catelyn Stark\",\n                \"allegiance\": \"Robert Baratheon\"\n                }\n        },\n        \"home\":\"Winterfell\",\n        \"sigil\":\"Direwolf\"\n    }\n}\n\nobj = recursive_instantiate(stark)\nprint(obj)\n# output\n# King:Eddard Stark\n# Home:Winterfell\n# Sigil:Direwolf\n```\n\n### Instantiation Using Positional Arguments  \nThe examples shown above always use keyword arguments to instantiate the classes. But we can also choose to simply pass in the positional arguments as shown below.  \n\n```python\nfrom genos import recursive_instantiate\n\nstark = {\n    \"cls\": \"genos.examples.House\",\n    \"args\": [\n        {\n            \"cls\": \"genos.examples.King\",\n            \"params\":{\n                \"name\": \"Eddard Stark\",\n                \"queen\": \"Catelyn Stark\",\n                \"allegiance\": \"Robert Baratheon\"\n                }\n        },\n        \"Winterfell\",\n        \"Direwolf\"\n    ]\n}\n\nobj = recursive_instantiate(stark)\nprint(obj)\n# output\n# King:Eddard Stark\n# Home:Winterfell\n# Sigil:Direwolf\n```\n\n### Instantiation Using Positional and Keyword Arguments\nThe following example makes use of both positional and keyword arguments together to instantiate the `House` class. We do not pass the keyword for the `king` parameter but we do so for the following parameters:`home` and `sigil`.\n```python\nfrom genos import recursive_instantiate\n\nstark = {\n    \"cls\": \"genos.examples.House\",\n    \"args\": [\n        {\n            \"cls\": \"genos.examples.King\",\n            \"params\":{\n                \"name\": \"Eddard Stark\",\n                \"queen\": \"Catelyn Stark\",\n                \"allegiance\": \"Robert Baratheon\"\n                }\n        }\n    ],\n    \"params\": {\n        \"home\":\"Winterfell\",\n        \"sigil\":\"Direwolf\"\n    }\n}\n\nobj = recursive_instantiate(stark)\nprint(obj)\n# output\n# King:Eddard Stark\n# Home:Winterfell\n# Sigil:Direwolf\n```\n\n### Call A Function\nJust like we classes, we can also instantiate functions by calling `recursive_instantiate`. The following example shows how we can instantiate and call a simple `multiply` function using `genos`.\n```python\nfrom genos import recursive_instantiate\n\nfunction_call = {\n    \"cls\": \"genos.examples.multiply\",\n    \"args\": [12, 1.3]\n}\n\nresult = recursive_instantiate(function_call)\nprint(result)\n# output\n# 15.600000000000001\n```\n\n# Advanced Usage\n\n### Deep Learning Example using PyTorch\n\nFor running the following examples you will need to install `Pytorch`.\n\n```shell script\npip install torch\n```\n\nSuch workflows where we need to instantiate multiple classes recursively is more evident in Deep Learning and related fields. \nNeuralSpace has been actively working in this space, building tools for Natural Language Processing (NLP). We have created this tool to make things easier for us. \nThe following example shows a scenario where you need different components/modules to create your own custom neural network for some specific task. The individual classes are merely wrappers around `PyTorch` functions. Let's get started.\n\nThe following example classes can be found in `genos.examples.complex_examples.py`.\n\n```python\nfrom torch import nn\n\nclass ActivationLayer(nn.Module):\n    '''\n    Gives two choices for activation function: ReLU or Tanh.\n    Introduces non-linearity in neural-networks. Usually applied \n    after an affine transformation.\n    '''\n    def __init__(self, activation:str):\n        super().__init__()\n        \n        if activation == 'relu':\n            self.activation = nn.ReLU()\n        else:\n            self.activation = nn.Tanh()\n    \n    def forward(self, x):\n        return self.activation(x)\n    \n\nclass AffineLayer(nn.Module):\n    '''\n    Performs an affine transformation on the input tensor.\n    For an input tensor \"x\", the output is W.x + b, where W \n    is a trainable weight matrix and b is the bias vector.\n    '''\n    def __init__(self, in_features:int, out_features:int, activation: ActivationLayer):\n        super().__init__()\n        self.transform = nn.Linear(in_features=in_features, out_features=out_features)\n        self.activation = activation\n    \n    def forward(self, x):\n        return self.activation(self.transform(x))\n        \nclass LSTMLayer(nn.Module):\n    '''\n    A wrapper over LSTM layer.\n    '''\n    def __init__(self, input_size, hidden_size, batch_first, dropout):\n        super().__init__()\n        self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=batch_first)\n        self.dropout = nn.Dropout(dropout)\n    \n    def forward(self, x):\n        output, _ = self.lstm(x)\n        return self.dropout(output)        \n```\nThe three classes above will now be used to create a custom neural network. \nNote carefully that in order to instantiate an `AffineLayer`, we need to pass an object of `ActivationLayer`. \nThe `CustomModel` will comprise of two components: `AffineLayer` and `LSTMLayer`.\n\n```python\n\nclass CustomModel(nn.Module):\n    \n    def __init__(self, affine_layer:AffineLayer, lstm_layer:LSTMLayer):\n        super().__init__()\n        self.affine_layer = affine_layer\n        self.lstm_layer = lstm_layer\n    \n    def forward(self, x):\n        return self.affine_layer(self.lstm_layer(x))\n```\n\nThe instantiation of this class using genos will be as follows.\n\n```python\nfrom genos import recursive_instantiate\n\ncustom_obj = \\\n{\n    \"cls\": \"genos.examples.CustomModel\",\n    \"params\": {\n        \"affine_layer\": {\n            {\n                \"cls\": \"examples.example.AffineLayer\",\n                \"params\": {\n                    \"in_features\": 256,\n                    \"out_features\": 256,\n                    \"activation\": {\n                        \"cls\": \"genos.examples.ActivationLayer\",\n                        \"params\": {\n                            \"activation\": \"relu\"\n                        }\n                    }\n                }\n            },\n            {\n                \"cls\": \"genos.examples.LSTMLayer\",\n                \"params\":{\n                    \"input_size\": 256,\n                    \"hidden_size\":256,\n                    \"batch_first\":True,\n                }\n            }\n        }\n        \n    }\n}\n\n\nmodel = recursive_instantiate(custom_obj)\nx = torch.randn(32, 100, 256)\nout = model(x)\nprint(out.shape)\n# [32, 100, 256]\n```\n\n### Get Class Reference from Class Path\nIf you do not wish to use `genos` for instantiating your functions or classes, you can still use it to find and load different classes within your project structure. The following example shows the usage of `get_class()` function from `genos` to locate the `King` class.\n```python\nfrom genos import get_class\n\nclass_path = \"genos.examples.King\"\nclass_reference = get_class(class_path)\n\neddard_stark = class_reference(name=\"Eddard Stark\", queen=\"Catelyn Stark\", \n                               allegiance=\"Robert Baratheon\")\nprint(eddard_stark)\n# Name:Eddard Stark\n# Queen:Catelyn Stark\n# Allegiance:Robert Baratheon\n```\n\n\n### Get Function Reference from Class Path\nSimilar to the `get_class()` method above, you can also use `get_method()` function from `genos` to find functions in your project structure and instantiate them normally.\n```python\nfrom genos import get_method\n\nmethod_path = \"genos.examples.multiply\"\nmethod_reference = get_method(method_path)\n\nresult = method_reference(2, 3.5)\nprint(result)\n# 7.0\n```\n\n\n# Dev Setup\n\n## Prerequisites\n\n- Python >=3.7, <4\n- Tested on Mac 10.15.6 Catalina, Ubuntu 18.04\n\n\n## Install Bleeding Edge Version \n\n```shell script\n# clone the repo\n$ git clone https://github.com/Neural-Space/genos.git\n# Install system-level dependencies\n$ make install-system-deps\n # Install environment level dependencies\n$ make install-deps\n```\n\n### Testing and Code formatting\n\n```shell script\n# run the tests to make sure everything works\n$ make unit-test\n\n# check coverage of the code\n$ make test-coverage\n```\n\n# Contribution guide\nRead the contribution guideline over [here](https://github.com/Neural-Space/genos/blob/main/CONTRIBUTING.md).\n\n# Attribution\nIcons made by <a href=\"https://www.flaticon.com/authors/skyclick\" title=\"Skyclick\">Skyclick</a> from <a href=\"https://www.flaticon.com/\" title=\"Flaticon\"> www.flaticon.com</a>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Instantiate objects and call functions using dictionary configs in Python using Genos.",
    "version": "0.1.12",
    "project_urls": {
        "Homepage": "https://github.com/Neural-Space/genos"
    },
    "split_keywords": [
        "instantiation",
        "objects",
        "recursive instantiation",
        "functio call",
        "config instantiate"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3db7bfab054678c963f1013177e132b7c31070de90a26ff3f8299ae5bca0597",
                "md5": "3e9d51787d70b747d7202ffc69cbccf9",
                "sha256": "04e67f5707d3d8a8554f3336c031f47b540e62d69a8f97e45d2802671c42d070"
            },
            "downloads": -1,
            "filename": "genos-0.1.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3e9d51787d70b747d7202ffc69cbccf9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7, <4",
            "size": 11524,
            "upload_time": "2023-06-19T11:13:51",
            "upload_time_iso_8601": "2023-06-19T11:13:51.086275Z",
            "url": "https://files.pythonhosted.org/packages/b3/db/7bfab054678c963f1013177e132b7c31070de90a26ff3f8299ae5bca0597/genos-0.1.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b80c4af5a7834a5e2c89e524c60f789dab4507dde5510a939577fc6fde6d925b",
                "md5": "9e7ca34278276e71de27707f4c2fce64",
                "sha256": "06e312854632333232afa93c2db6a2f507df770f69c3e77788a82d654fbd3e2b"
            },
            "downloads": -1,
            "filename": "genos-0.1.12.tar.gz",
            "has_sig": false,
            "md5_digest": "9e7ca34278276e71de27707f4c2fce64",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7, <4",
            "size": 15836,
            "upload_time": "2023-06-19T11:13:53",
            "upload_time_iso_8601": "2023-06-19T11:13:53.261486Z",
            "url": "https://files.pythonhosted.org/packages/b8/0c/4af5a7834a5e2c89e524c60f789dab4507dde5510a939577fc6fde6d925b/genos-0.1.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-19 11:13:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Neural-Space",
    "github_project": "genos",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "circle": true,
    "lcname": "genos"
}
        
Elapsed time: 0.07866s