Esempio n. 1
0
def length_accuracy(dataset):
    """

    generates the lengthwise accuracy of a dataset

    :param dataset: dataset to consider
    :return:
    """

    model = load_model(constants.text_recognition)

    for x, y in dataset:
        # create the accuracy evaluation object
        accuracy = SparseCategoricalAccuracy()

        # make a prediction and update the state of the accuracy using it
        prediction = model.predict(x)

        accuracy.update_state(y, prediction)

        print(y[0].numpy())
        print(np.argmax(prediction, axis=-1)[0])

        print("sequences of length", x[1].shape[1] - 1,
              "had an accuracy of", accuracy.result().numpy())
Esempio n. 2
0
    def evaluate(self):

        results = []
        for x_test, y_test in self.gen.evaluate():
            m = SparseCategoricalAccuracy()

            predictions = []

            predictions.append(self.models[0].predict(np.array(x_test)))
            predictions.append(self.models[0].predict(np.array(x_test)))

            # calculate average
            outcomes = average(predictions).numpy()
            print(outcomes)
            print(y_test)

            if len(results) > 0:
                curr_avg = sum(results) / len(results)
                print("Accuracy:", curr_avg)

            m.update_state(
                # We have changed y_true = [[2], [1], [3]] to the following
                y_true=y_test,
                y_pred=outcomes,
                sample_weight=[1, 1, 1])

            results.append(m.result().numpy())

        avg = sum(results) / len(results)
        return avg
Esempio n. 3
0
File: model.py Progetto: jh88/fbnet
    def evaluate(self, dataset):
        accuracy_metric = SparseCategoricalAccuracy()
        for x, y in dataset:
            y_hat = self.predict(x)

            accuracy_metric.update_state(y, y_hat)

        return accuracy_metric.result().numpy()
Esempio n. 4
0
    def on_epoch_end(self, epoch, logs={}):

        y_pred = self.model.predict(self.X_val, verbose=0)
        scce = SparseCategoricalCrossentropy(from_logits=True)
        score = scce(self.y_val, y_pred).numpy()
        acc = SparseCategoricalAccuracy()
        acc.update_state(self.y_val, y_pred)
        print("\n")
        print("The loss is : {}, the accuracy is: {}".format(
            score,
            acc.result().numpy()))
        gc.collect()
        K.clear_session()
Esempio n. 5
0
class SBVAT(SemiSupervisedModel):
    """
        Implementation of sample-based Batch Virtual Adversarial Training
        Graph Convolutional Networks (SBVAT).
        `Batch Virtual Adversarial Training for Graph Convolutional Networks
        <https://arxiv.org/abs/1902.09192>`
        Tensorflow 1.x implementation: <https://github.com/thudzj/BVAT>


    """
    def __init__(self,
                 *graph,
                 n_samples=50,
                 adj_transform="normalize_adj",
                 attr_transform=None,
                 device='cpu:0',
                 seed=None,
                 name=None,
                 **kwargs):
        """Create a sample-based Batch Virtual Adversarial Training
        Graph Convolutional Networks (SBVAT) model.

         This can be instantiated in several ways:

            model = SBVAT(graph)
                with a `graphgallery.data.Graph` instance representing
                A sparse, attributed, labeled graph.

            model = SBVAT(adj_matrix, attr_matrix, labels)
                where `adj_matrix` is a 2D Scipy sparse matrix denoting the graph,
                 `attr_matrix` is a 2D Numpy array-like matrix denoting the node
                 attributes, `labels` is a 1D Numpy array denoting the node labels.


        Parameters:
        ----------
        graph: An instance of `graphgallery.data.Graph` or a tuple (list) of inputs.
            A sparse, attributed, labeled graph.
        n_samples (Positive integer, optional):
            The number of sampled subset nodes in the graph where the length of the
            shortest path between them is at least `4`. (default :obj: `50`)
        adj_transform: string, `transform`, or None. optional
            How to transform the adjacency matrix. See `graphgallery.transforms`
            (default: :obj:`'normalize_adj'` with normalize rate `-0.5`.
            i.e., math:: \hat{A} = D^{-\frac{1}{2}} A D^{-\frac{1}{2}})
        attr_transform: string, `transform`, or None. optional
            How to transform the node attribute matrix. See `graphgallery.transforms`
            (default :obj: `None`)
        device: string. optional
            The device where the model is running on. You can specified `CPU` or `GPU`
            for the model. (default: :str: `CPU:0`, i.e., running on the 0-th `CPU`)
        seed: interger scalar. optional
            Used in combination with `tf.random.set_seed` & `np.random.seed`
            & `random.seed` to create a reproducible sequence of tensors across
            multiple calls. (default :obj: `None`, i.e., using random seed)
        name: string. optional
            Specified name for the model. (default: :str: `class.__name__`)
        kwargs: other customized keyword Parameters.
        """
        super().__init__(*graph, device=device, seed=seed, name=name, **kwargs)

        self.adj_transform = T.get(adj_transform)
        self.attr_transform = T.get(attr_transform)
        self.n_samples = n_samples
        self.process()

    def process_step(self):
        graph = self.graph
        adj_matrix = self.adj_transform(graph.adj_matrix)
        attr_matrix = self.attr_transform(graph.attr_matrix)
        self.neighbors = find_4o_nbrs(adj_matrix)

        self.feature_inputs, self.structure_inputs = T.astensors(
            attr_matrix, adj_matrix, device=self.device)

    # use decorator to make sure all list arguments have the same length
    @EqualVarLength()
    def build(self,
              hiddens=[16],
              activations=['relu'],
              dropout=0.5,
              lr=0.01,
              l2_norm=5e-4,
              use_bias=False,
              p1=1.,
              p2=1.,
              n_power_iterations=1,
              epsilon=0.03,
              xi=1e-6):

        with tf.device(self.device):

            x = Input(batch_shape=[None, self.graph.n_attrs],
                      dtype=self.floatx,
                      name='attr_matrix')
            adj = Input(batch_shape=[None, None],
                        dtype=self.floatx,
                        sparse=True,
                        name='adj_matrix')
            index = Input(batch_shape=[None],
                          dtype=self.intx,
                          name='node_index')

            GCN_layers = []
            dropout_layers = []
            for hidden, activation in zip(hiddens, activations):
                GCN_layers.append(
                    GraphConvolution(
                        hidden,
                        activation=activation,
                        use_bias=use_bias,
                        kernel_regularizer=regularizers.l2(l2_norm)))
                dropout_layers.append(Dropout(rate=dropout))

            GCN_layers.append(
                GraphConvolution(self.graph.n_classes, use_bias=use_bias))
            self.GCN_layers = GCN_layers
            self.dropout_layers = dropout_layers

            logit = self.forward(x, adj)
            output = Gather()([logit, index])
            model = Model(inputs=[x, adj, index], outputs=output)

            self.model = model
            self.train_metric = SparseCategoricalAccuracy()
            self.test_metric = SparseCategoricalAccuracy()
            self.loss_fn = SparseCategoricalCrossentropy(from_logits=True)
            self.optimizer = Adam(lr=lr)

        self.p1 = p1  # Alpha
        self.p2 = p2  # Beta
        self.xi = xi  # Small constant for finite difference
        # Norm length for (virtual) adversarial training
        self.epsilon = epsilon
        self.n_power_iterations = n_power_iterations  # Number of power iterations

    def forward(self, x, adj, training=True):
        h = x
        for dropout_layer, GCN_layer in zip(self.dropout_layers,
                                            self.GCN_layers[:-1]):
            h = GCN_layer([h, adj])
            h = dropout_layer(h, training=training)
        h = self.GCN_layers[-1]([h, adj])
        return h

    @tf.function
    def train_step(self, sequence):

        with tf.device(self.device):
            self.train_metric.reset_states()

            for inputs, labels in sequence:
                x, adj, index, adv_mask = inputs
                with tf.GradientTape() as tape:
                    logit = self.forward(x, adj)
                    output = tf.gather(logit, index)
                    loss = self.loss_fn(labels, output)
                    entropy_loss = entropy_y_x(logit)
                    vat_loss = self.virtual_adversarial_loss(x,
                                                             adj,
                                                             logit=logit,
                                                             adv_mask=adv_mask)
                    loss += self.p1 * vat_loss + self.p2 * entropy_loss

                    self.train_metric.update_state(labels, output)

                trainable_variables = self.model.trainable_variables
                gradients = tape.gradient(loss, trainable_variables)
                self.optimizer.apply_gradients(
                    zip(gradients, trainable_variables))

            return loss, self.train_metric.result()

    @tf.function
    def test_step(self, sequence):

        with tf.device(self.device):
            self.test_metric.reset_states()

            for inputs, labels in sequence:
                x, adj, index, _ = inputs
                logit = self.forward(x, adj, training=False)
                output = tf.gather(logit, index)
                loss = self.loss_fn(labels, output)
                self.test_metric.update_state(labels, output)

            return loss, self.test_metric.result()

    def virtual_adversarial_loss(self, x, adj, logit, adv_mask):
        d = tf.random.normal(shape=tf.shape(x), dtype=self.floatx)

        for _ in range(self.n_power_iterations):
            d = get_normalized_vector(d) * self.xi
            logit_p = logit
            with tf.GradientTape() as tape:
                tape.watch(d)
                logit_m = self.forward(x + d, adj)
                dist = kl_divergence_with_logit(logit_p, logit_m, adv_mask)
            grad = tape.gradient(dist, d)
            d = tf.stop_gradient(grad)

        r_vadv = get_normalized_vector(d) * self.epsilon
        logit_p = tf.stop_gradient(logit)
        logit_m = self.forward(x + r_vadv, adj)
        loss = kl_divergence_with_logit(logit_p, logit_m, adv_mask)
        return tf.identity(loss)

    def train_sequence(self, index):
        index = T.asintarr(index)
        labels = self.graph.labels[index]

        sequence = SBVATSampleSequence(
            [self.feature_inputs, self.structure_inputs, index],
            labels,
            neighbors=self.neighbors,
            n_samples=self.n_samples,
            device=self.device)

        return sequence

    def test_sequence(self, index):
        index = T.asintarr(index)
        labels = self.graph.labels[index]

        sequence = SBVATSampleSequence(
            [self.feature_inputs, self.structure_inputs, index],
            labels,
            neighbors=self.neighbors,
            n_samples=self.n_samples,
            resample=False,
            device=self.device)

        return sequence

    def predict_step(self, sequence):
        with tf.device(self.device):
            for inputs, _ in sequence:
                x, adj, index, adv_mask = inputs
                output = self.forward(x, adj, training=False)
                logit = tf.gather(output, index)

        if tf.is_tensor(logit):
            logit = logit.numpy()
        return logit
Esempio n. 6
0
def train(input_params, train, test, valid, class_cnt):
    current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

    # tensorboard
    train_log_dir = 'logs/gradient_tape/' + current_time + '/train'
    valid_log_dir = 'logs/gradient_tape/' + current_time + '/valid'
    test_log_dir = 'logs/gradient_tape/' + current_time + '/test'
    train_summary_writer = tf.summary.create_file_writer(train_log_dir)
    valid_summary_writer = tf.summary.create_file_writer(valid_log_dir)
    # test_summary_writer = tf.summary.create_file_writer(test_log_dir)

    # todo: create model with hyperparams with model_dir = '../data/models/params/current_time/'
    model_dir = '../data/models/model-' + current_time
    # Instantiate an optimizer.
    optimizer = Adam(learning_rate=0.001)
    # Instantiate a loss function.
    loss_fn = SparseCategoricalCrossentropy(from_logits=True)
    train_step = test_step = 0

    # Prepare the metrics.
    #todo use same variable for all the acc_metrics.
    acc_metric = SparseCategoricalAccuracy()

    if utility.dir_empty(model_dir):
        # model definition
        mobilenet = MOBILENET(include_top=False,
                              input_shape=(224, 224, 3),
                              weights='imagenet',
                              pooling='avg',
                              dropout=0.001)
        mobilenet.summary()
        # select till which layer use mobilenet.
        base_model = Model(inputs=mobilenet.input, outputs=mobilenet.output)
        base_model.summary()

        model = Sequential([
            base_model,
            Dropout(0.2),
            Dense(units=class_cnt, activation='softmax'),
        ])
        model.summary()

        epochs = 200
        for epoch in range(epochs):
            print("\nStart of epoch %d" % (epoch,))
            for batch_idx, (x_batch_train, y_batch_train) in enumerate(train):
                with tf.GradientTape() as tape:
                    # forward pass
                    logits = model(x_batch_train, training=True)

                    # compute loss for mini batch
                    loss_value = loss_fn(y_batch_train, logits)

                grads = tape.gradient(loss_value, model.trainable_weights)

                optimizer.apply_gradients(zip(grads, model.trainable_weights))

                # Update training metric.
                acc_metric.update_state(y_batch_train, logits)

                with train_summary_writer.as_default():
                    # import code; code.interact(local=dict(globals(), **locals()))
                    #TODO: add the metrics for test too.
                    #TODO: take the mean of the losses in every batch and then show,
                    #TODO       loss_value is last loss of the batch(only 1).
                            
                    tf.summary.scalar('loss', loss_value, step=train_step)
                    tf.summary.scalar('accuracy', acc_metric.result(), step=train_step)
                    train_step += 1

                if batch_idx % 10 == 0:
                    print("training loss for one batch at step %d: %.4f" % (batch_idx, float(loss_value)))
            # Display metrics at the end of each epoch.
            
            print("Training acc over epoch: %.4f" % (float(acc_metric.result()),))

            # Reset training metrics at the end of each epoch
            acc_metric.reset_states()


            # iterate on validation 
            for batch_idx, (x_batch_val, y_batch_val) in enumerate(valid):
                # val_logits: y_pred of the validation. 
                val_logits = model(x_batch_val, training=False)
                loss = loss_fn(y_batch_val, val_logits)
                # Update val metrics
                acc_metric.update_state(y_batch_val, val_logits)

                with valid_summary_writer.as_default():
                    tf.summary.scalar('loss', loss, step=test_step)
                    tf.summary.scalar('accuracy', acc_metric.result(), step=test_step)
                    test_step += 1
                
            print("Validation acc: %.4f" % (float(acc_metric.result()),))
            # print(classification_report(y_batch_val, val_logits, target_names=labels))
            acc_metric.reset_states()
        
        acc_metric.reset_states()
        model.save(model_dir + 'model')
        
    else:  # if model_dir is not empty
        print("model already exist. loading model...")
        model = load_model(model_dir+'model')
Esempio n. 7
0
File: model.py Progetto: jh88/fbnet
class Trainer():
    def __init__(self,
                 fbnet,
                 input_shape,
                 initial_temperature=5,
                 temperature_decay_rate=0.956,
                 temperature_decay_steps=1,
                 latency_alpha=0.2,
                 latency_beta=0.6,
                 weight_lr=0.01,
                 weight_momentum=0.9,
                 weight_decay=1e-4,
                 theta_lr=1e-3,
                 theta_beta1=0.9,
                 theta_beta2=0.999,
                 theta_decay=5e-4):
        self._epoch = 0

        self.initial_temperature = initial_temperature
        self.temperature = initial_temperature
        self.latency_alpha = latency_alpha
        self.latency_beta = latency_beta

        self.exponential_decay = lambda step: exponential_decay(
            initial_temperature, temperature_decay_rate,
            temperature_decay_steps, step)

        fbnet.build(input_shape)
        self.fbnet = fbnet

        self.weights = []
        self.thetas = []
        for trainable_weight in fbnet.trainable_weights:
            if 'theta' in trainable_weight.name:
                self.thetas.append(trainable_weight)
            else:
                self.weights.append(trainable_weight)

        self.weight_opt = SGD(learning_rate=weight_lr,
                              momentum=weight_momentum,
                              decay=weight_decay)

        self.theta_opt = Adam(learning_rate=theta_lr,
                              beta_1=theta_beta1,
                              beta_2=theta_beta2,
                              decay=theta_decay)

        self.loss_fn = SparseCategoricalCrossentropy(from_logits=True)
        self.accuracy_metric = SparseCategoricalAccuracy()
        self.loss_metric = Mean()

    @property
    def epoch(self):
        return self._epoch

    @epoch.setter
    def epoch(self, epoch):
        self._epoch = epoch
        self.temperature = self.exponential_decay(epoch)

    def reset_metrics(self):
        self.accuracy_metric.reset_states()
        self.loss_metric.reset_states()

    def _train(self, x, y, weights, opt, training=True):
        with tf.GradientTape() as tape:
            y_hat = self.fbnet(x, self.temperature, training=training)
            loss = self.loss_fn(y, y_hat)
            latency = sum(self.fbnet.losses)
            loss += latency_loss(latency, self.latency_alpha,
                                 self.latency_beta)

        grads = tape.gradient(loss, weights)
        opt.apply_gradients(zip(grads, weights))

        self.accuracy_metric.update_state(y, y_hat)
        self.loss_metric.update_state(loss)

    @tf.function
    def train_weights(self, x, y):
        self._train(x, y, self.weights, self.weight_opt)

    @tf.function
    def train_thetas(self, x, y):
        self._train(x, y, self.thetas, self.theta_opt, training=False)

    @property
    def training_accuracy(self):
        return self.accuracy_metric.result().numpy()

    @property
    def training_loss(self):
        return self.loss_metric.result().numpy()

    @tf.function
    def predict(self, x):
        y_hat = self.fbnet(x, self.temperature, training=False)

        return y_hat

    def evaluate(self, dataset):
        accuracy_metric = SparseCategoricalAccuracy()
        for x, y in dataset:
            y_hat = self.predict(x)

            accuracy_metric.update_state(y, y_hat)

        return accuracy_metric.result().numpy()

    def sample_sequential_config(self):
        ops = [
            op.sample(self.temperature)
            if isinstance(op, MixedOperation) else op for op in self.fbnet.ops
        ]

        sequential_config = {
            'name':
            'sampled_fbnet',
            'layers': [{
                'class_name': type(op).__name__,
                'config': op.get_config()
            } for op in ops if not isinstance(op, Identity)]
        }

        return sequential_config

    def save_weights(self, checkpoint):
        self.fbnet.save_weights(checkpoint, save_format='tf')

    def load_weights(self, checkpoint):
        self.fbnet.load_weights(checkpoint)
Esempio n. 8
0
def sparse_categorical_accuracy(y_true, y_pred):
    m = SparseCategoricalAccuracy()
    m.update_state(y_true, y_pred)
    return m.result().numpy()
Esempio n. 9
0
class DualStudent(Model):
    """"
    Dual Student for Automatic Speech Recognition (ASR).

    How to train: 1) set the optimizer by means of compile(), 2) use train()
    How to test: use test()

    Remarks:
    - Do not use fit() by Keras, use train()
    - Do not use evaluate() by Keras, use test()
    - Compiled metrics and loss (i.e. set by means of compile()) are not used

    Original proposal for image classification: https://arxiv.org/abs/1909.01804
    """
    def __init__(self,
                 n_classes,
                 n_hidden_layers=3,
                 n_units=96,
                 consistency_loss='mse',
                 consistency_scale=10,
                 stabilization_scale=100,
                 xi=0.6,
                 padding_value=0.,
                 sigma=0.01,
                 schedule='rampup',
                 schedule_length=5,
                 version='mono_directional'):
        """
        Constructs a Dual Student model.

        :param n_classes: number of classes (i.e. number of units in the last layer of each student)
        :param n_hidden_layers: number of hidden layers in each student (i.e. LSTM layers)
        :param n_units: number of units for each hidden layer
        :param consistency_loss: one of 'mse', 'kl'
        :param consistency_scale: maximum value of weight for consistency constraint
        :param stabilization_scale: maximum value of weight for stabilization constraint
        :param xi: threshold for stable sample
        :param padding_value: value used to pad input sequences (used as mask_value for Masking layer)
        :param sigma: standard deviation for noisy augmentation
        :param schedule: type of schedule for lambdas, one of 'rampup', 'triangular_cycling', 'sinusoidal_cycling'
        :param schedule_length:
        :param version: one of:
            - 'mono_directional': both students have mono-directional LSTM layers
            - 'bidirectional: both students have bidirectional LSTM layers
            - 'imbalanced': one student has mono-directional LSTM layers, the other one bidirectional
        """
        super(DualStudent, self).__init__()

        # store parameters
        self.n_classes = n_classes
        self.padding_value = padding_value
        self.n_units = n_units
        self.n_hidden_layers = n_hidden_layers
        self.xi = xi
        self.consistency_scale = consistency_scale
        self.stabilization_scale = stabilization_scale
        self.sigma = sigma
        self.version = version
        self.schedule = schedule
        self.schedule_length = schedule_length
        self._lambda1 = None
        self._lambda2 = None

        # schedule for lambdas
        if schedule == 'rampup':
            self.schedule_fn = sigmoid_rampup
        elif schedule == 'triangular_cycling':
            self.schedule_fn = triangular_cycling
        elif schedule == 'sinusoidal_cycling':
            self.schedule_fn = sinusoidal_cycling
        else:
            raise ValueError('Invalid schedule')

        # loss
        self._loss_cls = SparseCategoricalCrossentropy()  # classification loss
        self._loss_sta = MeanSquaredError()  # stabilization loss
        if consistency_loss == 'mse':
            self._loss_con = MeanSquaredError()  # consistency loss
        elif consistency_loss == 'kl':
            self._loss_con = KLDivergence()
        else:
            raise ValueError('Invalid consistency metric')

        # metrics for training
        self._loss1 = Mean(
            name='loss1')  # we want to average the loss for each batch
        self._loss2 = Mean(name='loss2')
        self._loss1_cls = Mean(name='loss1_cls')
        self._loss2_cls = Mean(name='loss2_cls')
        self._loss1_con = Mean(name='loss1_con')
        self._loss2_con = Mean(name='loss2_con')
        self._loss1_sta = Mean(name='loss1_sta')
        self._loss2_sta = Mean(name='loss2_sta')
        self._acc1 = SparseCategoricalAccuracy(name='acc1')
        self._acc2 = SparseCategoricalAccuracy(name='acc2')

        # metrics for testing
        self._test_loss1 = Mean(name='test_loss1')
        self._test_loss2 = Mean(name='test_loss2')
        self._test_acc1_train_phones = SparseCategoricalAccuracy(
            name='test_acc1_train_phones')
        self._test_acc2_train_phones = SparseCategoricalAccuracy(
            name='test_acc2_train_phones')
        self._test_acc1 = Accuracy(name='test_acc1')
        self._test_acc2 = Accuracy(name='test_acc2')
        self._test_per1 = PhoneErrorRate(name='test_per1')
        self._test_per2 = PhoneErrorRate(name='test_per2')

        # compose students
        if version == 'mono_directional':
            lstm_types = ['mono_directional', 'mono_directional']
        elif version == 'bidirectional':
            lstm_types = ['bidirectional', 'bidirectional']
        elif version == 'imbalanced':
            lstm_types = ['mono_directional', 'bidirectional']
        else:
            raise ValueError('Invalid student version')
        self.student1 = self._get_student('student1', lstm_types[0])
        self.student2 = self._get_student('student2', lstm_types[1])

        # masking layer (just to use compute_mask and remove padding)
        self.mask = Masking(mask_value=self.padding_value)

    def _get_student(self, name, lstm_type):
        student = Sequential(name=name)
        student.add(Masking(mask_value=self.padding_value))
        if lstm_type == 'mono_directional':
            for i in range(self.n_hidden_layers):
                student.add(LSTM(units=self.n_units, return_sequences=True))
        elif lstm_type == 'bidirectional':
            for i in range(self.n_hidden_layers):
                student.add(
                    Bidirectional(
                        LSTM(units=self.n_units, return_sequences=True)))
        else:
            raise ValueError('Invalid LSTM version')
        student.add(Dense(units=self.n_classes, activation="softmax"))
        return student

    def _noisy_augment(self, x):
        return x + tf.random.normal(shape=x.shape, stddev=self.sigma)

    def call(self, inputs, training=False, student='student1', **kwargs):
        """
        Feed-forwards inputs to one of the students.

        This function is called internally by __call__(). Do not use it directly, use the model as callable. You may
        prefer to use pad_and_predict() instead of this, because it pads the sequences and splits in batches. For a big
        dataset, it is strongly suggested that you use pad_and_predict().

        :param inputs: tensor of shape (batch_size, n_frames, n_features)
        :param training: boolean, whether the call is in inference mode or training mode
        :param student: one of 'student1', 'student2'
        :return: tensor of shape (batch_size, n_frames, n_classes), softmax activations (probabilities)
        """
        if student == 'student1':
            return self.student1(inputs, training=training)
        elif student != 'student1':
            return self.student2(inputs, training=training)
        else:
            raise ValueError('Invalid student')

    def build(self, input_shape):
        super(DualStudent, self).build(input_shape)
        self.student1.build(input_shape)
        self.student2.build(input_shape)

    def train(self,
              x_labeled,
              x_unlabeled,
              y_labeled,
              x_val=None,
              y_val=None,
              n_epochs=10,
              batch_size=32,
              shuffle=True,
              evaluation_mapping=None,
              logs_path=None,
              checkpoints_path=None,
              initial_epoch=0,
              seed=None):
        """
        Trains the students with both labeled and unlabeled data (semi-supervised learning).

        :param x_labeled: numpy array of numpy arrays (n_frames, n_features), features corresponding to y_labeled.
            'n_frames' can vary, padding is added to make x_labeled a tensor.
        :param x_unlabeled: numpy array of numpy arrays of shape (n_frames, n_features), features without labels.
            'n_frames' can vary, padding is added to make x_unlabeled a tensor.
        :param y_labeled: numpy array of numpy arrays of shape (n_frames,), labels corresponding to x_labeled.
            'n_frames' can vary, padding is added to make y_labeled a tensor.
        :param x_val: like x_labeled, but for validation set
        :param y_val: like y_labeled, but for validation set
        :param n_epochs: integer, number of training epochs
        :param batch_size: integer, batch size
        :param shuffle: boolean, whether to shuffle at each epoch or not
        :param evaluation_mapping: dictionary {training label -> test label}, the test phones should be a subset of the
            training phones
        :param logs_path: path where to save logs for TensorBoard
        :param checkpoints_path: path to a directory. If the directory contains checkpoints, the latest checkpoint is
            restored.
        :param initial_epoch: int, initial epoch from which to start the training. It can be used together with
            checkpoints_path to resume the training from a previous run.
        :param seed: seed for the random number generator
        """
        # set seed
        if seed is not None:
            np.random.seed(seed)
            tf.random.set_seed(seed)

        # show summary
        self.build(input_shape=(None, ) + x_labeled[0].shape)
        self.student1.summary()
        self.student2.summary()

        # setup for logs
        train_summary_writer = None
        if logs_path is not None:
            train_summary_writer = tf.summary.create_file_writer(logs_path)

        # setup for checkpoints
        checkpoint = None
        if checkpoints_path is not None:
            checkpoint = tf.train.Checkpoint(optimizer=self.optimizer,
                                             model=self)
            checkpoint_path = tf.train.latest_checkpoint(checkpoints_path)
            if checkpoint_path is not None:
                checkpoint.restore(checkpoint_path)
            checkpoint_path = Path(checkpoints_path) / 'ckpt'
            checkpoint_path = str(checkpoint_path)

        # compute batch sizes
        labeled_batch_size = ceil(
            len(x_labeled) / (len(x_unlabeled) + len(x_labeled)) * batch_size)
        unlabeled_batch_size = batch_size - labeled_batch_size
        n_batches = min(ceil(len(x_unlabeled) / unlabeled_batch_size),
                        ceil(len(x_labeled) / labeled_batch_size))

        # training loop
        for epoch in trange(initial_epoch, n_epochs, desc='epochs'):
            # ramp up lambda1 and lambda2
            self._lambda1 = self.consistency_scale * self.schedule_fn(
                epoch, self.schedule_length)
            self._lambda2 = self.stabilization_scale * self.schedule_fn(
                epoch, self.schedule_length)

            # shuffle training set
            if shuffle:
                indices = np.arange(
                    len(x_labeled)
                )  # get indices to shuffle coherently features and labels
                np.random.shuffle(indices)
                x_labeled = x_labeled[indices]
                y_labeled = y_labeled[indices]
                np.random.shuffle(x_unlabeled)

            for i in trange(n_batches, desc='batches'):
                # select batch
                x_labeled_batch = select_batch(x_labeled, i,
                                               labeled_batch_size)
                x_unlabeled_batch = select_batch(x_unlabeled, i,
                                                 unlabeled_batch_size)
                y_labeled_batch = select_batch(y_labeled, i,
                                               labeled_batch_size)

                # pad batch
                x_labeled_batch = pad_sequences(x_labeled_batch,
                                                padding='post',
                                                value=self.padding_value,
                                                dtype='float32')
                x_unlabeled_batch = pad_sequences(x_unlabeled_batch,
                                                  padding='post',
                                                  value=self.padding_value,
                                                  dtype='float32')
                y_labeled_batch = pad_sequences(y_labeled_batch,
                                                padding='post',
                                                value=-1)

                # convert to tensors
                x_labeled_batch = tf.convert_to_tensor(x_labeled_batch)
                x_unlabeled_batch = tf.convert_to_tensor(x_unlabeled_batch)
                y_labeled_batch = tf.convert_to_tensor(y_labeled_batch)

                # train step
                self._train_step(x_labeled_batch, x_unlabeled_batch,
                                 y_labeled_batch)

            # put metrics in dictionary (easy management)
            train_metrics = {
                self._loss1.name: self._loss1.result(),
                self._loss2.name: self._loss2.result(),
                self._loss1_cls.name: self._loss1_cls.result(),
                self._loss2_cls.name: self._loss2_cls.result(),
                self._loss1_con.name: self._loss1_con.result(),
                self._loss2_con.name: self._loss2_con.result(),
                self._loss1_sta.name: self._loss1_sta.result(),
                self._loss2_sta.name: self._loss2_sta.result(),
                self._acc1.name: self._acc1.result(),
                self._acc2.name: self._acc2.result(),
            }
            metrics = {'train': train_metrics}

            # test on validation set
            if x_val is not None and y_val is not None:
                val_metrics = self.test(x_val,
                                        y_val,
                                        evaluation_mapping=evaluation_mapping)
                metrics['val'] = val_metrics

            # print metrics
            for dataset, metrics_ in metrics.items():
                print(f'Epoch {epoch + 1} - ', dataset, ' - ', sep='', end='')
                for k, v in metrics_.items():
                    print(f'{k}: {v}, ', end='')
                print()

            # save logs
            if train_summary_writer is not None:
                with train_summary_writer.as_default():
                    for dataset, metrics_ in metrics.items():
                        for k, v in metrics_.items():
                            tf.summary.scalar(k, v, step=epoch)

            # save checkpoint
            if checkpoint is not None:
                checkpoint.save(file_prefix=checkpoint_path)

            # reset metrics
            self._loss1.reset_states()
            self._loss2.reset_states()
            self._loss1_cls.reset_states()
            self._loss2_cls.reset_states()
            self._loss1_con.reset_states()
            self._loss2_con.reset_states()
            self._loss1_sta.reset_states()
            self._loss2_sta.reset_states()
            self._acc1.reset_states()
            self._acc2.reset_states()

    """
    If you want to use graph execution, pad the whole dataset externally and uncomment the decorator below.
    If you uncomment the decorator without padding the dataset, the graph will be compiled for each batch, 
    because train() pads at batch level and so the batches have different shapes. This would result in worse
    performance compared to eager execution.
    """

    # @tf.function
    def _train_step(self, x_labeled, x_unlabeled, y_labeled):
        # noisy augmented batches (TODO: improvement with data augmentation instead of noise)
        B1_labeled = self._noisy_augment(x_labeled)
        B2_labeled = self._noisy_augment(x_labeled)
        B1_unlabeled = self._noisy_augment(x_unlabeled)
        B2_unlabeled = self._noisy_augment(x_unlabeled)

        # compute masks (to remove padding)
        mask_labeled = self.mask.compute_mask(x_labeled)
        mask_unlabeled = self.mask.compute_mask(x_unlabeled)
        y_labeled = y_labeled[mask_labeled]  # remove padding from labels

        # forward pass
        with tf.GradientTape(persistent=True) as tape:
            # predict augmented labeled samples (for classification and consistency constraint)
            prob1_labeled_B1 = self.student1(B1_labeled, training=True)
            prob1_labeled_B2 = self.student1(B2_labeled, training=True)
            prob2_labeled_B1 = self.student2(B1_labeled, training=True)
            prob2_labeled_B2 = self.student2(B2_labeled, training=True)

            # predict augmented unlabeled samples (for consistency and stabilization constraints)
            prob1_unlabeled_B1 = self.student1(B1_unlabeled, training=True)
            prob1_unlabeled_B2 = self.student1(B2_unlabeled, training=True)
            prob2_unlabeled_B1 = self.student2(B1_unlabeled, training=True)
            prob2_unlabeled_B2 = self.student2(B2_unlabeled, training=True)

            # remove padding
            prob1_labeled_B1 = prob1_labeled_B1[mask_labeled]
            prob1_labeled_B2 = prob1_labeled_B2[mask_labeled]
            prob2_labeled_B1 = prob2_labeled_B1[mask_labeled]
            prob2_labeled_B2 = prob2_labeled_B2[mask_labeled]
            prob1_unlabeled_B1 = prob1_unlabeled_B1[mask_unlabeled]
            prob1_unlabeled_B2 = prob1_unlabeled_B2[mask_unlabeled]
            prob2_unlabeled_B1 = prob2_unlabeled_B1[mask_unlabeled]
            prob2_unlabeled_B2 = prob2_unlabeled_B2[mask_unlabeled]

            # compute classification losses
            L1_cls = self._loss_cls(y_labeled, prob1_labeled_B1)
            L2_cls = self._loss_cls(y_labeled, prob2_labeled_B2)

            # concatenate labeled and unlabeled probability predictions (for consistency loss)
            prob1_labeled_unlabeled_B1 = tf.concat(
                [prob1_labeled_B1, prob1_unlabeled_B1], axis=0)
            prob1_labeled_unlabeled_B2 = tf.concat(
                [prob1_labeled_B2, prob1_unlabeled_B2], axis=0)
            prob2_labeled_unlabeled_B1 = tf.concat(
                [prob2_labeled_B1, prob2_unlabeled_B1], axis=0)
            prob2_labeled_unlabeled_B2 = tf.concat(
                [prob2_labeled_B2, prob2_unlabeled_B2], axis=0)

            # compute consistency losses
            L1_con = self._loss_con(prob1_labeled_unlabeled_B1,
                                    prob1_labeled_unlabeled_B2)
            L2_con = self._loss_con(prob2_labeled_unlabeled_B1,
                                    prob2_labeled_unlabeled_B2)

            # prediction
            P1_unlabeled_B1 = tf.argmax(prob1_unlabeled_B1, axis=-1)
            P1_unlabeled_B2 = tf.argmax(prob1_unlabeled_B2, axis=-1)
            P2_unlabeled_B1 = tf.argmax(prob2_unlabeled_B1, axis=-1)
            P2_unlabeled_B2 = tf.argmax(prob2_unlabeled_B2, axis=-1)

            # confidence (probability of predicted class)
            M1_unlabeled_B1 = tf.reduce_max(prob1_unlabeled_B1, axis=-1)
            M1_unlabeled_B2 = tf.reduce_max(prob1_unlabeled_B2, axis=-1)
            M2_unlabeled_B1 = tf.reduce_max(prob2_unlabeled_B1, axis=-1)
            M2_unlabeled_B2 = tf.reduce_max(prob2_unlabeled_B2, axis=-1)

            # stable samples (masks to index probabilities)
            R1 = tf.logical_and(
                P1_unlabeled_B1 == P1_unlabeled_B2,
                tf.logical_or(M1_unlabeled_B1 > self.xi,
                              M1_unlabeled_B2 > self.xi))
            R2 = tf.logical_and(
                P2_unlabeled_B1 == P2_unlabeled_B2,
                tf.logical_or(M2_unlabeled_B1 > self.xi,
                              M2_unlabeled_B2 > self.xi))
            R12 = tf.logical_and(R1, R2)

            # stabilities
            epsilon1 = MSE(prob1_unlabeled_B1[R12], prob1_unlabeled_B2[R12])
            epsilon2 = MSE(prob2_unlabeled_B1[R12], prob2_unlabeled_B2[R12])

            # compute stabilization losses
            L1_sta = self._loss_sta(
                prob1_unlabeled_B1[R12][epsilon1 > epsilon2],
                prob2_unlabeled_B1[R12][epsilon1 > epsilon2])
            L2_sta = self._loss_sta(
                prob1_unlabeled_B2[R12][epsilon1 < epsilon2],
                prob2_unlabeled_B2[R12][epsilon1 < epsilon2])

            L1_sta += self._loss_sta(
                prob1_unlabeled_B1[tf.logical_and(tf.logical_not(R1), R2)],
                prob2_unlabeled_B1[tf.logical_and(tf.logical_not(R1), R2)])
            L2_sta += self._loss_sta(
                prob1_unlabeled_B2[tf.logical_and(R1, tf.logical_not(R2))],
                prob2_unlabeled_B2[tf.logical_and(R1, tf.logical_not(R2))])

            # compute complete losses
            L1 = L1_cls + self._lambda1 * L1_con + self._lambda2 * L1_sta
            L2 = L2_cls + self._lambda1 * L2_con + self._lambda2 * L2_sta

        # backward pass
        gradients1 = tape.gradient(L1, self.student1.trainable_variables)
        gradients2 = tape.gradient(L2, self.student2.trainable_variables)
        self.optimizer.apply_gradients(
            zip(gradients1, self.student1.trainable_variables))
        self.optimizer.apply_gradients(
            zip(gradients2, self.student2.trainable_variables))
        del tape  # to release memory (persistent tape)

        # update metrics
        self._loss1.update_state(L1)
        self._loss2.update_state(L2)
        self._loss1_cls.update_state(L1_cls)
        self._loss2_cls.update_state(L2_cls)
        self._loss1_con.update_state(L1_con)
        self._loss2_con.update_state(L2_con)
        self._loss1_sta.update_state(L1_sta)
        self._loss2_sta.update_state(L2_sta)
        self._acc1.update_state(y_labeled, prob1_labeled_B1)
        self._acc2.update_state(y_labeled, prob2_labeled_B2)

    def test(self, x, y, batch_size=32, evaluation_mapping=None):
        """
        Tests the model (both students).

        :param x: numpy array of numpy arrays (n_frames, n_features), features corresponding to y_labeled.
            'n_frames' can vary, padding is added to make x a tensor.
        :param y: numpy array of numpy arrays of shape (n_frames,), labels corresponding to x_labeled.
            'n_frames' can vary, padding is added to make y a tensor.
        :param batch_size: integer, batch size
        :param evaluation_mapping: dictionary {training label -> test label}, the test phones should be a subset of the
            training phones
        :return: dictionary {metric_name -> value}
        """
        # test batch by batch
        n_batches = ceil(len(x) / batch_size)
        for i in trange(n_batches, desc='test batches'):
            # select batch
            x_batch = select_batch(x, i, batch_size)
            y_batch = select_batch(y, i, batch_size)

            # pad batch
            x_batch = pad_sequences(x_batch,
                                    padding='post',
                                    value=self.padding_value,
                                    dtype='float32')
            y_batch = pad_sequences(y_batch, padding='post', value=-1)

            # convert to tensors
            x_batch = tf.convert_to_tensor(x_batch)
            y_batch = tf.convert_to_tensor(y_batch)

            # test step
            self._test_step(x_batch, y_batch, evaluation_mapping)

        # put metrics in dictionary (easy management)
        test_metrics = {
            self._test_loss1.name:
            self._test_loss1.result(),
            self._test_loss2.name:
            self._test_loss2.result(),
            self._test_acc1_train_phones.name:
            self._test_acc1_train_phones.result(),
            self._test_acc2_train_phones.name:
            self._test_acc2_train_phones.result(),
            self._test_acc1.name:
            self._test_acc1.result(),
            self._test_acc2.name:
            self._test_acc2.result(),
            self._test_per1.name:
            self._test_per1.result(),
            self._test_per2.name:
            self._test_per2.result(),
        }

        # reset metrics
        self._test_loss1.reset_states()
        self._test_loss2.reset_states()
        self._test_acc1_train_phones.reset_states()
        self._test_acc2_train_phones.reset_states()
        self._test_acc1.reset_states()
        self._test_acc2.reset_states()
        self._test_per1.reset_states()
        self._test_per2.reset_states()

        return test_metrics

    # @tf.function      # see note in _train_step()
    def _test_step(self, x, y, evaluation_mapping):
        # compute mask (to remove padding)
        mask = self.mask.compute_mask(x)

        # forward pass
        y_prob1_train_phones = self.student1(x, training=False)
        y_prob2_train_phones = self.student2(x, training=False)
        y_pred1_train_phones = tf.argmax(y_prob1_train_phones, axis=-1)
        y_pred2_train_phones = tf.argmax(y_prob2_train_phones, axis=-1)
        y_train_phones = tf.identity(y)

        # map labels to set of test phones
        if evaluation_mapping is not None:
            y = tf.numpy_function(map_labels,
                                  [y_train_phones, evaluation_mapping],
                                  [tf.float32])
            y_pred1 = tf.numpy_function(
                map_labels, [y_pred1_train_phones, evaluation_mapping],
                [tf.float32])
            y_pred2 = tf.numpy_function(
                map_labels, [y_pred2_train_phones, evaluation_mapping],
                [tf.float32])
        else:
            y = y_train_phones
            y_pred1 = y_pred1_train_phones
            y_pred2 = y_pred2_train_phones

        # update phone error rate
        self._test_per1.update_state(y, y_pred1, mask)
        self._test_per2.update_state(y, y_pred2, mask)

        # remove padding
        y_pred1 = y_pred1[mask]
        y_pred2 = y_pred2[mask]
        y_prob1_train_phones = y_prob1_train_phones[mask]
        y_prob2_train_phones = y_prob2_train_phones[mask]
        y_train_phones = y_train_phones[mask]
        y = y[mask]

        # compute loss
        loss1 = self._loss_cls(y_train_phones, y_prob1_train_phones)
        loss2 = self._loss_cls(y_train_phones, y_prob2_train_phones)

        # update loss
        self._test_loss1.update_state(loss1)
        self._test_loss2.update_state(loss2)

        # update accuracy using training phones
        self._test_acc1_train_phones.update_state(y_train_phones,
                                                  y_prob1_train_phones)
        self._test_acc2_train_phones.update_state(y_train_phones,
                                                  y_prob2_train_phones)

        # update accuracy using test phones
        self._test_acc1.update_state(y, y_pred1)
        self._test_acc2.update_state(y, y_pred2)
Esempio n. 10
0
class SBVAT(SupervisedModel):
    """
        Implementation of sample-based Batch Virtual Adversarial Training  Graph Convolutional Networks (SBVAT). 
        [Batch Virtual Adversarial Training for Graph Convolutional Networks](https://arxiv.org/pdf/1902.09192)
        Tensorflow 1.x implementation: https://github.com/thudzj/BVAT

        Arguments:
        ----------
            adj: `scipy.sparse.csr_matrix` (or `csc_matrix`) with shape (N, N)
                The input `symmetric` adjacency matrix, where `N` is the number of nodes 
                in graph.
            features: `np.array` with shape (N, F)
                The input node feature matrix, where `F` is the dimension of node features.
            labels: `np.array` with shape (N,)
                The ground-truth labels for all nodes in graph.
            n_samples (Positive integer, optional): 
                The number of sampled subset nodes in the graph where the shortest path 
                length between them is at least 4. (default :obj: `50`)
            normalize_rate (Float scalar, optional): 
                The normalize rate for adjacency matrix `adj`. (default: :obj:`-0.5`, 
                i.e., math:: \hat{A} = D^{-\frac{1}{2}} A D^{-\frac{1}{2}}) 
            normalize_features (Boolean, optional): 
                Whether to use row-normalize for node feature matrix. 
                (default :obj: `True`)
            device (String, optional): 
                The device where the model is running on. You can specified `CPU` or `GPU` 
                for the model. (default: :obj: `CPU:0`, i.e., the model is running on 
                the 0-th device `CPU`)
            seed (Positive integer, optional): 
                Used in combination with `tf.random.set_seed & np.random.seed & random.seed` 
                to create a reproducible sequence of tensors across multiple calls. 
                (default :obj: `None`, i.e., using random seed)
            name (String, optional): 
                Name for the model. (default: name of class)

    """
    
    def __init__(self, adj, features, labels, n_samples=100, 
                 normalize_rate=-0.5, normalize_features=True, device='CPU:0', seed=None, **kwargs):
    
        super().__init__(adj, features, labels, device=device, seed=seed, **kwargs)
        
        self.normalize_rate = normalize_rate
        self.normalize_features = normalize_features            
        self.preprocess(adj, features)
        self.n_samples = n_samples

    def preprocess(self, adj, features):
        
        if self.normalize_rate is not None:
            adj = self._normalize_adj(adj, self.normalize_rate)        
            
        if self.normalize_features:
            features = self._normalize_features(features)
            
        self.neighbors = list(find_4o_nbrs(adj.indices, adj.indptr, np.arange(self.n_nodes)))

        with self.device:
            self.features, self.adj = self._to_tensor([features, adj])


    def build(self, hidden_layers=[16], activations=['relu'], dropout=0.5, 
              learning_rate=0.01, l2_norm=5e-4, p1=1., p2=1., 
              n_power_iterations=1, epsilon=0.03, xi=1e-6):
        
        with self.device:
            
            x = Input(batch_shape=[self.n_nodes, self.n_features], dtype=tf.float32, name='features')
            adj = Input(batch_shape=[self.n_nodes, self.n_nodes], dtype=tf.float32, sparse=True, name='adj_matrix')
            index = Input(batch_shape=[None],  dtype=tf.int32, name='index')

            self.GCN_layers = [GraphConvolution(hidden_layers[0], 
                                                activation=activations[0], 
                                                kernel_regularizer=regularizers.l2(l2_norm)),
                               GraphConvolution(self.n_classes)]
            self.dropout_layer = Dropout(dropout)
            
            logit = self.propagation(x, adj)
            output = tf.gather(logit, index)
            output = Softmax()(output)
            model = Model(inputs=[x, adj, index], outputs=output)
    
            self.model = model
            self.train_metric = SparseCategoricalAccuracy()
            self.test_metric = SparseCategoricalAccuracy()
            self.optimizer = Adam(lr=learning_rate)
            self.built = True
            
        self.p1 = p1 # Alpha
        self.p2 = p2 # Beta
        self.xi = xi # Small constant for finite difference
        self.epsilon = epsilon # Norm length for (virtual) adversarial training
        self.n_power_iterations = n_power_iterations #  Number of power iterations
            
    def propagation(self, x, adj, training=True):
        h = x
        for layer in self.GCN_layers:
            h = self.dropout_layer(h, training=training)
            h = layer([h, adj])
        return h
    
    @tf.function
    def do_train_forward(self, sequence):
        
        with self.device:
            self.train_metric.reset_states()
            
            for inputs, labels in sequence:
                x, adj, index, adv_mask = inputs
                with tf.GradientTape() as tape:
                    logit = self.propagation(x, adj)
                    output = tf.gather(logit, index)
                    output = softmax(output)

                    loss = tf.reduce_mean(sparse_categorical_crossentropy(labels, output))
                    entropy_loss = entropy_y_x(logit)
                    vat_loss = self.virtual_adversarial_loss(x, adj, logit=logit, adv_mask=adv_mask)
                    loss += self.p1 * vat_loss + self.p2 * entropy_loss
            
                    self.train_metric.update_state(labels, output)

                trainable_variables = self.model.trainable_variables
                gradients = tape.gradient(loss, trainable_variables)
                self.optimizer.apply_gradients(zip(gradients, trainable_variables))

        return loss, self.train_metric.result()
            
    @tf.function
    def do_test_forward(self, sequence):
            
        with self.device:
            self.test_metric.reset_states()
            
            for inputs, labels in sequence:
                x, adj, index, _ = inputs
                logit = self.propagation(x, adj, training=False)
                output = tf.gather(logit, index)
                output = softmax(output)
                loss = tf.reduce_mean(sparse_categorical_crossentropy(labels, output))
                self.test_metric.update_state(labels, output)
            
        return loss, self.test_metric.result()
        
        
    def do_forward(self, sequence, training=True):
        if training:
            loss, accuracy = self.do_train_forward(sequence)
        else:
            loss, accuracy = self.do_test_forward(sequence)
            
        return loss.numpy(), accuracy.numpy()

    
    def virtual_adversarial_loss(self, x, adj, logit, adv_mask):
        d = tf.random.normal(shape=tf.shape(x))
        
        for _ in range(self.n_power_iterations):
            d = get_normalized_vector(d) * self.xi
            logit_p = logit
            with tf.GradientTape() as tape:
                tape.watch(d)
                logit_m = self.propagation(x + d, adj)
                dist = kl_divergence_with_logit(logit_p, logit_m, adv_mask)
            grad = tape.gradient(dist, d)
            d = tf.stop_gradient(grad)

        r_vadv = get_normalized_vector(d) * self.epsilon
        logit_p = tf.stop_gradient(logit)
        logit_m = self.propagation(x + r_vadv, adj)
        loss = kl_divergence_with_logit(logit_p, logit_m, adv_mask)
        return tf.identity(loss)    

    
    def train_sequence(self, index):
        index = self._check_and_convert(index)
        labels = self.labels[index]
           
        with self.device:
            sequence = NodeSampleSequence([self.features, self.adj, index], labels,
                                          neighbors=self.neighbors,
                                          n_samples=self.n_samples)
            
        return sequence    
    
    def test_sequence(self, index):
        index = self._check_and_convert(index)
        labels = self.labels[index]
           
        with self.device:
            sequence = NodeSampleSequence([self.features, self.adj, index], labels,
                                          neighbors=self.neighbors,
                                          n_samples=self.n_samples,
                                          resample=False)
            
        return sequence        
    
    def predict(self, index):
        super().predict(index)
        index = self._check_and_convert(index)
        
        with self.device:
            sequence = NodeSampleSequence([self.features, self.adj, index], None,
                                          neighbors=self.neighbors,
                                          n_samples=self.n_samples,
                                          resample=False)
            for inputs, _ in sequence:
                x, adj, index, adv_mask = inputs
                output = self.propagation(x, adj, training=False)
                logit = softmax(tf.gather(output, index))
                
        return logit.numpy()      
Esempio n. 11
0
            # forward pass of student model
            student_pred = stud_model(x, training=True)
            assert stud_model.trainable == True, 'Student model should be trainable'

            # hard labels loss
            loss_hard = cross_entr(y, student_pred)

            # soft labels loss with temperature temp
            loss_soft = kl_div(tf.nn.softmax(teacher_pred / temp),
                               tf.nn.softmax(student_pred / temp))

            # final loss value
            loss = alpha * loss_hard + (1 - alpha) * loss_soft

        # update train accuracy metric
        train_acc_metric.update_state(y, student_pred)

        # calculate gradients
        grads = tape.gradient(loss, stud_model.weights)

        # gradient descent
        optimizer.apply_gradients(zip(grads, stud_model.trainable_weights))

        # print some info
        print("Epoch {}, step {}, loss {:5f}".format(ep, step, loss))

    # get result of train accuracy metric
    print("Train accuracy is {:4f}".format(train_acc_metric.result()))

    # reset metric
    train_acc_metric.reset_states()