Exemplo n.º 1
0
def do_train_noweights(model, dataset_batches):
    optimizer = AdamOptimizer()

    loss_history = []

    for (batch, (invec, labels)) in enumerate(dataset_batches.take(1000)):

        with tf.GradientTape() as tape:
            logits = model(invec, training=True)
            loss_value = sparse_softmax_cross_entropy(labels, logits)

        loss_history.append(loss_value.numpy())
        grads = tape.gradient(loss_value, model.trainable_variables)
        optimizer.apply_gradients(
            zip(grads, model.trainable_variables),
            global_step=tf.compat.v1.train.get_or_create_global_step())

    return loss_history
Exemplo n.º 2
0
def do_train(model,
             dataset_batches,
             k,
             epochs=None,
             num_epochs=None,
             optimizer_params=None,
             saveloc=None):
    optimizer = AdamOptimizer(**optimizer_params)

    loss_history = []
    label_losses_history = []

    if num_epochs:
        epochs = range(num_epochs)

    #weights = tf.constant(np.ones((32, 1)))
    weights_dict = {0: 1., 1: 1., 2: 1., 3: 1.}
    for epoch in epochs:

        for (batch, (invec, labels,
                     _)) in enumerate(tqdm(dataset_batches.take(k))):
            if labels.shape[0] < 32:
                continue
            weights = np.array(
                [weights_dict[labels[i].numpy()] for i in range(32)])

            # NOTE: is this tape in the right place?
            with tf.GradientTape() as tape:
                logits = model(invec, training=True)
                loss_value = sparse_softmax_cross_entropy(labels,
                                                          logits,
                                                          weights=weights)

                indices_vec = [[
                    i for i in range(32) if labels[i].numpy() == label
                ] for label in [0, 1, 2, 3]]

                losses = [
                    sparse_softmax_cross_entropy(labels.numpy()[indices],
                                                 logits.numpy()[indices],
                                                 weights=weights[indices])
                    for indices in indices_vec
                ]
                weights_dict = weights_for_losses(losses)

            loss_history.append(loss_value.numpy())
            label_losses_history.append([x.numpy() for x in losses])
            grads = tape.gradient(loss_value, model.trainable_variables)
            optimizer.apply_gradients(
                zip(grads, model.trainable_variables),
                global_step=tf.compat.v1.train.get_or_create_global_step())

            prefix = (f'{saveloc}/epoch_{str(epoch).zfill(3)}'
                      f'_batch_{str(batch).zfill(5)}')

            if batch % 10 == 0:
                save_model(model, (f'{prefix}_model.h5'))

                to_json_local(loss_history,
                              f'{prefix}_train_loss_history.json')

                to_json_local(label_losses_history,
                              f'{prefix}_train_label_losses_history.json')

    return loss_history