def test_category_accuracy(self): layer = tl.CategoryAccuracy() targets = np.array([0, 1, 2]) model_outputs = np.array([[.7, .2, .1, 0.], [.2, .7, .1, 0.], [.2, .1, .7, 0.]]) accuracy = layer([model_outputs, targets]) self.assertEqual(accuracy, 1.0) model_outputs = np.array([[.2, .1, .7, 0.], [.2, .1, .7, 0.], [.2, .1, .7, 0.]]) accuracy = layer([model_outputs, targets]) self.assertEqual(accuracy, 1 / 3)
train_data, len_input, batch_size)) recommender = RecommenderTransformer(n_classes_in=len(classes), embedding_size=embedding_size, n_out_classes=len(classes), dropout_rate=dropout_rate) n_train_b = math.floor(float(len(train_data)) / float(batch_size)) train_task = ts.training.TrainTask( labeled_data=inputs_train, loss_layer=tl.CategoryCrossEntropy(), optimizer=trax.optimizers.Adam(learning_rate), n_steps_per_checkpoint= 400, #This will print the results at every 200 training steps. ) n_eval_b = math.floor(float(len(val_data)) / float(batch_size)) # Evaluaton task. eval_task = ts.training.EvalTask( labeled_data=inputs_test, metrics=[tl.CategoryCrossEntropy(), tl.CategoryAccuracy()], n_eval_batches=n_eval_b) training_loop = ts.training.Loop(recommender, train_task, eval_tasks=[eval_task], output_dir=out_folder) # Run 2000 steps (batches). training_loop.run(2000)