Beispiel #1
0
 def test_l2_loss(self):
     """Test invoking L2Loss in eager mode."""
     with context.eager_mode():
         input1 = np.random.rand(5, 10).astype(np.float32)
         input2 = np.random.rand(5, 10).astype(np.float32)
         result = layers.L2Loss()(input1, input2)
         expected = np.mean((input1 - input2)**2, axis=1)
         assert np.allclose(result, expected)
Beispiel #2
0
    def test_tensorboard(self):
        """Test creating an Estimator from a TensorGraph that logs information to TensorBoard."""
        n_samples = 10
        n_features = 3
        n_tasks = 2

        # Create a dataset and an input function for processing it.

        np.random.seed(123)
        X = np.random.rand(n_samples, n_features)
        y = np.zeros((n_samples, n_tasks))
        dataset = dc.data.NumpyDataset(X, y)

        def input_fn(epochs):
            x, y, weights = dataset.make_iterator(batch_size=n_samples,
                                                  epochs=epochs).get_next()
            return {'x': x, 'weights': weights}, y

        # Create a TensorGraph model.

        model = dc.models.TensorGraph()
        features = layers.Feature(shape=(None, n_features))
        dense = layers.Dense(out_channels=n_tasks, in_layers=features)
        dense.set_summary('histogram')
        model.add_output(dense)
        labels = layers.Label(shape=(None, n_tasks))
        loss = layers.ReduceMean(layers.L2Loss(in_layers=[labels, dense]))
        model.set_loss(loss)

        # Create an estimator from it.

        x_col = tf.feature_column.numeric_column('x', shape=(n_features, ))
        estimator = model.make_estimator(feature_columns=[x_col])

        # Train the model.

        estimator.train(input_fn=lambda: input_fn(100))
model = dc.models.TensorGraph(model_dir='rnai')
features = layers.Feature(shape=(None, 21, 4))
labels = layers.Label(shape=(None, 1))
prev = features
for i in range(2):
    prev = layers.Conv1D(filters=10,
                         kernel_size=10,
                         activation=tf.nn.relu,
                         padding='same',
                         in_layers=prev)
    prev = layers.Dropout(dropout_prob=0.3, in_layers=prev)
output = layers.Dense(out_channels=1,
                      activation_fn=tf.sigmoid,
                      in_layers=layers.Flatten(prev))
model.add_output(output)
loss = layers.ReduceMean(layers.L2Loss(in_layers=[labels, output]))
model.set_loss(loss)

# Load the data.

train = dc.data.DiskDataset('train_siRNA')
valid = dc.data.DiskDataset('valid_siRNA')

# Train the model, tracking its performance on the training and validation datasets.

metric = dc.metrics.Metric(dc.metrics.pearsonr, mode='regression')
for i in range(20):
    model.fit(train, nb_epoch=10)
    print(model.evaluate(train, [metric])['pearsonr'][0])
    print(model.evaluate(valid, [metric])['pearsonr'][0])
loader = dc.data.ImageLoader()
dataset = loader.featurize(files, np.array(labels))
splitter = dc.splits.RandomSplitter()
train_dataset, valid_dataset, test_dataset = splitter.train_valid_test_split(dataset, seed=123)

# Create the model.
learning_rate = dc.models.tensorgraph.optimizers.ExponentialDecay(0.001, 0.9, 250)
model = dc.models.TensorGraph(learning_rate=learning_rate, model_dir='models/model')
features = layers.Feature(shape=(None, 520, 696))
labels = layers.Label(shape=(None,))
prev_layer = features
for num_outputs in [16, 32, 64, 128, 256]:
  prev_layer = layers.Conv2D(num_outputs, kernel_size=5, stride=2, in_layers=prev_layer)
output = layers.Dense(1, in_layers=layers.Flatten(prev_layer))
model.add_output(output)
loss = layers.ReduceSum(layers.L2Loss(in_layers=(output, labels)))
model.set_loss(loss)

if not os.path.exists('./models'):
  os.mkdir('models')
if not os.path.exists('./models/model'):
  os.mkdir('models/model')

if not RETRAIN:
  model.restore()

# Train it and evaluate performance on the test set.
if RETRAIN:
  print("About to fit model for 50 epochs")
  model.fit(train_dataset, nb_epoch=50)
y_pred = model.predict(test_dataset).flatten()