IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Browse Source

Add test_fast_model_01().

master
Stanislaw Adaszewski 4 years ago
parent
commit
8edf8ce4f9
2 changed files with 57 additions and 2 deletions
  1. +7
    -2
      src/icosagon/fastmodel.py
  2. +50
    -0
      tests/icosagon/test_fastmodel.py

+ 7
- 2
src/icosagon/fastmodel.py View File

@@ -1,12 +1,16 @@
from .fastconv import FastConvLayer from .fastconv import FastConvLayer
from .bulkdec import BulkDecodeLayer from .bulkdec import BulkDecodeLayer
from .input import OneHotInputLayer from .input import OneHotInputLayer
from .trainprep import PreparedData
import torch import torch
import types import types
from typing import List, \
Union, \
Callable
class FastModel(torch.nn.Module): class FastModel(torch.nn.Module):
def __init(self, prep_d: PreparedData,
def __init__(self, prep_d: PreparedData,
layer_dimensions: List[int] = [32, 64], layer_dimensions: List[int] = [32, 64],
keep_prob: float = 1., keep_prob: float = 1.,
rel_activation: Callable[[torch.Tensor], torch.Tensor] = lambda x: x, rel_activation: Callable[[torch.Tensor], torch.Tensor] = lambda x: x,
@@ -20,6 +24,7 @@ class FastModel(torch.nn.Module):
layer_activation, dec_activation) layer_activation, dec_activation)
self.prep_d = prep_d self.prep_d = prep_d
self.layer_dimensions = layer_dimensions
self.keep_prob = float(keep_prob) self.keep_prob = float(keep_prob)
self.rel_activation = rel_activation self.rel_activation = rel_activation
self.layer_activation = layer_activation self.layer_activation = layer_activation
@@ -55,7 +60,7 @@ class FastModel(torch.nn.Module):
def forward(self, _): def forward(self, _):
return self.seq(None) return self.seq(None)
def self._check_params(self, prep_d, layer_dimensions, rel_activation,
def _check_params(self, prep_d, layer_dimensions, rel_activation,
layer_activation, dec_activation): layer_activation, dec_activation):
if not isinstance(prep_d, PreparedData): if not isinstance(prep_d, PreparedData):


+ 50
- 0
tests/icosagon/test_fastmodel.py View File

@@ -0,0 +1,50 @@
from icosagon.fastmodel import FastModel
from icosagon.data import Data
from icosagon.trainprep import prepare_training, \
TrainValTest
import torch
import time
def _make_symmetric(x: torch.Tensor):
x = (x + x.transpose(0, 1)) / 2
return x
def _symmetric_random(n_rows, n_columns):
return _make_symmetric(torch.rand((n_rows, n_columns),
dtype=torch.float32).round().to_sparse())
def _some_data_with_interactions():
d = Data()
d.add_node_type('Gene', 1000)
d.add_node_type('Drug', 100)
fam = d.add_relation_family('Drug-Gene', 1, 0, True)
fam.add_relation_type('Target',
torch.rand((100, 1000), dtype=torch.float32).round().to_sparse())
fam = d.add_relation_family('Gene-Gene', 0, 0, True)
fam.add_relation_type('Interaction',
_symmetric_random(1000, 1000))
fam = d.add_relation_family('Drug-Drug', 1, 1, True)
for i in range(500):
fam.add_relation_type('Side Effect: Nausea %d' % i,
_symmetric_random(100, 100))
fam.add_relation_type('Side Effect: Infertility %d' % i,
_symmetric_random(100, 100))
fam.add_relation_type('Side Effect: Death %d' % i,
_symmetric_random(100, 100))
return d
def test_fast_model_01():
d = _some_data_with_interactions()
prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
model = FastModel(prep_d)
for i in range(10):
t = time.time()
_ = model(None)
print('Model forward took:', time.time() - t)

Loading…
Cancel
Save