with – NOT saving!’) print(”) print(‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ‘) print(‘Convergence

with tf.Session() as sess:    tf.set_random_seed(1) # SEED    np.random.seed(1) # SEED        dev_scores = ## SS – var used for early stopping    sess.run(tf.initialize_all_variables())    for i in range(50):        timestamp = time.time()        total_loss = 0        shuffle(training_indices)        for j in range(train_size // BATCH_SIZE):            batch_indices = training_indicesj * BATCH_SIZE: (j + 1) * BATCH_SIZE #JC            batch_feed_dict = {                story_pl: train_storiesbatch_indices,                labels_pl: train_ordersbatch_indices,                seq_len: seq_len_batch,                dropout_pl: global_dropout,                learn_rate: learn_rate_base * (learn_rate_discount ** i)            }            _, current_loss = sess.run(run_optimizer, loss, feed_dict=batch_feed_dict)            total_loss += current_loss                        if j in checkpoints:                dev_feed_dict = {                    story_pl: dev_stories,                    labels_pl: dev_orders,                    seq_len: seq_len_dev,                    dropout_pl: 1.0,                    learn_rate: learn_rate_base                }                dev_predictions = sess.run(predict, feed_dict=dev_feed_dict)                dev_accuracy = nn.calculate_accuracy(dev_orders, dev_predictions) * 100                dev_accuracies.append(dev_accuracy)                print(dev_accuracy)                if dev_accuracy == max(dev_accuracies):                    nn.save_model(sess)                                        train_feed_dict = {            story_pl: train_stories,            labels_pl: train_orders,            seq_len: seq_len_train,            dropout_pl: global_dropout,            learn_rate: learn_rate_base * (learn_rate_discount ** i)        }        dev_feed_dict = {            story_pl: dev_stories,            labels_pl: dev_orders,            seq_len: seq_len_dev,            dropout_pl: 1.0,            learn_rate: learn_rate_base        }        train_predictions = sess.run(predict, feed_dict=train_feed_dict)        train_accuracy = nn.calculate_accuracy(train_orders, train_predictions) * 100        dev_predictions = sess.run(predict, feed_dict=dev_feed_dict)        dev_accuracy = nn.calculate_accuracy(dev_orders, dev_predictions) * 100        dev_accuracies.append(dev_accuracy)        if dev_accuracy == max(dev_accuracies):            nn.save_model(sess)        print(“Epoch: {0} | Loss: {1:>7,.0f} | Time: {2:.0f}s | Train acc: {3:.2f}% | Dev acc: {4:.2f}%”.              format(i+1,total_loss,time.time()-timestamp,train_accuracy,dev_accuracy))        ################### EARLY STOPPING SS ###################        if (early_stopping == True) and (i >= 1):            best = 0            if (dev_accuracy – max(dev_scores)) < -1:                print('Model suboptimal - NOT saving!')                print('')                print('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ')                print('Convergence Criterium met: Stopping Computation ')                print('')                print('Accuracy of Best Model / Saved Model on dev set: '+ str(round(max(dev_scores), 2))+'%')                break            else:                if dev_accuracy >= max(dev_scores):                    dev_scores.append(dev_accuracy)                    print(‘Best model so far – Saving!’)                    nn.save_model(sess)                    # Best model will be saved and used in final assessment                else:                    dev_scores.append(dev_accuracy)                    print(‘Model suboptimal – NOT saving!’)        else:            if i == 0:                print(‘Best model so far – Saving!’)#                 nn.save_model(sess) # Always keeps best model saved            else:                print(‘Early Stopping set to “False” – Saving!’)#                 nn.save_model(sess) # Always save current model if early_stopping deactivated

Author: