Beispiel #1
0
def test_WeaveGather_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 75))
  atom_split = Feature(shape=(None,), dtype=tf.int32)
  weave_gather = WeaveGather(
      32, gaussian_expand=True, in_layers=[atom_feature, atom_split])
  tg.add_output(weave_gather)
  tg.set_loss(weave_gather)
  tg.build()
  tg.save()
Beispiel #2
0
    def test_weave_gather(self):
        """Test that WeaveGather can be invoked."""
        batch_size = 1
        num_samples = 2
        num_atoms_per_sample = 5
        num_features = 5
        gaussian_expand = False

        atom_split_np = list()
        for i in range(num_samples):
            atom_split_np.extend([i] * num_atoms_per_sample)
        atom_split_np = np.array(atom_split_np)
        tensor_input_np = np.random.randn(num_samples * num_atoms_per_sample,
                                          num_features)

        # Expected output
        expected_output = list()
        for i in range(num_samples):
            expected_output.append(
                np.sum(tensor_input_np[i * num_atoms_per_sample:(i + 1) *
                                       num_atoms_per_sample],
                       axis=0))
        expected_output = np.array(expected_output)

        with self.session() as sess:
            tensor_input_tf = tf.convert_to_tensor(tensor_input_np)
            atom_split_tf = tf.convert_to_tensor(atom_split_np)
            weave_gather = WeaveGather(batch_size=batch_size,
                                       gaussian_expand=gaussian_expand)
            weave_gather.create_tensor(
                in_layers=[tensor_input_tf, atom_split_tf])

            sess.run(tf.global_variables_initializer())
            out_tensor = weave_gather.out_tensor.eval()

            self.assertAllClose(expected_output, out_tensor)
            self.assertEqual(expected_output.shape, out_tensor.shape)
Beispiel #3
0
  def test_weave_gather(self):
    """Test that WeaveGather can be invoked."""
    batch_size = 1
    num_samples = 2
    num_atoms_per_sample = 5
    num_features = 5
    gaussian_expand = False

    atom_split_np = list()
    for i in range(num_samples):
      atom_split_np.extend([i] * num_atoms_per_sample)
    atom_split_np = np.array(atom_split_np)
    tensor_input_np = np.random.randn(num_samples * num_atoms_per_sample,
                                      num_features)

    # Expected output
    expected_output = list()
    for i in range(num_samples):
      expected_output.append(
          np.sum(
              tensor_input_np[i * num_atoms_per_sample:(i + 1) *
                              num_atoms_per_sample],
              axis=0))
    expected_output = np.array(expected_output)

    with self.session() as sess:
      tensor_input_tf = tf.convert_to_tensor(tensor_input_np)
      atom_split_tf = tf.convert_to_tensor(atom_split_np)
      weave_gather = WeaveGather(
          batch_size=batch_size, gaussian_expand=gaussian_expand)
      weave_gather.create_tensor(in_layers=[tensor_input_tf, atom_split_tf])

      sess.run(tf.global_variables_initializer())
      out_tensor = weave_gather.out_tensor.eval()

      self.assertAllClose(expected_output, out_tensor)
      self.assertEqual(expected_output.shape, out_tensor.shape)
Beispiel #4
0
  def build_graph(self):
    """Building graph structures:
        Features => WeaveLayer => WeaveLayer => Dense => WeaveGather => Classification or Regression
        """
    self.atom_features = Feature(shape=(None, self.n_atom_feat))
    self.pair_features = Feature(shape=(None, self.n_pair_feat))
    combined = Combine_AP(in_layers=[self.atom_features, self.pair_features])
    self.pair_split = Feature(shape=(None,), dtype=tf.int32)
    self.atom_split = Feature(shape=(None,), dtype=tf.int32)
    self.atom_to_pair = Feature(shape=(None, 2), dtype=tf.int32)
    weave_layer1 = WeaveLayer(
        n_atom_input_feat=self.n_atom_feat,
        n_pair_input_feat=self.n_pair_feat,
        n_atom_output_feat=self.n_hidden,
        n_pair_output_feat=self.n_hidden,
        in_layers=[combined, self.pair_split, self.atom_to_pair])
    weave_layer2 = WeaveLayer(
        n_atom_input_feat=self.n_hidden,
        n_pair_input_feat=self.n_hidden,
        n_atom_output_feat=self.n_hidden,
        n_pair_output_feat=self.n_hidden,
        update_pair=False,
        in_layers=[weave_layer1, self.pair_split, self.atom_to_pair])
    separated = Separate_AP(in_layers=[weave_layer2])
    dense1 = Dense(
        out_channels=self.n_graph_feat,
        activation_fn=tf.nn.tanh,
        in_layers=[separated])
    batch_norm1 = BatchNormalization(epsilon=1e-5, mode=1, in_layers=[dense1])
    weave_gather = WeaveGather(
        self.batch_size,
        n_input=self.n_graph_feat,
        gaussian_expand=True,
        in_layers=[batch_norm1, self.atom_split])

    costs = []
    self.labels_fd = []
    for task in range(self.n_tasks):
      if self.mode == "classification":
        classification = Dense(
            out_channels=2, activation_fn=None, in_layers=[weave_gather])
        softmax = SoftMax(in_layers=[classification])
        self.add_output(softmax)

        label = Label(shape=(None, 2))
        self.labels_fd.append(label)
        cost = SoftMaxCrossEntropy(in_layers=[label, classification])
        costs.append(cost)
      if self.mode == "regression":
        regression = Dense(
            out_channels=1, activation_fn=None, in_layers=[weave_gather])
        self.add_output(regression)

        label = Label(shape=(None, 1))
        self.labels_fd.append(label)
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      all_cost = Concat(in_layers=costs, axis=1)
    elif self.mode == "regression":
      all_cost = Stack(in_layers=costs, axis=1)
    self.weights = Weights(shape=(None, self.n_tasks))
    loss = WeightedError(in_layers=[all_cost, self.weights])
    self.set_loss(loss)
Beispiel #5
0
    def build_graph(self):
        """Building graph structures:
                Features => WeaveLayer => WeaveLayer => Dense => WeaveGather => Classification or Regression
                """
        self.atom_features = Feature(shape=(None, self.n_atom_feat))
        self.pair_features = Feature(shape=(None, self.n_pair_feat))
        self.pair_split = Feature(shape=(None, ), dtype=tf.int32)
        self.atom_split = Feature(shape=(None, ), dtype=tf.int32)
        self.atom_to_pair = Feature(shape=(None, 2), dtype=tf.int32)
        weave_layer1A, weave_layer1P = WeaveLayerFactory(
            n_atom_input_feat=self.n_atom_feat,
            n_pair_input_feat=self.n_pair_feat,
            n_atom_output_feat=self.n_hidden,
            n_pair_output_feat=self.n_hidden,
            in_layers=[
                self.atom_features, self.pair_features, self.pair_split,
                self.atom_to_pair
            ])
        weave_layer2A, weave_layer2P = WeaveLayerFactory(
            n_atom_input_feat=self.n_hidden,
            n_pair_input_feat=self.n_hidden,
            n_atom_output_feat=self.n_hidden,
            n_pair_output_feat=self.n_hidden,
            update_pair=False,
            in_layers=[
                weave_layer1A, weave_layer1P, self.pair_split,
                self.atom_to_pair
            ])
        dense1 = Dense(out_channels=self.n_graph_feat,
                       activation_fn=tf.nn.tanh,
                       in_layers=weave_layer2A)
        batch_norm1 = BatchNorm(epsilon=1e-5, in_layers=[dense1])
        weave_gather = WeaveGather(self.batch_size,
                                   n_input=self.n_graph_feat,
                                   gaussian_expand=True,
                                   in_layers=[batch_norm1, self.atom_split])

        n_tasks = self.n_tasks
        weights = Weights(shape=(None, n_tasks))
        if self.mode == 'classification':
            n_classes = self.n_classes
            labels = Label(shape=(None, n_tasks, n_classes))
            logits = Reshape(shape=(None, n_tasks, n_classes),
                             in_layers=[
                                 Dense(in_layers=weave_gather,
                                       out_channels=n_tasks * n_classes)
                             ])
            output = SoftMax(logits)
            self.add_output(output)
            loss = SoftMaxCrossEntropy(in_layers=[labels, logits])
            weighted_loss = WeightedError(in_layers=[loss, weights])
            self.set_loss(weighted_loss)
        else:
            labels = Label(shape=(None, n_tasks))
            output = Reshape(shape=(None, n_tasks),
                             in_layers=[
                                 Dense(in_layers=weave_gather,
                                       out_channels=n_tasks)
                             ])
            self.add_output(output)
            weighted_loss = ReduceSum(
                L2Loss(in_layers=[labels, output, weights]))
            self.set_loss(weighted_loss)
Beispiel #6
0
    def build_graph(self):
        self.atom_features = Feature(shape=(None, self.n_atom_feat))
        self.pair_features = Feature(shape=(None, self.n_pair_feat))
        self.pair_split = Feature(shape=(None, ), dtype=tf.int32)
        self.atom_split = Feature(shape=(None, ), dtype=tf.int32)
        self.atom_to_pair = Feature(shape=(None, 2), dtype=tf.int32)
        weave_layer1A, weave_layer1P = WeaveLayerFactory(
            n_atom_input_feat=self.n_atom_feat,
            n_pair_input_feat=self.n_pair_feat,
            n_atom_output_feat=self.n_hidden[0],
            n_pair_output_feat=self.n_hidden[0],
            in_layers=[
                self.atom_features, self.pair_features, self.pair_split,
                self.atom_to_pair
            ])
        for myind in range(1, len(self.n_hidden) - 1):
            weave_layer1A, weave_layer1P = WeaveLayerFactory(
                n_atom_input_feat=self.n_hidden[myind - 1],
                n_pair_input_feat=self.n_hidden[myind - 1],
                n_atom_output_feat=self.n_hidden[myind],
                n_pair_output_feat=self.n_hidden[myind],
                update_pair=True,
                in_layers=[
                    weave_layer1A, weave_layer1P, self.pair_split,
                    self.atom_to_pair
                ])
        if len(self.n_hidden) > 1.5:
            myind = len(self.n_hidden) - 1
            weave_layer1A, weave_layer1P = WeaveLayerFactory(
                n_atom_input_feat=self.n_hidden[myind - 1],
                n_pair_input_feat=self.n_hidden[myind - 1],
                n_atom_output_feat=self.n_hidden[myind],
                n_pair_output_feat=self.n_hidden[myind],
                update_pair=False,
                in_layers=[
                    weave_layer1A, weave_layer1P, self.pair_split,
                    self.atom_to_pair
                ])
        dense1 = Dense(out_channels=self.n_graph_feat[0],
                       activation_fn=tf.nn.tanh,
                       in_layers=weave_layer1A)
        #batch_norm1 = BatchNormalization(epsilon=1e-5, mode=1, in_layers=[dense1])
        batch_norm1 = MyBatchNorm(in_layers=[dense1])
        weave_gather = WeaveGather(self.batch_size,
                                   n_input=self.n_graph_feat[0],
                                   gaussian_expand=False,
                                   in_layers=[batch_norm1, self.atom_split])

        weave_gatherBatchNorm2 = MyBatchNorm(in_layers=[weave_gather])
        curLayer = weave_gatherBatchNorm2
        for myind in range(1, len(self.n_graph_feat) - 1):
            curLayer = Dense(out_channels=self.n_graph_feat[myind],
                             activation_fn=tf.nn.relu,
                             in_layers=[curLayer])
            curLayer = Dropout(self.dropout, in_layers=[curLayer])

        classification = Dense(out_channels=self.n_tasks,
                               activation_fn=None,
                               in_layers=[curLayer])
        sigmoid = MySigmoid(in_layers=[classification])
        self.add_output(sigmoid)

        self.label = Label(shape=(None, self.n_tasks))
        all_cost = MySigmoidCrossEntropy(
            in_layers=[self.label, classification])
        self.weights = Weights(shape=(None, self.n_tasks))
        loss = WeightedError(in_layers=[all_cost, self.weights])
        self.set_loss(loss)

        self.mydense1 = dense1
        self.mybatch_norm1 = batch_norm1
        self.myweave_gather = weave_gather
        self.myclassification = classification
        self.mysigmoid = sigmoid
        self.myall_cost = all_cost
        self.myloss = loss