Commit 96de0a46 authored by miaecle's avatar miaecle
Browse files

refinement and changes

parent 0dfcb436
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -80,7 +80,6 @@ def pad_batch(batch_size, X_b, y_b, w_b, ids_b):
  num_samples = len(X_b)
  if num_samples == batch_size:
    return (X_b, y_b, w_b, ids_b)
  else:
  # By invariant of when this is called, can assume num_samples > 0
  # and num_samples < batch_size
  if len(X_b.shape) > 1:
+7 −26
Original line number Diff line number Diff line
@@ -163,19 +163,7 @@ def atom_features(atom, bool_id_feat=False, explicit_H=False):
            'Pb',
            'Unknown'
        ]) + one_of_k_encoding(atom.GetDegree(),
                               [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    # In case of explicit hydrogen(QM8, QM9), avoid calling `GetTotalNumHs`
    if explicit_H:
      results = results + \
        one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6]) + \
        [atom.GetFormalCharge(), atom.GetNumRadicalElectrons()] + \
        one_of_k_encoding_unk(atom.GetHybridization(), [
            Chem.rdchem.HybridizationType.SP, Chem.rdchem.HybridizationType.SP2,
            Chem.rdchem.HybridizationType.SP3, Chem.rdchem.HybridizationType.
            SP3D, Chem.rdchem.HybridizationType.SP3D2
        ]) + [atom.GetIsAromatic()]
    else:
      results = results + one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4]) + \
                               [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + \
        one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6]) + \
        [atom.GetFormalCharge(), atom.GetNumRadicalElectrons()] + \
        one_of_k_encoding_unk(atom.GetHybridization(), [
@@ -183,6 +171,9 @@ def atom_features(atom, bool_id_feat=False, explicit_H=False):
            Chem.rdchem.HybridizationType.SP3, Chem.rdchem.HybridizationType.
            SP3D, Chem.rdchem.HybridizationType.SP3D2
        ]) + [atom.GetIsAromatic()]
    # In case of explicit hydrogen(QM8, QM9), avoid calling `GetTotalNumHs`
    if not explicit_H:
      results = results + one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4])

    return np.array(results)

@@ -290,27 +281,17 @@ class WeaveFeaturizer(Featurizer):

  name = ['weave_mol']

  def __init__(self, graph_distance=True, explicit_H=None):
  def __init__(self, graph_distance=True, explicit_H=False):
    # Distance is either graph distance(True) or Euclidean distance(False,
    # only support datasets providing Cartesian coordinates)
    self.graph_distance = graph_distance
    # Set dtype
    self.dtype = object
    # Check if there are explicit hydrogens, default to be False
    self.check_H = False
    if explicit_H is None:
      self.explicit_H = False
      # Set to True if explicit hydrogen is not specified
      self.check_H = True
    # If includes explicit hydrogens
    self.explicit_H = explicit_H

  def _featurize(self, mol):
    """Encodes mol as a WeaveMol object."""
    # Check hydrogen in the molecule
    if self.check_H and not self.explicit_H:
      for a in mol.GetAtoms():
        if a.GetSymbol() == 'H':
          self.explicit_H = True
          break
    # Atom features
    idx_nodes = [(a.GetIdx(), atom_features(a, explicit_H=self.explicit_H))
                 for a in mol.GetAtoms()]
+16 −6
Original line number Diff line number Diff line
@@ -873,7 +873,6 @@ class MessagePassing(Layer):
      self.out_tensor = out_tensor
    return out_tensor


class EdgeNetwork(object):
  """ Submodule for Message Passing """

@@ -971,11 +970,11 @@ class SetGather(Layer):
    atom_features = in_layers[0].out_tensor
    atom_split = in_layers[1].out_tensor

    c = tf.zeros((self.batch_size, self.n_hidden))
    h = tf.zeros((self.batch_size, self.n_hidden))
    self.c = tf.zeros((self.batch_size, self.n_hidden))
    self.h = tf.zeros((self.batch_size, self.n_hidden))

    for i in range(self.M):
      q_expanded = tf.gather(h, atom_split)
      q_expanded = tf.gather(self.h, atom_split)
      e = tf.reduce_sum(atom_features * q_expanded, 1)
      e_mols = tf.dynamic_partition(e, atom_split, self.batch_size)
      # Add another value(~-Inf) to prevent error in softmax
@@ -985,8 +984,8 @@ class SetGather(Layer):
      a = tf.concat([tf.nn.softmax(e_mol)[:-1] for e_mol in e_mols], 0)
      r = tf.segment_sum(tf.reshape(a, [-1, 1]) * atom_features, atom_split)
      # Model using this layer must set pad_batches=True
      q_star = tf.concat([h, r], axis=1)
      h, c = self.LSTMStep(q_star, c)
      q_star = tf.concat([self.h, r], axis=1)
      self.h, self.c = self.LSTMStep(q_star, self.c)

    out_tensor = q_star
    if set_tensors:
@@ -1005,3 +1004,14 @@ class SetGather(Layer):
    h_out = o * tf.nn.tanh(c_out)

    return h_out, c_out


  def none_tensors(self):
    self.out_tensor = None
    self.h = None
    self.c = None
    saved_tensors = [self.out_tensor, self.h, self.c]
    return saved_tensors

  def set_tensors(self, tensors):
    self.out_tensor, self.h, self.c = tensors

examples/results.csv

deleted100644 → 0
+0 −5
Original line number Diff line number Diff line
qm7b,random,regression,dtnn,mean-pearson_r2_score,train,0.93149678885630138,valid,0.9070831996898977,time_for_running,628.2365071773529
sampl,random,regression,krr,mean-pearson_r2_score,train,0.99985273293498711,valid,0.81413385193207155,time_for_running,0.484935998916626
ppb,random,regression,krr,mean-pearson_r2_score,train,0.99436827538907779,valid,0.36149235755484083,time_for_running,1.822425127029419
qm7,random,regression,ani,mean-pearson_r2_score,train,0.90124742726047558,valid,0.86827080462582273,time_for_running,1619.0067789554596
qm7,random,regression,ani,mean-pearson_r2_score,train,0.96049723833152711,valid,0.9318116152192033,time_for_running,1144.9879438877106