Commit fc18810a authored by miaecle's avatar miaecle
Browse files

add mpnn classification

parent c6e14b14
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1112,7 +1112,7 @@ class MPNNTensorGraph(TensorGraph):
      results = []
      for feed_dict in generator:
        # Extract number of unique samples in the batch from w_b
        n_valid_samples = len(np.nonzero(feed_dict[self.weights][:, 0])[0])
        n_valid_samples = len(np.nonzero(np.sum(feed_dict[self.weights], 1))[0])
        feed_dict = {
            self.layers[k.name].out_tensor: v
            for k, v in six.iteritems(feed_dict)
+126 −0
Original line number Diff line number Diff line
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Nov  2 16:27:33 2017

@author: zqwu
"""
from __future__ import division
import numpy as np
import tensorflow as tf
import os
from sklearn.externals import joblib 
import sklearn.pipeline
import sklearn.preprocessing
from sklearn.kernel_approximation import RBFSampler

class Actor(object):
  
  def __init__(self, len_states=10, n_actions=7, batch_size=32, lr=0.001):
    self.len_states = len_states
    self.n_actions = n_actions # Number of actions
    self.batch_size = batch_size
    self.lr = lr # learning rate
    self.initialize_featurizer()
    self.g = tf.Graph()
    with self.g.as_default():
      self._build_model()
      self._add_train_cost()
      self.sess = tf.Session(graph=self.g)
      self.sess.run(tf.global_variables_initializer())
  
  def save(self, model_path, global_step=1):
    """ Save model """
    with self.g.as_default():
      saver = tf.train.Saver()
      saver.save(self.sess, os.path.join(model_path, "model"), global_step)
    joblib.dump(self.scaler, os.path.join(model_path, "scaler.joblib"))
    joblib.dump(self.featurizer, os.path.join(model_path, "featurizer.joblib"))
      
  def restore(self, model_path, global_step=1):
    """ Restore model """
    with self.g.as_default():
      saver = tf.train.Saver()
      saver.restore(self.sess, os.path.join(model_path, "model-") + str(global_step))
    self.scaler = joblib.load(os.path.join(model_path, "scaler.joblib"))
    self.featurizer = joblib.load(os.path.join(model_path, "featurizer.joblib"))

  def initialize_featurizer(self):
    observation_examples = np.array([np.random.uniform(-np.pi, 
                                                       np.pi, 
                                                       (self.len_states,)) 
   for x in range(10000)])
    self.scaler = sklearn.preprocessing.StandardScaler()
    self.scaler.fit(observation_examples) # Normalization

    self.featurizer = sklearn.pipeline.FeatureUnion([
        ("rbf1", RBFSampler(gamma=5.0, n_components=100)),
        ("rbf2", RBFSampler(gamma=2.0, n_components=100)),
        ("rbf3", RBFSampler(gamma=1.0, n_components=100)),
        ("rbf4", RBFSampler(gamma=0.5, n_components=100))])
    self.featurizer.fit(self.scaler.transform(observation_examples)) # RBF featurizer
  
  def process_states(self, s):
    # This function featurize states(batch_size * 2)  into inputs(batch_size * 400)
    assert len(s.shape) == 2
    assert s.shape[1] == self.len_states
    scaled = self.scaler.transform(s)
    featurized = self.featurizer.transform(scaled)
    return featurized

  def _build_model(self):
    # Inputs: batch_size * 400
    self.state = tf.placeholder(tf.float32, shape=(None, 400))
    # First hidden layer: batch_size * 10
    W1 = tf.Variable(np.random.normal(0, 0.02, (400, 10)), dtype=tf.float32)
    b1 = tf.Variable(np.zeros((10,))+0.01, dtype=tf.float32)
    hidden1 = tf.nn.tanh(tf.matmul(self.state, W1) + b1)
    # State values: batch_size * 1
    W2 = tf.Variable(np.random.normal(0, 0.02, (10, 1)), dtype=tf.float32)
    b2 = tf.Variable(np.zeros((1,))+0.01, dtype=tf.float32)
    self.V = tf.matmul(hidden1, W2) + b2
    # Action values: batch_size * n_actions
    W3 = tf.Variable(np.random.normal(0, 0.02, (10, self.n_actions)), dtype=tf.float32)
    b3 = tf.Variable(np.zeros((self.n_actions,))+0.01, dtype=tf.float32)
    self.A = tf.matmul(hidden1, W3) + b3

    W4 = tf.Variable(np.random.normal(0, 0.02, (10, self.n_actions)), dtype=tf.float32)
    b4 = tf.Variable(np.zeros((self.n_actions,))+0.01, dtype=tf.float32)
    self.sigma = tf.matmul(hidden1, W4) + b4

    # State-action values: batch_size * n_actions
    self.mu = self.V + (self.A - tf.reduce_mean(self.A, axis=1, keep_dims=True))
    self.normal_dist = tf.contrib.distributions.Normal(tf.reshape(self.mu, (-1,)), 
                                                       tf.reshape(self.sigma, (-1,)))
    self.a = tf.reshape(self.normal_dist.sample(), (-1, self.n_actions))
    
  def _add_train_cost(self):
    # Target Q values
    self.action = tf.placeholder(tf.float32, shape=(None, self.n_actions))
    self.target = tf.placeholder(tf.float32, shape=(None,))
    target = tf.stack([self.target]*self.n_actions, axis=1)
    # Minimize L2 loss
    self.loss = -self.normal_dist.log_prob(tf.reshape(self.action, (-1,))) * tf.reshape(target, (-1,))
    self.loss -= 0.01 * self.normal_dist.entropy()
    self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr)
    self.train_op = self.optimizer.minimize(tf.reduce_sum(self.loss))
  
  def update_Q(self, states, action, target):
    # Update model variables
    s = np.reshape(np.array(states, dtype=float), (-1, self.len_states))
    feed_dict = {
        self.state: self.process_states(s),
        self.action: action,
        self.target: target
    }
    self.sess.run(self.train_op, feed_dict=feed_dict)
  
  def ChooseAction(self, states, mode='epsilon', epsilon=0.9):
    # Using epsilon-greedy to choose actions
    s = np.reshape(np.array(states, dtype=float), (-1, self.len_states))
    feed_dict = {self.state: self.process_states(s)}
    a = self.sess.run(self.a, feed_dict=feed_dict)
    return a
  

  
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ CheckFeaturizer = {
    ('bace_c', 'dag'): ['GraphConv', 75],
    ('bace_c', 'weave'): ['Weave', 75],
    ('bace_c', 'textcnn'): ['Raw', None],
    ('bace_c', 'mpnn'): ['Weave', [75, 14]],
    ('bbbp', 'logreg'): ['ECFP', 1024],
    ('bbbp', 'tf'): ['ECFP', 1024],
    ('bbbp', 'tf_robust'): ['ECFP', 1024],
@@ -21,6 +22,7 @@ CheckFeaturizer = {
    ('bbbp', 'dag'): ['GraphConv', 75],
    ('bbbp', 'weave'): ['Weave', 75],
    ('bbbp', 'textcnn'): ['Raw', None],
    ('bbbp', 'mpnn'): ['Weave', [75, 14]],
    ('clintox', 'logreg'): ['ECFP', 1024],
    ('clintox', 'tf'): ['ECFP', 1024],
    ('clintox', 'tf_robust'): ['ECFP', 1024],
@@ -32,6 +34,7 @@ CheckFeaturizer = {
    ('clintox', 'dag'): ['GraphConv', 75],
    ('clintox', 'weave'): ['Weave', 75],
    ('clintox', 'textcnn'): ['Raw', None],
    ('clintox', 'mpnn'): ['Weave', [75, 14]],
    ('hiv', 'logreg'): ['ECFP', 1024],
    ('hiv', 'tf'): ['ECFP', 1024],
    ('hiv', 'tf_robust'): ['ECFP', 1024],
@@ -43,6 +46,7 @@ CheckFeaturizer = {
    ('hiv', 'dag'): ['GraphConv', 75],
    ('hiv', 'weave'): ['Weave', 75],
    ('hiv', 'textcnn'): ['Raw', None],
    ('hiv', 'mpnn'): ['Weave', [75, 14]],
    ('muv', 'logreg'): ['ECFP', 1024],
    ('muv', 'tf'): ['ECFP', 1024],
    ('muv', 'tf_robust'): ['ECFP', 1024],
@@ -56,6 +60,7 @@ CheckFeaturizer = {
    ('muv', 'res'): ['GraphConv', 75],
    ('muv', 'weave'): ['Weave', 75],
    ('muv', 'textcnn'): ['Raw', None],
    ('muv', 'mpnn'): ['Weave', [75, 14]],
    ('pcba', 'logreg'): ['ECFP', 1024],
    ('pcba', 'tf'): ['ECFP', 1024],
    ('pcba', 'tf_robust'): ['ECFP', 1024],
@@ -92,6 +97,7 @@ CheckFeaturizer = {
    ('sider', 'attn'): ['GraphConv', 75],
    ('sider', 'res'): ['GraphConv', 75],
    ('sider', 'textcnn'): ['Raw', None],
    ('sider', 'mpnn'): ['Weave', [75, 14]],
    ('tox21', 'logreg'): ['ECFP', 1024],
    ('tox21', 'tf'): ['ECFP', 1024],
    ('tox21', 'tf_robust'): ['ECFP', 1024],
@@ -106,6 +112,7 @@ CheckFeaturizer = {
    ('tox21', 'attn'): ['GraphConv', 75],
    ('tox21', 'res'): ['GraphConv', 75],
    ('tox21', 'textcnn'): ['Raw', None],
    ('tox21', 'mpnn'): ['Weave', [75, 14]],
    ('toxcast', 'logreg'): ['ECFP', 1024],
    ('toxcast', 'tf'): ['ECFP', 1024],
    ('toxcast', 'tf_robust'): ['ECFP', 1024],
@@ -116,6 +123,7 @@ CheckFeaturizer = {
    ('toxcast', 'graphconv'): ['GraphConv', 75],
    ('toxcast', 'weave'): ['Weave', 75],
    ('toxcast', 'textcnn'): ['Raw', None],
    ('toxcast', 'mpnn'): ['Weave', [75, 14]],
    ('bace_r', 'tf_regression'): ['ECFP', 1024],
    ('bace_r', 'rf_regression'): ['ECFP', 1024],
    ('bace_r', 'krr'): ['ECFP', 1024],
+8 −8
Original line number Diff line number Diff line
@@ -83,6 +83,14 @@ hps['textcnn'] = {
    'num_filters': [100, 200, 200, 200, 200, 100, 100, 100, 100, 100, 160, 160],
    'seed': 123
}
hps['mpnn'] = {
    'batch_size': 16,
    'nb_epoch': 50,
    'learning_rate': 0.0005,
    'T': 2,
    'M': 5,
    'seed': 123
}
hps['rf'] = {'n_estimators': 500}
hps['kernelsvm'] = {'C': 1.0, 'gamma': 0.05}
hps['xgb'] = {
@@ -177,14 +185,6 @@ hps['ani'] = {
    'layer_structures': [20, 10, 10],
    'seed': 123
}
hps['mpnn'] = {
    'batch_size': 64,
    'nb_epoch': 50,
    'learning_rate': 0.001,
    'T': 2,
    'M': 5,
    'seed': 123
}

hps['xgb_regression'] = {
    'max_depth': 5,
+8 −0
Original line number Diff line number Diff line
model_checkpoint_path: "model-699"
all_model_checkpoint_paths: "model-99"
all_model_checkpoint_paths: "model-199"
all_model_checkpoint_paths: "model-299"
all_model_checkpoint_paths: "model-399"
all_model_checkpoint_paths: "model-499"
all_model_checkpoint_paths: "model-599"
all_model_checkpoint_paths: "model-699"
Loading