def test_dtnn_extract(self): """Test that DTNNExtract can be invoked.""" num_samples = 20 num_features = 30 task_id = 15 test_tensor_input = np.random.randn(num_samples, num_features) test_output = test_tensor_input[:, task_id:task_id + 1] with self.session() as sess: test_tensor = tf.convert_to_tensor(test_tensor_input) dtnn_extract = DTNNExtract(task_id=task_id) dtnn_extract.create_tensor(in_layers=[test_tensor]) sess.run(tf.global_variables_initializer()) out_tensor = dtnn_extract.out_tensor.eval() self.assertAllClose(test_output, out_tensor) self.assertEqual(out_tensor.shape, (num_samples, 1))
def test_DTNNExtract_pickle(): tg = TensorGraph() atom_features = Feature(shape=(None, 30)) Ext = DTNNExtract(0, in_layers=[atom_features]) tg.add_output(Ext) tg.set_loss(Ext) tg.build() tg.save()
def build_graph(self): """Building graph structures: Features => DTNNEmbedding => DTNNStep => DTNNStep => DTNNGather => Regression """ self.atom_number = Feature(shape=(None,), dtype=tf.int32) self.distance = Feature(shape=(None, self.n_distance)) self.atom_membership = Feature(shape=(None,), dtype=tf.int32) self.distance_membership_i = Feature(shape=(None,), dtype=tf.int32) self.distance_membership_j = Feature(shape=(None,), dtype=tf.int32) dtnn_embedding = DTNNEmbedding( n_embedding=self.n_embedding, in_layers=[self.atom_number]) dtnn_layer1 = DTNNStep( n_embedding=self.n_embedding, n_distance=self.n_distance, in_layers=[ dtnn_embedding, self.distance, self.distance_membership_i, self.distance_membership_j ]) dtnn_layer2 = DTNNStep( n_embedding=self.n_embedding, n_distance=self.n_distance, in_layers=[ dtnn_layer1, self.distance, self.distance_membership_i, self.distance_membership_j ]) dtnn_gather = DTNNGather( n_embedding=self.n_embedding, layer_sizes=[self.n_hidden], n_outputs=self.n_tasks, output_activation=self.output_activation, in_layers=[dtnn_layer2, self.atom_membership]) costs = [] self.labels_fd = [] for task in range(self.n_tasks): regression = DTNNExtract(task, in_layers=[dtnn_gather]) self.add_output(regression) label = Label(shape=(None, 1)) self.labels_fd.append(label) cost = L2Loss(in_layers=[label, regression]) costs.append(cost) 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)