CLASSES = list(set(Y_train)) #list of classes #Load Testing Data test_data = np.loadtxt("NoisyXORTestData.txt") X_test = test_data[:, 0:-1] #last column is class labels Y_test = test_data[:, -1] #last column is class labels #Initialize the Tsetlin Machine tm = MultiClassTsetlinMachine(NUM_CLAUSES, THRESHOLD, S, boost_true_positive_feedback=0) #Fit TM on training data tm.fit(X_train, Y_train, epochs=1) #Predict on test data, compare to ground truth, calculate accuracy0 print("Accuracy:", 100 * (tm.predict(X_test) == Y_test).mean()) print('Type II feedbacks on clauses: ', tm.typeII_feedback_clauses) #Prediction on some random data print("Prediction: x1 = 1, x2 = 0, ... -> y = %d" % (tm.predict(np.array([[1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0]])))) print("Prediction: x1 = 0, x2 = 1, ... -> y = %d" % (tm.predict(np.array([[0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0]])))) print("Prediction: x1 = 0, x2 = 0, ... -> y = %d" % (tm.predict(np.array([[0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0]])))) print("Prediction: x1 = 1, x2 = 1, ... -> y = %d" % (tm.predict(np.array([[1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0]]))))
clauses = np.zeros((RUNS * NUM_CLAUSES, NUM_FEATURES * 2 + 1)) clause_dict = {} for r in range(RUNS): print('Run:', r) x_train, x_test, y_train, y_test = train_test_split(data, labels) x_train_ids = x_train[:, -1] x_test_ids = x_test[:, -1] x_train = x_train[:, :-1] x_test = x_test[:, :-1] #print('\nsplits ready:',x_train.shape, x_test.shape) tm = MultiClassTsetlinMachine(NUM_CLAUSES, T, s) tm.fit(x_train, y_train, epochs=TRAIN_EPOCHS, incremental=True) print('\nfit done') result[r] = 100 * (tm.predict(x_test) == y_test).mean() feature_vector = np.zeros(NUM_FEATURES * 2) for cur_cls in CLASSES: for cur_clause in range(NUM_CLAUSES): if cur_clause % 2 == 0: clause_type = 'positive' else: clause_type = 'negative' this_clause = '' for f in range(0, NUM_FEATURES): action_plain = tm.ta_action(int(cur_cls), cur_clause, f) action_negated = tm.ta_action(int(cur_cls), cur_clause, f + NUM_FEATURES) feature_vector[f] = action_plain
#np.save('x_train_conv', Xtrain) #np.save('x_test_conv', Xtest) # Setup tm = MultiClassTsetlinMachine(CLAUSES, T, s, weighted_clauses=weighting) labels_test_indx = np.where(labels_test == 1) labels_train_indx = np.where(labels_train == 1) acc = [] acc_train = [] # Training for i in range(RUNS): print(i) start_training = time() tm.fit(Xtrain, labels_train, epochs=50, incremental=True) stop_training = time() start_testing = time() res_test = tm.predict(Xtest) res_train = tm.predict(Xtrain) res_test_indx = np.where(res_test == 1) res_train_indx = np.where(res_train == 1) result_test2 = 100 * len( set(list(res_test_indx[0])).intersection(set(list( labels_test_indx[0])))) / len(list(labels_test_indx[0])) result_train2 = 100 * len( set(list(res_train_indx[0])).intersection( set(list(labels_train_indx[0])))) / len(list(labels_train_indx[0]))
''' CLAUSES=250 T=60 s=37 weighting = True training_epoch=5 RUNS=100 X_train, X_test, y_train, y_test = train_test_split(featureset_transformed_X, featureset_transformed_y, test_size=0.30, random_state=42, shuffle=True) tm = MultiClassTsetlinMachine(CLAUSES, T, s, weighted_clauses=weighting,append_negated=False) allacc=[] for i in range(RUNS): tm.fit(X_train, y_train, epochs=training_epoch, incremental=True) res_test=tm.predict(X_test) print(res_test) acc_test = 100*(res_test == y_test).mean() allacc.append(acc_test) #print(type(y_test), type(res_test), len(y_test), len(res_test)) prf_test_macro=precision_recall_fscore_support(list(res_test), list(y_test), average='macro') prf_test_macro=[str(round(p,2)) for p in prf_test_macro[:-1]] prf_test_micro=precision_recall_fscore_support(list(res_test), list(y_test), average='micro') prf_test_micro=[str(round(p,2)) for p in prf_test_micro[:-1]]
print(word_idx) print(sents[10], data[10]) x_train, x_test, y_train, y_test = train_test_split(data, labels) x_train_ids = x_train[:, -1] x_test_ids = x_test[:, -1] x_train = x_train[:, :-1] x_test = x_test[:, :-1] NUM_CLAUSES = 20 T = 15 s = 3.9 print('\nsplits ready:', x_train.shape, x_test.shape) tm = MultiClassTsetlinMachine(NUM_CLAUSES, T, s) tm.fit(x_train, y_train, epochs=200, incremental=True) print('\nfit done') result = 100 * (tm.predict(x_test) == y_test).mean() print(result) res = tm.predict(x_test) for i in range(len(x_test_ids)): sidx = x_test_ids[i] print(sents[sidx], res[i]) NUM_CLAUSES = 10 NUM_FEATURES = len(x_train[0]) CLASSES = list(set(y_train)) print('Num Clauses:', NUM_CLAUSES) print('Num Classes: ', len(CLASSES), ' : ', CLASSES)
X_test = test[:, 0:2] Y_test = test[:, -1] CLASSES = list(set(Y_train)) #list of classes NUM_FEATURES = len(X_train[0]) #number of features print('Num Clauses:', NUM_CLAUSES) print('Num Classes: ', len(CLASSES), ' : ', CLASSES) print('Num Features: ', NUM_FEATURES) tm = MultiClassTsetlinMachine(NUM_CLAUSES, THRESHOLD, S, boost_true_positive_feedback=0) tm.fit(X_train, Y_train, epochs=200) print("Accuracy:", 100 * (tm.predict(X_test) == Y_test)) all_clauses = [[] for i in range(NUM_CLAUSES)] for cur_cls in CLASSES: for cur_clause in range(NUM_CLAUSES): this_clause = '' for f in range(NUM_FEATURES * 2): action = tm.ta_action(int(cur_cls), cur_clause, f) if action == 1: if this_clause != '': this_clause += 'AND ' if f < NUM_FEATURES: this_clause += 'F' + str(f) + ' '
X_test = X_final4[3608:, :] #For Laptop dataset change this from 3608 to 2328 ytrain = y[0:3608] #For Laptop dataset change this from 3608 to 2328 ytest = y[3608:] #For Laptop dataset change this from 3608 to 2328 #%%%%%%%%%%%%%%%%%% Initialize Tsetlin Machine %%%%%%%%%%%%%%%%%%%%%%% tm1 = MultiClassTsetlinMachine( 700, 90 * 100, 15, weighted_clauses=True) #number of clause= 700, T = 90*100 and s = 15 #tm1.fit(X_train, ytrain, epochs=0) print("\nTraining Classification Layer...") print("\nAccuracy over 1000 epochs:\n") max = 0 for i in range(500): start_training = time() tm1.fit(X_train, ytrain, epochs=1, incremental=True) stop_training = time() start_testing = time() result2 = 100 * (tm1.predict(X_train) == ytrain).mean() result1 = 100 * (tm1.predict(X_test) == ytest).mean() y_pred = tm1.predict(X_test) f1 = metrics.f1_score(ytest, y_pred, average='macro') if result1 > max: max = result1 pred = tm1.predict(X_test) ta_state = tm1.get_state() stop_testing = time() print( "#%d AccuracyTrain: %.2f%% AccuracyTest: %.2f%% F1-Score: %.2f%% Training: %.2fs Testing: %.2fs" % (i + 1, result2, result1, f1 * 100, stop_training - start_training,