GithubHelp home page GithubHelp logo

chytorch's Introduction

Chytorch [kʌɪtɔːrtʃ]

Library for modeling molecules and reactions in torch way.

Installation

Use pip install chytorch to install release version.

Or pip install . in source code directory to install DEV version.

Pretrained models

Chytorch main package doesn't include models zoo. Each model has its own named package and can be installed separately. Installed models can be imported as from chytorch.zoo.<model_name> import Model.

Usage

chytorch.nn.MoleculeEncoder and chytorch.nn.ReactionEncoder - core graphormer layers for molecules and reactions. API is combination of torch.nn.TransformerEncoderLayer with torch.nn.TransformerEncoder.

Batch preparation:

chytorch.utils.data.MoleculeDataset and chytorch.utils.data.ReactionDataset - Map-like on-the-fly dataset generators for molecules and reactions. Supported chython.MoleculeContainer and chython.ReactionContainer objects, and bytes-packed structures.

chytorch.utils.data.collate_molecules and chytorch.utils.data.collate_reactions - collate functions for torch.utils.data.DataLoader.

Note: torch DataLoader automatically do proper collation since 1.13 release.

Example:

from chytorch.utils.data import MoleculeDataset, SMILESDataset
from torch.utils.data import DataLoader

data = ['CCO', 'CC=O']
ds = MoleculeDataset(SMILESDataset(data, cache={}))
dl = DataLoader(ds, batch_size=10)

Forward call:

Molecules coded as tensors of:

  • atoms numbers shifted by 2 (e.g. hydrogen = 3). 0 - reserved for padding, 1 - reserved for CLS token, 2 - extra reservation.
  • neighbors count, including implicit hydrogens shifted by 2 (e.g. CO = CH3OH = [6, 4]). 0 - reserved for padding, 1 - extra reservation, 2 - no-neighbors, 3 - one neighbor.
  • topological distances' matrix shifted by 2 with upper limit. 0 - reserved for padding, 1 - reserved for not-connected graph components coding, 2 - self-loop, 3 - connected atoms.

Reactions coded in similar way. Molecules atoms and neighbors matrices just stacked. Distance matrices stacked on diagonal. Reactions include additional tensor with reaction role codes for each token. 0 - padding, 1 - reaction CLS, 2 - reactants, 3 - products.

from chytorch.nn import MoleculeEncoder

encoder = MoleculeEncoder()
for b in dl:
    encoder(b)

Combine molecules and labels:

chytorch.utils.data.chained_collate - helper for combining different data parts. Useful for tricky input.

from torch import stack
from torch.utils.data import DataLoader, TensorDataset
from chytorch.utils.data import chained_collate, collate_molecules, MoleculeDataset

dl = DataLoader(TensorDataset(MoleculeDataset(molecules_list), properties_tensor),
    collate_fn=chained_collate(collate_molecules, stack))

Scheduler:

chytorch.optim.lr_scheduler.WarmUpCosine - Linear warmup followed with cosine-function for 0-pi range rescaled to lr_rate - decrease_coef * lr_rate interval.

Voting NN with single hidden layer:

chytorch.nn.VotingClassifier, chytorch.nn.BinaryVotingClassifier and chytorch.nn.VotingRegressor - speed optimized multiple heads for ensemble predictions.

Helper Modules:

chytorch.nn.Slicer - do tensor slicing. Useful for transformer's CLS token extraction in torch.nn.Sequence.

Data Wrappers:

In chytorch.utils.data module stored different data wrappers for simplifying ML workflows. All wrappers have torch.utils.data.Dataset interface.

  • SizedList - list wrapper with size() method. Useful with torch.utils.data.TensorDataset.
  • SMILESDataset - on-the-fly smiles to chython.MoleculeContainer or chython.ReactionContainer parser.
  • LMDBMapper - LMDB KV storage to dataset mapper.
  • PostgresMapper - Postgres DB table to dataset mapper.
  • SMILESTokenizerDataset - on-the-fly generator of tokenized SMILES.
  • TensorUnpack, StructUnpack, PickleUnpack - bytes to tensor/object unpackers

Publications

1 Bidirectional Graphormer for Reactivity Understanding: Neural Network Trained to Reaction Atom-to-Atom Mapping Task

chytorch's People

Contributors

aigulkhkmv avatar stsouko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chytorch's Issues

Collate import incompatible with torch version <1.13

One import makes chytorch incompatible with PyTorch with a version smaller than 1.13.

The import is located in utils/data/molecule/contrastive.py (line 25):

from torch.utils.data._utils.collate import default_collate_fn_map

In torch 1.13 dict default_collate_fn_map can be found here: https://github.com/pytorch/pytorch/blob/v1.13.0/torch/utils/data/_utils/collate.py#L191 , however in older versions it is not present.

It seems that it is enough to copy-paste the code from PyTorch 1.13 that is responsible for creating default_collate_fn_map:

default_collate_fn_map: Dict[Union[Type, Tuple[Type, ...]], Callable] = {torch.Tensor: collate_tensor_fn}
with contextlib.suppress(ImportError):
    import numpy as np
    # For both ndarray and memmap (subclass of ndarray)
    default_collate_fn_map[np.ndarray] = collate_numpy_array_fn
    # See scalars hierarchy: https://numpy.org/doc/stable/reference/arrays.scalars.html
    # Skip string scalars
    default_collate_fn_map[(np.bool_, np.number, np.object_)] = collate_numpy_scalar_fn
default_collate_fn_map[float] = collate_float_fn
default_collate_fn_map[int] = collate_int_fn
default_collate_fn_map[string_classes] = collate_str_fn

Although I do not see any usage of the default_collate_fn_map in the repo... Is it relevant to have it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.