def fte_bte_experiment( train_x, train_y, test_x, test_y, ntrees, shift, slot, which_task, acorn=None, ): # We initialize lists to store the results df = pd.DataFrame() accuracies_across_tasks = [] # Declare the progressive learner model (L2F) learner = LifelongClassificationForest() for task_num in range((which_task - 1), 10): accuracy_per_task = [] # print("Starting Task {} For Shift {} For Slot {}".format(task_num, shift, slot)) if acorn is not None: np.random.seed(acorn) # If first task, add task. Else, add a transformer for the task if task_num == (which_task - 1): learner.add_task( X=train_x[(task_num * 900):((task_num + 1) * 900)], y=train_y[(task_num * 900):((task_num + 1) * 900)], task_id=0, ) t_num = 0 # Add transformers for all task up to current task (task t) while t_num < task_num: # Make a prediction on task t using the trained learner on test data llf_task = learner.predict( test_x[((which_task - 1) * 1000):(which_task * 1000), :], task_id=0, ) acc = np.mean(llf_task == test_y[((which_task - 1) * 1000):(which_task * 1000)]) accuracies_across_tasks.append(acc) learner.add_transformer( X=train_x[(t_num * 900):((t_num + 1) * 900)], y=train_y[(t_num * 900):((t_num + 1) * 900)], ) # Add transformer for next task t_num = t_num + 1 else: learner.add_transformer( X=train_x[(task_num * 900):((task_num + 1) * 900)], y=train_y[(task_num * 900):((task_num + 1) * 900)], ) # Make a prediction on task t using the trained learner on test data llf_task = learner.predict(test_x[((which_task - 1) * 1000):(which_task * 1000), :], task_id=0) acc = np.mean(llf_task == test_y[((which_task - 1) * 1000):(which_task * 1000)]) accuracies_across_tasks.append(acc) # print("Accuracy Across Tasks: {}".format(accuracies_across_tasks)) df["task"] = range(1, 11) df["task_accuracy"] = accuracies_across_tasks return df
def label_shuffle_experiment( train_x, train_y, test_x, test_y, ntrees, shift, slot, num_points_per_task, acorn=None, ): # We initialize lists to store the results df = pd.DataFrame() shifts = [] accuracies_across_tasks = [] # Declare the progressive learner model (L2F), with ntrees as a parameter learner = LifelongClassificationForest(n_estimators=ntrees) for task_ii in range(10): print("Starting Task {} For Fold {} For Slot {}".format( task_ii, shift, slot)) if acorn is not None: np.random.seed(acorn) # If task number is 0, add task. Else, add a transformer for the task if task_ii == 0: learner.add_task( X=train_x[task_ii * 5000 + slot * num_points_per_task:task_ii * 5000 + (slot + 1) * num_points_per_task], y=train_y[task_ii * 5000 + slot * num_points_per_task:task_ii * 5000 + (slot + 1) * num_points_per_task], task_id=0, ) else: learner.add_transformer( X=train_x[task_ii * 5000 + slot * num_points_per_task:task_ii * 5000 + (slot + 1) * num_points_per_task], y=train_y[task_ii * 5000 + slot * num_points_per_task:task_ii * 5000 + (slot + 1) * num_points_per_task], ) # Make a prediction on task 0 using the trained learner on test data llf_task = learner.predict(test_x[:1000], task_id=0) # Calculate the accuracy of the task 0 predictions acc = np.mean(llf_task == test_y[:1000]) accuracies_across_tasks.append(acc) shifts.append(shift) print("Accuracy Across Tasks: {}".format(accuracies_across_tasks)) df["data_fold"] = shifts df["task"] = range(1, 11) df["task_1_accuracy"] = accuracies_across_tasks return df
def LF_experiment(angle, data_x, data_y, granularity, max_depth, reps=1, ntrees=29, acorn=None): # Set random seed to acorn if acorn is specified if acorn is not None: np.random.seed(acorn) errors = np.zeros( 2 ) # initializes array of errors that will be generated during each rep for rep in range(reps): # training and testing subsets are randomly selected by calling the cross_val_data function train_x1, train_y1, train_x2, train_y2, test_x, test_y = cross_val_data( data_x, data_y, total_cls=10) # Change data angle for second task tmp_data = train_x2.copy() _tmp_ = np.zeros((32, 32, 3), dtype=int) total_data = tmp_data.shape[0] for i in range(total_data): tmp_ = image_aug(tmp_data[i], angle) # 2D image is flattened into a 1D array as random forests can only take in flattened images as inputs tmp_data[i] = tmp_ # .shape gives the dimensions of each numpy array # .reshape gives a new shape to the numpy array without changing its data train_x1 = train_x1.reshape(( train_x1.shape[0], train_x1.shape[1] * train_x1.shape[2] * train_x1.shape[3], )) tmp_data = tmp_data.reshape(( tmp_data.shape[0], tmp_data.shape[1] * tmp_data.shape[2] * tmp_data.shape[3], )) test_x = test_x.reshape( (test_x.shape[0], test_x.shape[1] * test_x.shape[2] * test_x.shape[3])) # number of trees (estimators) to use is passed as an argument because the default is 100 estimators progressive_learner = LifelongClassificationForest( n_estimators=ntrees, default_max_depth=max_depth) # Add the original task progressive_learner.add_task(X=train_x1, y=train_y1) # Predict and get errors for original task llf_single_task = progressive_learner.predict(test_x, task_id=0) # Add the new transformer progressive_learner.add_transformer(X=tmp_data, y=train_y2) # Predict and get errors with the new transformer llf_task1 = progressive_learner.predict(test_x, task_id=0) errors[1] = errors[1] + (1 - np.mean(llf_task1 == test_y) ) # errors from transfer learning errors[0] = errors[0] + (1 - np.mean(llf_single_task == test_y) ) # errors from original task errors = ( errors / reps ) # errors are averaged across all reps ==> more reps means more accurate errors # Average errors for original task and transfer learning are returned for the angle tested return errors