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 feature_vector[f + NUM_FEATURES] = action_negated feature_count_plain[f] += action_plain
# 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])) result_test = 100 * (res_test == labels_test).mean() result_train = 100 * (res_train == labels_train).mean() prf_test = precision_recall_fscore_support(res_test,
#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]])))) print("\nLet's try to get clauses....") NUM_FEATURES = len(X_train[0])
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) print('Num Features: ', NUM_FEATURES)
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]] prf_test_class=precision_recall_fscore_support(list(res_test), list(y_test), average=None)
test_size=0.084, shuffle=False) print(x_train.shape) print(x_test.shape) print(y_train.shape) print(y_test.shape) print('data fitting started..........................') tm = MultiClassTsetlinMachine(2000, 40, 27, weighted_clauses=False) print("\nAccuracy over 1000 epochs:\n") tempAcc = [] for i in range(500): start_training = time() tm.fit(x_train, y_train, epochs=1, incremental=True) stop_training = time() start_testing = time() result1 = 100 * (tm.predict(x_test) == y_test).mean() result2 = 100 * (tm.predict(x_train) == y_train).mean() stop_testing = time() tempAcc.append(result1) print( "#%d AccuracyTrain: %.2f%% AccuracyTest: %.2f%% Training: %.2fs Testing: %.2fs" % (i + 1, result2, result1, stop_training - start_training, stop_testing - start_testing)) finalAccuracy = max(tempAcc) print('Final Accuracy', finalAccuracy)
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) + ' ' else: this_clause += '-|F' + str(f - NUM_FEATURES) + ' '
#%%%%%%%%%%%%%%%%%% 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, stop_testing - start_testing)) #%%%%%%%%%%%%%%%%% To extrat the clause with its literal of particular class %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tm1.set_state(ta_state)
for r in range(RUNS): print('Run:', r) x_train, x_test, y_train, y_test = train_test_split(data, labels) x_test = x_train[:100].copy() ###### y_test = y_train[:100].copy() ###### 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') res = tm.predict(x_test) result[r] = 100 * (res == y_test).mean() X_test_transformed = tm.transform(x_test) print('here', len(x_test)) for testsample in range(100): print(testsample) print(res[testsample], y_test[testsample]) if res[testsample] != y_test[testsample]: sid = x_test_ids[testsample] fot.write(str(sid) + '\t') '''for sentence in sents: if sentence[0]==sid: fot.write(' '.join(sentence[1:])+'\n')''' fot.write(' '.join(sents[sid][1:]) + '\n') strTransformed = [str(k) for k in X_test_transformed[testsample]] fot.write(' '.join(strTransformed) + '\n')
EPOCHS = 1 #print('Epoch\tClass\tClause Number\tClause\tFeature\tAction\tType II fb cnt\n') for ep in range(EPOCHS): np.random.shuffle(data) training, test = data[:4000, :], data[4000:, :] X_train = training[:, 0:2] Y_train = training[:, -1] X_test = test[:, 0:2] Y_test = test[:, -1] tm.fit(X_train, Y_train, epochs=200) accur = 100 * (tm.predict(X_test) == Y_test).mean() #print("Accuracy:", 100*(tm.predict(X_test) == Y_test).mean()) all_clauses = tm.get_all_clauses(CLASSES, NUM_CLAUSES, NUM_FEATURES) print('all_clauses:') print('Class\tClause#\tClause\t') for cur_cls in CLASSES: for cur_clause in range(NUM_CLAUSES): print_str = str(cur_cls) + '\t' + str( cur_clause) + '\t' + all_clauses[cur_clause][int(cur_cls)] print(print_str) print(' ') '''for cur_clause in range(NUM_CLAUSES): for cur_cls in CLASSES: for f in range(NUM_FEATURES*2):