Exemplo n.º 1
0
    def trainStep(self, images, noiseAndC):
        with GradientTape() as genTape, GradientTape(
        ) as disTape, GradientTape() as cTape:
            generatedImages = self.generator(noiseAndC, training=True)

            realOutput = self.discriminator(images, training=True)
            fakeOutput = self.discriminator(generatedImages, training=True)
            cApprox = self.qNet(generatedImages, training=True)

            genLoss = generatorLoss(fakeOutput, cApprox, self.batchSize)
            disLoss = discriminatorLoss(realOutput, fakeOutput, cApprox,
                                        self.batchSize)
            qNetLoss = qLoss(cApprox, self.batchSize)

        gradientsGenerator = genTape.gradient(
            genLoss, self.generator.trainable_variables)
        gradientsDiscriminator = disTape.gradient(
            disLoss, self.discriminator.trainable_variables)
        gradientsQnet = cTape.gradient(qNetLoss, self.qNet.trainable_variables)

        self.genOpt.apply_gradients(
            zip(gradientsGenerator, self.generator.trainable_variables))
        self.disOpt.apply_gradients(
            zip(gradientsDiscriminator,
                self.discriminator.trainable_variables))
        self.qNetOpt.apply_gradients(
            zip(gradientsQnet, self.qNet.trainable_variables))
        #return realOutput, fakeOutput, cApprox, qNetLoss, genLoss, disLoss
        return qNetLoss
Exemplo n.º 2
0
    def gradient_panalty(loss_type: str, gradtape: tf.GradientTape,
                         discriminator: tf.keras.Model, real: tf.Tensor,
                         fake: tf.Tensor, ld: float):
        # 梯度惩罚
        if 'dragan' in loss_type:
            eps = tf.random.uniform(tf.shape(real), 0., 1.)
            _, x_var = tf.nn.moments(real, axes=[0, 1, 2, 3])
            # magnitude of noise decides the size of local region
            x_std = tf.sqrt(x_var)

            fake = real + 0.5 * x_std * eps

        batch = tf.shape(real)[0]
        alpha = tf.random.uniform([batch, 1, 1, 1], 0., 1.)
        interpolated = real + alpha * (fake - real)
        gradtape.watch(interpolated)
        logit = discriminator(interpolated, training=True)
        with gradtape.stop_recording():
            # gradient of D(interpolated) NOTE : should test more
            grad = gradtape.gradient(logit, interpolated)[0]
        grad_norm = tf.norm(tf.keras.layers.Flatten()(grad), axis=1)  # l2 norm

        gp_loss = 0
        # WGAN - LP
        if 'lp' in loss_type:
            gp_loss = ld * tf.reduce_mean(
                tf.square(tf.maximum(0.0, grad_norm - 1.)))
        elif 'gp' in loss_type or 'dragan' == loss_type:
            gp_loss = ld * tf.reduce_mean(tf.square(grad_norm - 1.))

        return gp_loss
Exemplo n.º 3
0
    def train_step(self, images, low_res):

        with GradientTape() as gen_tape, GradientTape() as disc_tape:
            generated_images = self.generator(low_res, training=True)
            mse = MeanSquaredError()
            loss = mse(images, generated_images)
            if loss < self.min_loss and self.count > 200:
                gan_name = 'generator.h5'
                self.generator.save(gan_name)
                self.discriminator.save('discriminator.h5')
                self.min_loss = loss

            real_output = self.discriminator(images, training=True)
            fake_output = self.discriminator(generated_images, training=True)

            gen_loss = self.generator_loss(fake_output) + loss
            disc_loss = self.discriminator_loss(real_output, fake_output) + loss
            if self.count % 100 == 0:
                print(self.count, self.min_loss, gen_loss, disc_loss)
        gradients_of_generator = gen_tape.gradient(gen_loss, self.generator.trainable_variables)
        gradients_of_discriminator = disc_tape.gradient(disc_loss, self.discriminator.trainable_variables)

        self.generator_optimizer.apply_gradients(zip(gradients_of_generator, self.generator.trainable_variables))
        self.discriminator_optimizer.apply_gradients(
            zip(gradients_of_discriminator, self.discriminator.trainable_variables))
Exemplo n.º 4
0
    def _apply_backwards_pass(self, loss: tf.Tensor,
                              tape: tf.GradientTape) -> None:
        print("Executing NatGradModel backwards pass")
        # TODO(Ti) This is to check tf.function() compilation works and this
        # won't be called repeatedly. Surprisingly, it gets called *twice*...
        # Leaving the print here until we've established why twice, or whether
        # it's irrelevant, and that it all works correctly in practice.

        variational_params, other_vars = self._split_natgrad_params_and_other_vars(
        )
        variational_params_vars = [(q_mu.unconstrained_variable,
                                    q_sqrt.unconstrained_variable)
                                   for (q_mu, q_sqrt) in variational_params]

        variational_params_grads, other_grads = tape.gradient(
            loss, (variational_params_vars, other_vars))

        num_natgrad_opt = len(self.natgrad_optimizers)
        num_variational = len(variational_params)
        if len(self.natgrad_optimizers) != len(variational_params):
            raise ValueError(
                f"Model has {num_natgrad_opt} NaturalGradient optimizers, "
                f"but {num_variational} variational distributions"
            )  # pragma: no cover

        for (natgrad_optimizer, (q_mu_grad, q_sqrt_grad),
             (q_mu, q_sqrt)) in zip(self.natgrad_optimizers,
                                    variational_params_grads,
                                    variational_params):
            natgrad_optimizer._natgrad_apply_gradients(q_mu_grad, q_sqrt_grad,
                                                       q_mu, q_sqrt)

        self.optimizer.apply_gradients(zip(other_grads, other_vars))
Exemplo n.º 5
0
        def disc_train_step(g_queue, noise_size, batch, r_labels, f_labels):

            nonlocal d_loss_current

            noise = np.random.normal(0, 1, (noise_size, self.latent_dims))

            ims_arr = []
            val_arr = []
            for gen in g_queue:
                ims_arr.extend(gen(noise))
                val_arr.extend(f_labels)
            
            ims_arr.extend(batch)
            val_arr.extend(r_labels)

            ims_arr = np.array(ims_arr)
            val_arr = np.array(val_arr)
            shuffle_unison(ims_arr, val_arr)

            with GradientTape() as d_tape:

                preds = self.discriminator(ims_arr)
                d_loss = compute_loss(val_arr, preds, d_loss_object)

            d_loss_current = d_loss

            d_grad = d_tape.gradient(d_loss, self.discriminator.trainable_weights)
            self.d_optimizer.apply_gradients(zip(d_grad, self.discriminator.trainable_weights))

            return d_loss
Exemplo n.º 6
0
def make_gradcam_heatmap(img_array, model, last_conv_layer_name,
                         classifier_layer_names):
    # First, we create a model that maps the input image to the activations of the last conv layer
    last_conv_layer = model.get_layer(last_conv_layer_name)
    last_conv_layer_model = Model(model.inputs, last_conv_layer.output)
    # Second, we create a model that maps the activations of the last conv layer to the final class predictions
    classifier_input = Input(shape=last_conv_layer.output.shape[1:])
    x = classifier_input
    for layer_name in classifier_layer_names:
        x = model.get_layer(layer_name)(x)
    classifier_model = Model(classifier_input, x)
    # Then, we compute the gradient of the top predicted class for our input image with respect to the activations of the last conv layer
    with GradientTape() as tape:
        # Compute activations of the last conv layer and make the tape watch it
        last_conv_layer_output = last_conv_layer_model(img_array)
        tape.watch(last_conv_layer_output)
        # Compute class predictions
        preds = classifier_model(last_conv_layer_output)
        top_pred_index = argmax(preds[0])
        top_class_channel = preds[:, top_pred_index]
    # This is the gradient of the top predicted class with regard to the output feature map of the last conv layer
    grads = tape.gradient(top_class_channel, last_conv_layer_output)
    # This is a vector where each entry is the mean intensity of the gradient over a specific feature map channel
    pooled_grads = reduce_mean(grads, axis=(0, 1, 2))
    # We multiply each channel in the feature map array by "how important this channel is" with regard to the top predicted class
    last_conv_layer_output = last_conv_layer_output.numpy()[0]
    pooled_grads = pooled_grads.numpy()
    for i in range(pooled_grads.shape[-1]):
        last_conv_layer_output[:, :, i] *= pooled_grads[i]
    # The channel-wise mean of the resulting feature map is our heatmap of class activation
    heatmap = np.mean(last_conv_layer_output, axis=-1)
    heatmap = np.maximum(heatmap, 0) / np.max(heatmap)
    return heatmap, top_pred_index
    def train(self,X,Y,Yi,learningRate,L2val):

        #very sorry L2 is currtently out of order I will fix this later

        #apply L2 regularization to avoid overfitting
        #this is really really important
        #regularizer=l2(L2val)#just to be clear this is tf.keras.regularizers.l2
        #regularizer(self.weights)

        #compute gradients of weights and biases
        with GradientTape() as g:
            for i in range(len(self.nHidden)+1):#iterate over layers
                g.watch(self.getTrainableVariables())

            #calculate error
            guess=self.evaluate([[constant(j) for j in i] for i in X])#convert everything in x to tensorflow format
            #calculate error using sqared error
            error=0
            for i in range(len(Y)):
                error+=(guess[i][Yi[i]]-Y[i][Yi[i]])**2
            error=error/len(Y)

        optimizer=Adam(learningRate)
        grads=g.gradient(error,self.getTrainableVariables())
        optimizer.apply_gradients(zip(grads,self.getTrainableVariables()),)
        return error
Exemplo n.º 8
0
    def train(self,X,Y,learningRate,indexes=None):

        #very sorry L2 is currtently out of order I will fix this later

        #apply L2 regularization to avoid overfitting
        #this is really really important
        #regularizer=l2(L2val)#just to be clear this is tf.keras.regularizers.l2
        #regularizer(self.weights)

        #compute gradients of weights and biases
        with GradientTape() as g:
            myTrainableVariables=self.getTrainableVariables()
            g.watch(myTrainableVariables)

            #calculate error
            if self.debug:
                print("EXECUTING")
            guess=self.evaluate(X)
            #calculate error using sqared error
            if self.debug:
                print("TRAINING")
            if self.errorFunction.multipleLabels:
                error=self.errorFunction.execute(guess,Y)
            else:
                error=self.errorFunction.execute(guess,Y,indexes)

        optimizer=Adam(learningRate)
        grads=g.gradient(error,myTrainableVariables)
        optimizer.apply_gradients(zip(grads,myTrainableVariables),)
        return error
Exemplo n.º 9
0
 def train(self,
           feature_value,
           embedding_index,
           label,
           optimizer='adam',
           learning_rate=1e-4,
           loss='sigmoid',
           epochs=50,
           batch=32,
           shuffle=10000):
     for epoch in range(epochs):
         train_set = tensorflow.data.Dataset.from_tensor_slices(
             (feature_value, embedding_index,
              label)).shuffle(shuffle).batch(batch, drop_remainder=True)
         for batch_set in train_set:
             with GradientTape() as tape:
                 prediction = self.call(feature_value=batch_set[0],
                                        embedding_index=batch_set[1],
                                        training=True)
                 self.loss_obj = get_loss(loss)
                 self.optimizer = get_optimizer(optimizer,
                                                learning_rate=learning_rate)
                 batch_loss = self.loss_obj(batch_set[2], prediction)
             gradients = tape.gradient(batch_loss, self.trainable_variables)
             self.optimizer.apply_gradients(
                 zip(gradients, self.trainable_variables))
         mean_loss = Mean(name='train_loss')
         print('epoch: {} ==> loss: {}'.format(epoch + 1,
                                               mean_loss(batch_loss)))
Exemplo n.º 10
0
    def learn(self, gamma: float, frame_number: int, priority_scale: float=1.0) -> Tuple[float, np.ndarray]:
        if self.use_per:
            (states, actions, rewards, new_states, terminal), importance, indices = self.replay_buffer.get_minibatch(self.batch_size, priority_scale=priority_scale)
            importance = importance ** (1-self.calc_epsilon(frame_number))
        else:
            states, actions, rewards, new_states, terminal = self.replay_buffer.get_minibatch(self.batch_size, priority_scale=priority_scale)
        
        main_q_values = self.main_q.predict(new_states).argmax(axis=1)
        
        future_q_values = self.target_q.predict(new_states)
        double_q = future_q_values[range(self.batch_size), main_q_values] # bad use of range
        
        target_q = rewards + (gamma * double_q * (1 - terminal)) # error with terminal
        
        with GradientTape() as tape:
            q_values = self.main_q(states)
            
            #
            one_hot_actions = to_categorical(actions, self.n_actions, dtype=np.float32)
            Q = tf.reduce_sum(tf.multiply(q_values, one_hot_actions), axis=1)
            
            error = Q - target_q
            loss = Huber()(target_q, Q)
            
            if self.use_per:
                loss = tf.reduce_mean(loss * importance)

        gradients = tape.gradient(loss, self.main_q.trainable_variables)
        self.main_q.optimizer.apply_gradients(zip(gradients, self.main_q.trainable_variables))
        
        if self.use_per:
            self.replay_buffer.set_priorities(indices, error)
        
        return float(loss.numpy()), error
Exemplo n.º 11
0
    def learn(self):
        actions = np.array(self.action_memory)  # Get actions taken
        G = self.calc_advantages()  # Calc advantages of each action

        with GradientTape() as tape:  # Calculate gradients for all weights
            loss = 0
            for idx, (g, state) in enumerate(zip(G, self.state_memory)):
                state = convert_to_tensor(
                    [state], dtype='float32')  # Makes training faster
                probs = self.policy(state, training=False)[
                    0]  # Get probability predictions based on each step state
                action_probs = distributions.Categorical(
                    probs=probs)  # Create distribution based on probabilities
                log_prob = action_probs.log_prob(
                    actions[idx]
                )  # Find logistic probability of action being taken

                loss += -g * squeeze(
                    log_prob
                )  # Update loss based on reward and model's certainty

        # Calculate gradients from observing tape, then apply them to the policy weights
        gradient = tape.gradient(loss, self.policy.trainable_variables)
        self.policy.optimizer.apply_gradients(
            zip(gradient, self.policy.trainable_variables))

        self.clear_steps()  # Clear step memory
Exemplo n.º 12
0
    def train_generator(self, x, z):
        with GradientTape() as tape:
            y_fake = self.adversarial_supervised(z)
            generator_loss_unsupervised = self._bce(y_true=ones_like(y_fake),
                                                    y_pred=y_fake)

            y_fake_e = self.adversarial_embedded(z)
            generator_loss_unsupervised_e = self._bce(
                y_true=ones_like(y_fake_e), y_pred=y_fake_e)
            h = self.embedder(x)
            h_hat_supervised = self.supervisor(h)
            generator_loss_supervised = self._mse(h[:, 1:, :],
                                                  h_hat_supervised[:, 1:, :])

            x_hat = self.generator(z)
            generator_moment_loss = self.calc_generator_moments_loss(x, x_hat)

            generator_loss = (generator_loss_unsupervised +
                              generator_loss_unsupervised_e +
                              100 * sqrt(generator_loss_supervised) +
                              100 * generator_moment_loss)

        var_list = self.generator_aux.trainable_variables + self.supervisor.trainable_variables
        gradients = tape.gradient(generator_loss, var_list)
        self.generator_opt.apply_gradients(zip(gradients, var_list))
        return generator_loss_unsupervised, generator_loss_supervised, generator_moment_loss
Exemplo n.º 13
0
def __grad(model, x, y, loss):
    with GradientTape() as tape:
        loss_value = __calculate_loss(model, x, y, loss)['loss']
        return {
            'loss_value': loss_value,
            'grad_tape': tape.gradient(loss_value, model.trainable_variables),
        }
Exemplo n.º 14
0
def mpg_test(
    filename='/home/jimao/PycharmProjects/tensorflow/data/auto-mpg.data-original'
):
    train_datas, train_lables, test_datas, test_lables = dispose_mpg_data(
        filename)
    model = Network()
    # 通过 build 函数完成内部张量的创建,其中 4 为任意设置的 batch 数量,9 为输入特征长度
    model.build(input_shape=(4, 9))
    # 打印网络信息
    model.summary()
    train_db = data.Dataset.from_tensor_slices(
        (train_datas.values, train_lables.values))
    train_db = train_db.shuffle(100).batch(2)
    optimzer = keras.optimizers.RMSprop(.001)
    for epoch in range(200):
        for step, (x, y) in enumerate(train_db):
            with GradientTape() as tape:
                out = model(x)
                loss = reduce_mean(losses.MSE(y, out))
            # if step % 10 == 0:
            #     print(epoch, step, float(loss))
            grads = tape.gradient(loss, model.trainable_variables)
            optimzer.apply_gradients(zip(grads, model.trainable_variables))
    print(model.variables)
    print(model.trainable_variables)
Exemplo n.º 15
0
    def train_complete(self,
                       tape: tf.GradientTape,
                       training_info: TrainingInfo,
                       weight=1.0):
        """Complete one iteration of training.

        `train_complete` should calculate gradients and update parameters using
        those gradients.

        Args:
            tape (tf.GradientTape): the tape which are used for calculating
                gradient. All the previous `train_interval` `train_step()` for
                are called under the context of this tape.
            training_info (TrainingInfo): information collected for training.
                training_info.info are the batched from each policy_step.info
                returned by train_step()
            weight (float): weight for this batch. Loss will be multiplied with
                this weight before calculating gradient
        Returns:
            a tuple of the following:
            loss_info (LossInfo): loss information
            grads_and_vars (list[tuple]): list of gradient and variable tuples
        """
        valid_masks = tf.cast(
            tf.not_equal(training_info.step_type, StepType.LAST), tf.float32)

        # reward shaping
        if self._reward_shaping_fn is not None:
            # record unshaped extrinsic rewards given by the environment
            self.add_reward_summary("reward/raw", training_info.reward)
            training_info = training_info._replace(
                reward=self._reward_shaping_fn(training_info.reward))

        # record shaped extrinsic rewards actually used for training
        self.add_reward_summary("reward/extrinsic", training_info.reward)

        with tape:
            loss_info = self.calc_loss(training_info)
            loss_info = tf.nest.map_structure(
                lambda l: tf.reduce_mean(l * valid_masks), loss_info)
            loss = weight * loss_info.loss

        var_sets = self._get_cached_var_sets()
        all_grads_and_vars = ()
        for vars, optimizer in zip(var_sets, self._optimizers):
            grads = tape.gradient(loss, vars)
            grads_and_vars = tuple(zip(grads, vars))
            all_grads_and_vars = all_grads_and_vars + grads_and_vars
            if self._gradient_clipping is not None:
                if self._clip_by_global_norm:
                    grads, _ = tf.clip_by_global_norm(grads,
                                                      self._gradient_clipping)
                    grads_and_vars = tuple(zip(grads, vars))
                else:
                    grads_and_vars = eager_utils.clip_gradient_norms(
                        grads_and_vars, self._gradient_clipping)

            optimizer.apply_gradients(grads_and_vars)

        return loss_info, all_grads_and_vars
Exemplo n.º 16
0
    def train_discriminator(self, x, z):
        with GradientTape() as tape:
            discriminator_loss = self.discriminator_loss(x, z)

        var_list = self.discriminator.trainable_variables
        gradients = tape.gradient(discriminator_loss, var_list)
        self.discriminator_opt.apply_gradients(zip(gradients, var_list))
        return discriminator_loss
def apply_gradient_descent(X, model, optimizer, y):
    with GradientTape() as tape:
        ŷ = model(X, training=True)
        loss_value = model.compiled_loss(y, ŷ, regularization_losses=model.losses)
    gd_weights = model.trainable_variables
    grads = tape.gradient(loss_value, gd_weights)
    optimizer.apply_gradients(zip(grads, gd_weights))
    model._trainable_variables = gd_weights
Exemplo n.º 18
0
def do_gradops(opt,loss,model,insh,t=None):
    with GradientTape() as tape:
        y = do_eval(model, insh)[1]
        if t == None:
            t = uniform(shape(y))
        l = loss(t,y)
    grad = tape.gradient(l,model.trainable_weights)
    opt.apply_gradients(zip(grad,model.trainable_weights))
Exemplo n.º 19
0
    def train(features, labels):
        with GradientTape() as tape:
            predictions = model(features)
            loss = loss_fn(labels, predictions)
        grad = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(grad, model.trainable_variables))

        train_loss(loss)
        train_accuracy(labels, predictions)
Exemplo n.º 20
0
    def train_step(self, images):
        noise = random.normal([self.batchSize, self.noiseDim])

        with GradientTape() as gen_tape, GradientTape() as disc_tape:
            generated_images = self.generator(noise, training=True)

            real_output = self.discriminator(images, training=True)
            fake_output = self.discriminator(generated_images, training=True)

            gen_loss = self.generator_loss(fake_output)
            disc_loss = self.discriminator_loss(real_output, fake_output)

        gradients_of_generator = gen_tape.gradient(gen_loss, self.generator.trainable_variables)
        gradients_of_discriminator = disc_tape.gradient(disc_loss, self.discriminator.trainable_variables)

        self.generatorOptimizer.apply_gradients(zip(gradients_of_generator, self.generator.trainable_variables))
        self.discriminatorOptimizer.apply_gradients(
            zip(gradients_of_discriminator, self.discriminator.trainable_variables))
Exemplo n.º 21
0
def compute_gradient(x0):
    # Define x as a variable with an initial value of x0
    x = Variable(x0)
    with GradientTape() as tape:
        tape.watch(x)
        # Define y using the multiply operation
        y = multiply(x, x)
    # Return the gradient of y with respect to x
    return tape.gradient(y, x).numpy()
Exemplo n.º 22
0
def train_model(
        model: Union[Model, NaiveModel],
        optimizer: Optimizer,
        loss_fn: Loss,
        batch_streamer_train: BatchStreamer,
        batch_streamer_test: BatchStreamer,
        epochs: int = 10,
        ) -> Union[Model, NaiveModel]:
    """
    Train the previously built model as described in the paper.
    """
    loss_per_epoch_train = []
    loss_per_epoch_test = []

    for epoch in range(epochs):
        print(f'Current Epoch: {epoch}')

        # loss for each mini-batch
        loss_array_train = []
        loss_array_test = []

        # TRAIN
        batch_streamer_train.reset() # ensure that all iterators are reset
        for H, F, C, P in tqdm(batch_streamer_train):

            with GradientTape() as tape:
                # generate and evaluate predictions
                sigmoid_proba = model([H, F, C], training=True)
                loss_value = loss_fn(P, sigmoid_proba)
                loss_array_train.append(loss_value)

            if isinstance(model, NaiveModel):
                pass
            else:
                # update weights with computed gradients
                grads = tape.gradient(loss_value, model.trainable_weights)
                optimizer.apply_gradients(zip(grads, model.trainable_weights))

        mean_loss_current_epoch_train = np.array(loss_array_train).mean()
        loss_per_epoch_train.append(mean_loss_current_epoch_train)
        print(f'Avg loss train: {mean_loss_current_epoch_train}')

        # TEST
        batch_streamer_test.reset()
        for H, F, C, P in tqdm(batch_streamer_test):

            # generate and evaluate predictions
            sigmoid_proba=model([H, F, C], training=False)
            loss_value = loss_fn(P, sigmoid_proba)
            loss_array_test.append(loss_value)

        mean_loss_current_epoch_test = np.array(loss_array_test).mean()
        loss_per_epoch_test.append(mean_loss_current_epoch_test.copy())
        print(f'Avg loss train: {mean_loss_current_epoch_test}')

    return model
Exemplo n.º 23
0
 def learn(self, cur_state, action, new_state, reward):
     cur_state = self.state2vec(cur_state)
     new_state = self.state2vec(new_state)
     with GradientTape() as actor_tape, GradientTape() as critic_tape:
         prob = self.actor(state, training=True)
         value = self.critic(state, training=True)
         value_new = self.critic(new_state, training=True)
         temporal_diff = reward + self.gamma * value_new - value
         actor_loss = self.actor_loss(prob, action, temporal_diff)
         critic_loss = temporal_diff**2
     actor_grads = actor_tape.gradient(actor_loss,
                                       self.actor.trainable_variables)
     critic_grads = critic_tape.gradient(critic_loss,
                                         self.critic.trainable_variables)
     self.actor_opt.apply_gradients(
         zip(actor_grads, self.actor.trainable_variables))
     self.critic_opt.apply_gradients(
         zip(critic_grads, self.critic.trainable_variables))
     return actor_loss, critic_loss
Exemplo n.º 24
0
    def train_autoencoder(self, x):
        with GradientTape() as tape:
            x_tilde = self.autoencoder(x)
            embedding_loss_t0 = self._mse(x, x_tilde)
            e_loss_0 = 10 * sqrt(embedding_loss_t0)

        var_list = self.embedder.trainable_variables + self.recovery.trainable_variables
        gradients = tape.gradient(e_loss_0, var_list)
        self.autoencoder_opt.apply_gradients(zip(gradients, var_list))
        return sqrt(embedding_loss_t0)
Exemplo n.º 25
0
    def train_supervisor(self, x):
        with GradientTape() as tape:
            h = self.embedder(x)
            h_hat_supervised = self.supervisor(h)
            g_loss_s = self._mse(h[:, 1:, :], h_hat_supervised[:, 1:, :])

        var_list = self.supervisor.trainable_variables
        gradients = tape.gradient(g_loss_s, var_list)
        self.supervisor_opt.apply_gradients(zip(gradients, var_list))
        return g_loss_s
Exemplo n.º 26
0
    def train(self):
        """
        Performs one step of model training.
        """
        batch_size = min(self.batch_size, len(self.memory))
        minibatch = random.sample(self.memory, batch_size)

        state = [mb[0] for mb in minibatch]
        action = [mb[1] for mb in minibatch]
        reward = [mb[2] for mb in minibatch]
        next_state = [mb[3] for mb in minibatch]
        done = [mb[4] for mb in minibatch]

        states = convert_to_tensor(state, dtype=float32)
        actions = convert_to_tensor(action, dtype=float32)
        rewards = convert_to_tensor(reward, dtype=float32)
        next_states = convert_to_tensor(next_state, dtype=float32)
        dones = convert_to_tensor(done, dtype=float32)

        with GradientTape() as tape:
            target_actions = self.target_actor(next_states)
            critic_value_ = squeeze(
                self.target_critic([next_states, target_actions]), 1)
            critic_value = squeeze(self.critic([states, actions]), 1)
            target = rewards + self.discount_factor * critic_value_ * (1 -
                                                                       dones)
            critic_loss = MSE(target, critic_value)

        critic_network_gradient = tape.gradient(
            critic_loss, self.critic.trainable_variables)
        self.critic.optimizer.apply_gradients(
            zip(critic_network_gradient, self.critic.trainable_variables))

        with GradientTape() as tape:
            new_policy_actions = self.actor(states)
            actor_loss = -self.critic([states, new_policy_actions])
            actor_loss = reduce_mean(actor_loss)

        actor_network_gradient = tape.gradient(actor_loss,
                                               self.actor.trainable_variables)
        self.actor.optimizer.apply_gradients(
            zip(actor_network_gradient, self.actor.trainable_variables))
Exemplo n.º 27
0
def optimize(tape: tf.GradientTape, model: tf.keras.Model, loss: tf.Tensor) -> None:
  """ This optimizes a model with respect to its loss

  Inputs:
  - tape: the Gradient Tape
  - model: the model to be trained
  - loss: the model's loss
  """
  # calculate the gradients our input model and apply them
  gradients = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(zip(gradients, model.trainable_variables))
Exemplo n.º 28
0
        def train_step(model_, id_, mk_, type_ids_, optimizer_, target_):
            with GradientTape() as tp:
                y_pred = model_(id_, mk_, type_ids_)
                loss_value = loss_fn(target=target_, output=y_pred)
                # y_pred = [round(y_p) for y_p in y_pred]
                acc = accuracy_fn(target_, y_pred)
            gradient = tp.gradient(loss_value, model.trainable_variables)

            optimizer_.apply_gradients(zip(gradient,
                                           model.trainable_variables))

            return loss_value, np.array(acc).mean(), y_pred
Exemplo n.º 29
0
    def train_supervisor(self, x, opt):
        with GradientTape() as tape:
            h = self.embedder(x)
            h_hat_supervised = self.supervisor(h)
            g_loss_s = self._mse(h[:, 1:, :], h_hat_supervised[:, 1:, :])

        var_list = self.supervisor.trainable_variables + self.generator.trainable_variables
        gradients = tape.gradient(g_loss_s, var_list)
        apply_grads = [(grad, var) for (grad, var) in zip(gradients, var_list)
                       if grad is not None]
        opt.apply_gradients(apply_grads)
        return g_loss_s
Exemplo n.º 30
0
    def _backpropagates_gradient(
        self,
        tape: tf.GradientTape,
        models: List[tf.keras.Model],
        loss: tf.float32,
        optimizer: tf.keras.optimizers.Adam,
    ) -> None:
        """ Backpropagates the gradient of the loss into the given networks"""

        trainable_variables = sum(
            [model.trainable_variables for model in models], [])
        gradients = tape.gradient(loss, trainable_variables)
        optimizer.apply_gradients(zip(gradients, trainable_variables))