Skip to content

Commit

Permalink
Merge pull request #1313 from rbharath/tfbinding
Browse files Browse the repository at this point in the history
Multilayer SequenceDNN
  • Loading branch information
rbharath authored Jul 15, 2018
2 parents 52c2350 + ad19082 commit da74fda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
22 changes: 14 additions & 8 deletions deepchem/models/tensorgraph/models/sequence_dnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
tasks working with genomic data.
"""
import tensorflow as tf
import collections
from deepchem.models import Sequential
from deepchem.models.tensorgraph import layers
from deepchem.data import NumpyDataset
Expand All @@ -14,9 +15,6 @@ class SequenceDNN(Sequential):
"""
Sequence DNN models.
# TODO(rbharath): This model only supports one-conv layer. Extend
# so that conv layers of greater depth can be implemented.
Parameters
----------
seq_length : int
Expand All @@ -27,7 +25,7 @@ class SequenceDNN(Sequential):
number of tasks. Default: 1.
num_filters : list[int] | tuple[int]
number of convolutional filters in each layer. Default: (15,).
conv_width : list[int] | tuple[int]
kernel_size: list[int] | tuple[int]
width of each layer's convolutional filters. Default: (15,).
pool_width : int
width of max pooling after the last layer. Default: 35.
Expand All @@ -44,17 +42,25 @@ def __init__(self,
loss,
use_RNN=False,
num_tasks=1,
num_filters=15,
kernel_size=15,
num_filters=(15,),
kernel_size=(15,),
pool_width=35,
L1=0,
dropout=0.0,
verbose=True,
**kwargs):
super(SequenceDNN, self).__init__(loss, **kwargs)
if not isinstance(num_filters, collections.Sequence) or not isinstance(
kernel_size, collections.Sequence):
raise ValueError("num_filters and kernel_size must be lists")
if not len(num_filters) == len(kernel_size):
raise ValueError("num_filters and kernel_size must be of same length")
n_layers = len(num_filters)
self.num_tasks = num_tasks
self.verbose = verbose
self.add(layers.Conv2D(num_filters, kernel_size=kernel_size))
self.add(layers.Dropout(dropout))
for layer in range(n_layers):
self.add(
layers.Conv2D(num_filters[layer], kernel_size=kernel_size[layer]))
self.add(layers.Dropout(dropout))
self.add(layers.Flatten())
self.add(layers.Dense(self.num_tasks, activation_fn=tf.nn.relu))
13 changes: 11 additions & 2 deletions deepchem/models/tensorgraph/tests/test_sequencednn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_seq_dnn_singlefilter_train(self):
# # False: num_sequences / num_negatives
# #} if not multitask else None,
dataset = dc.data.NumpyDataset(X, y)
model = dc.models.SequenceDNN(50, "binary_crossentropy", num_filters=1)
model = dc.models.SequenceDNN(50, "binary_crossentropy", num_filters=[1])
model.fit(dataset, nb_epoch=1)

def test_seq_dnn_multifilter_train(self):
Expand All @@ -32,5 +32,14 @@ def test_seq_dnn_multifilter_train(self):
# # False: num_sequences / num_negatives
# #} if not multitask else None,
dataset = dc.data.NumpyDataset(X, y)
model = dc.models.SequenceDNN(50, "binary_crossentropy", num_filters=15)
model = dc.models.SequenceDNN(50, "binary_crossentropy", num_filters=[15])
model.fit(dataset, nb_epoch=1)

def test_seq_dnn_multilayer_train(self):
"""Test SequenceDNN with multiple layers works."""
X = np.random.rand(10, 1, 4, 50)
y = np.random.randint(0, 2, size=(10, 1))
dataset = dc.data.NumpyDataset(X, y)
model = dc.models.SequenceDNN(
50, "binary_crossentropy", num_filters=[1, 1], kernel_size=[15, 15])
model.fit(dataset, nb_epoch=1)

0 comments on commit da74fda

Please sign in to comment.