def test(X, Y, dims, func, param_niter=1e5, param_delta=0.001, plot=True): ptdeep = pt_deep.PTDeep(dims, func) # nauči parametre (X i Yoh_ moraju biti tipa torch.Tensor): pt_deep.train(ptdeep, X, Y, param_niter=param_niter, param_delta=param_delta) # dohvati vjerojatnosti na skupu za učenje probs = pt_deep.evaluation(ptdeep, X) # ispiši performansu (preciznost i odziv po razredima) if plot: rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(deep_decfun(ptdeep), rect, offset=0.5) data.graph_data(X, Y, np.argmax(pt_deep.evaluation(ptdeep, X), axis=1), special=[]) plt.show() return ptdeep, probs
def __test_tp1_task2(): np.random.seed(50) X, Y_ = data.sample_gmm(6, 2, 10) W1, b1, W2, b2 = fcann2_train(X, Y_, param_hidden_len=100) decfunc = fcann2_classify_new(W1, b1, W2, b2) Y = np.argmax(fcann2_classify(X, W1, b1, W2, b2), axis=1) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfunc, bbox, offset=0.5) data.graph_data(X, Y_, Y) plt.show()
def __test(): np.random.seed(100) X, Y_ = data.sample_gmm(6, 2, 10) svm = KSVMWrap(X, Y_) Y = svm.predict(X) accuracy, recall, precision = data.eval_perf_binary(Y, Y_) print("Accuracy : ", accuracy) print("Precision : ", precision) print("Recall : ", recall) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(svm.get_scores, bbox, offset=0) data.graph_data(X, Y_, Y, special=svm.support()) plt.show()
def binlogreg_train(X, Y_, param_niter=500, param_delta=0.2, animate=False): b = 0 w = np.random.randn(2) N = len(Y_) files = [] for i in range(param_niter): scores = np.dot(X, w) + b # result classification N x 1 probabilities = 1. / (1 + np.exp(-scores)) # class probabilities c_1 # N x 1 loss = np.sum(-np.log(probabilities)) # scalar if i % 3 == 0: print("iteration {}: loss {}".format(i, loss)) if animate: # Graph Y = np.where(probabilities >= .5, 1, 0) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(data.binlogreg_decfun(X, w, b), bbox, offset=0) data.graph_data(X, Y_, Y) # Animate file_name = '_tmp%03d.png' % i print('Saving frame', file_name) plt.savefig(file_name) files.append(file_name) dL_dscores = probabilities - Y_ # loss derivation on result classification N x 1 grad_w = 1./N * np.dot(dL_dscores, X) # D x 1 grad_b = 1./N * np.sum(dL_dscores) # 1 x 1 w += -param_delta * grad_w b += -param_delta * grad_b if animate: print('Making movie animation.mpg - this make take a while') os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg") os.system("convert _tmp*.png animation.mng") # cleanup for file_name in files: os.remove(file_name) print('Weights:', w) return w, b
def main(): np.random.seed(100) tf.set_random_seed(100) # Init the dataset X, Y_, Yoh_ = data.sample_gmm_2d(K=6, C=3, N=20) # Construct the computing graph tflr = TFLogreg(X.shape[1], Yoh_.shape[1], param_delta=0.5) tflr.train(X, Yoh_, 10000) tflr.eval(X, Yoh_) # Plot the results bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(tflogreg_classify(X, tflr), bbox, offset=0) data.graph_data(X, Y_, tflr.predict(X)) # show the results #plt.savefig('tf_logreg_classification.png') plt.show()
def main(): # create the dataset X, Y_, Yoh_ = data.sample_gmm_2d(K=6, C=2, N=10) model = fcann2_train(X, Y_) # fit the model probabilities = fcann2_classify(X, model) Y = np.argmax(probabilities, axis=1) # evaluate the model accuracy, recall, precision = data.eval_perf_binary(Y, Y_) print('Acc: {0}\nRecall: {1}\nPrecision: {2}\n'.format( accuracy, recall, precision)) # graph the data points bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(fcann2_decfun(X, model), bbox, offset=0) data.graph_data(X, Y_, Y) # show the resultsfcann2_train #plt.savefig('fcann2_classification.png') plt.show()
def main(): np.random.seed(100) # Init the dataset class_num = 3 X, Y_, Yoh_ = data.sample_gmm_2d(K=6, C=class_num, N=40) # Train the model svm = KSVMWrap(C=1.0, X=X, Y_=Y_, kernel='rbf') # Plot the results bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(svm_classify(X, svm), bbox, offset=0) data.graph_data(X, Y_, svm.predict(X), svm.get_scores()) # show the results #plt.savefig('svm.png') plt.show() accuracy, recall, precision = data.eval_perf_binary(svm.predict(X), Y_) print('Acc: {0}\nRecall: {1}\nPrecision: {2}\n'.format(accuracy, recall, precision)) svm.get_scores()
probs = expscores / sumexp # N x C return probs def fcann2_decfun(W1, b1, W2, b2): return lambda X: fcann2_classify(X, W1, b1, W2, b2)[np.arange(len(X)), 1] if __name__ == "__main__": np.random.seed(100) # get data X, Y_ = data.sample_gmm_2d(6, 2, 10) # train the model W1, b1, W2, b2 = fcann2_train(X, Y_) # get the class predictions probs = fcann2_classify(X, W1, b1, W2, b2) Y = np.argmax(probs, axis=1) # # graph the decision surface decfun = fcann2_decfun(W1, b1, W2, b2) rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfun, rect, offset=0) # # graph the data points data.graph_data(X, Y_, Y, special=[]) plt.show()
print(i, loss) def eval(self, X): return self.session.run([self.probs], feed_dict={self.X: X})[0] def count_params(self): for v in tf.trainable_variables(): print(v.name) total_count = 0 for i in range(1, len(self.h)): total_count += self.h[i] * self.h[i - 1] total_count += sum(self.h[1:]) print("Total parameter count: " + str(total_count)) if __name__ == '__main__': (X, Y_) = data.sample_gmm_2d(6, 2, 10) N, D = X.shape C = 2 Yoh_ = np.zeros((N, C)) Yoh_[range(N), Y_.astype(int)] = 1 model = TFDeep([2, 3, 2]) model.train(X, Yoh_, 1000) probs = model.eval(X) model.count_params() Y = np.argmax(probs, axis=1) print(data.eval_perf_binary(Y, np.argmax(Yoh_, axis=1))) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(model.eval, bbox, offset=0.5) data.graph_data(X, Y_, Y)
w_2 += -delta * grad_w2 b_1 += -delta * grad_b1 b_2 += -delta * grad_b2 return w_1, w_2, b_1, b_2 if __name__ == "__main__": X, y = sample_gmm_2d(6, 4, 30) C = len(np.lib.arraysetops.unique(y)) #X = np.array([[1, 2], [2, 3], [4, 5]]) #y = np.array([0, 1, 1])[np.newaxis] y_ = OneHotEncoder().fit_transform(y).toarray() w_1, w_2, b_1, b_2 = fcann_train(X, y_, C) probs, _ = forward(X, w_1, w_2, b_1, b_2) Y = np.argmax(probs, axis=1) y = y.flatten() print eval_perf_binary(Y, y) bbox = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface( lambda x: np.argmax(forward(x, w_1, w_2, b_1, b_2)[0], axis=1), bbox, offset=0.5) graph_data(X, y, Y)
def fcann2_decfun(w, b): def classify(X): return np.argmax(fcann2_classify(X, w, b), axis=1) return classify if __name__ == "__main__": X, Y_ = sample_gmm_2d(6, 2, 10) meanX = X.mean() stdX = X.std() X = (X - meanX) / stdX w, b = fcann2_train(X, Y_, param_lambda=1e-5, param_delta=1e-5) #param_niter # graph the decision surface rect = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface(fcann2_decfun(w, b), rect, offset=0.5) #print(fcann2_classify(X, w, b)) # graph the data points graph_data(X, Y_, np.argmax(fcann2_classify(X, w, b), axis=1), special=[]) # graph_data(X, Y_, list(map(lambda x: np.argmax(x), fcann2_classify(X, w, b))), special=[]) plt.show() # Finish
from sklearn import svm import numpy as np import data class KSVMWrap: def __init__(self, X, Y_, param_svm_c=1, param_svm_gamma='auto'): self.clf = svm.SVC(C=param_svm_c, gamma=param_svm_gamma) self.clf.fit(X, Y_) def predict(self, X): return self.clf.predict(X) def get_scores(self, X): return self.clf.decision_function(X) def support(self): return self.clf.support_ if __name__ == "__main__": np.random.seed(100) (X, Y_) = data.sample_gmm_2d(6, 2, 10) ksvm = KSVMWrap(X, Y_) Y = ksvm.predict(X) acc, prec, rec, avg_prec = data.eval_perf_binary(Y, Y_) print("Accuracy: {}\nPrecision: {}\nRecall: {}\nAverage Precision: {}". format(acc, prec, rec, avg_prec)) data.graph_surface(ksvm.get_scores, (np.min(X, axis=0), np.max(X, axis=0))) data.graph_data(X, Y_, Y, special=ksvm.support())
# instanciraj podatke X i labele Yoh_ X, Y_ = data.sample_gauss(3, 100) Yoh_ = data.class_to_onehot(Y_) # izgradi graf: tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.004) # nauči parametre: tflr.train(X, Yoh_, 20000) # dohvati vjerojatnosti na skupu za učenje probs = tflr.eval(X) Y = [np.argmax(ps) for ps in probs] # ispiši performansu (preciznost i odziv po razredima) accuracy, recall, precision = data.eval_perf_multi(Y_, np.array(Y)) print('acc:', accuracy, '\n rec', recall) plt.show() # iscrtaj rezultate, decizijsku plohu bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(logreg_classify_function(tflr), bbox, offset=0.5, width=256, height=256) # graph the data points data.graph_data(X, Y_, Y) # show the plot plt.show()
np.random.seed(100) # get the training dataset X, Y_ = data.sample_gmm(6, 2, 10) # train the model W1, b1, W2, b2 = fcann2_train(X, Y_, 5) # evaluate the model on the training dataset probs = fcann2_classify(X, W1, b1, W2, b2) Y = [np.argmax(ps) for ps in probs] # report performance accuracy, recall, precision = data.eval_perf_multi(Y, Y_) print(accuracy, recall, precision) plt.show() # graph the decision surface bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(fcann2_classify_function(W1, b1, W2, b2), bbox, offset=0.5, width=256, height=256) # graph the data points data.graph_data(X, Y_, Y) # show the plot plt.show()
from data import graph_data, graph_surface, sample_gmm_2d, eval_perf_binary class KSVMWrapper(object): def __init__(self, X, Y_, c=1, g='auto'): self.clf = SVC(C=c, gamma=g) self.clf.fit(X, Y_) def predict(self, X): return self.clf.predict(X) def get_scores(self, X): return self.clf.decision_function(X) @property def support(self): return self.clf.support_ if __name__ == '__main__': X, y = sample_gmm_2d(6, 2, 10) ksvmw = KSVMWrapper(X, y) y_ = ksvmw.predict(X) y = y.flatten() print eval_perf_binary(y_, y) bbox = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface(lambda x: ksvmw.get_scores(x), bbox, offset=0) graph_data(X, y_, y, special=ksvmw.support)
import numpy as np import data import matplotlib.pyplot as plt if __name__ == "__main__": np.random.seed(100) # get the training dataset X, Y_ = data.sample_gauss_2d(2, 100) # get the class predictions Y = data.myDummyDecision(X) > 0.5 # graph the data points bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(data.myDummyDecision, bbox, offset=0.4) data.graph_data(X, Y_, Y) # show the results plt.show()
self.session.run(tf.initialize_all_variables()) def train(self, X, Y_, param_niter): for i in range(param_niter): _, val_loss = self.session.run([self.train_step, self.loss], feed_dict={self.X: X, self.Y_: Y_}) if i % 1000 == 0: print i, val_loss def eval(self, X): P = self.session.run([self.probs], feed_dict={self.X: X}) return P if __name__ == '__main__': X, y = sample_gmm_2d(6, 3, 50) C = len(np.lib.arraysetops.unique(y)) y_ = OneHotEncoder().fit_transform(y).toarray() logreg = TFLogreg(2, C) logreg.train(X, y_, 10000) probs = logreg.eval(X) Y = np.argmax(probs[0], axis=1) y = y.flatten() print eval_perf_binary(Y, y) bbox = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface(lambda x: np.argmax(logreg.eval(x)[0], axis=1), bbox, offset=0.5) graph_data(X, Y, y)
return W, b if __name__ == "__main__": np.random.seed(100) # get the training dataset X, Y_ = data.sample_gauss(3, 100) # train the model W, b = logreg_train(X, data.class_to_onehot(Y_)) # evaluate the model on the training dataset probs = logreg_classify(X, W, b) Y = np.argmax(probs, axis=1) # report performance accuracy, recall, precision = data.eval_perf_multi(Y, Y_) AP = data.eval_AP(Y_) print(accuracy, recall, precision, AP) # graph the decision surface rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(lambda x: np.argmax(logreg_classify(x, W, b), axis=1), rect, offset=0.5) # graph the data points data.graph_data(X, Y_, Y, special=[]) plt.show()
# poboljšani parametri w += -param_delta * grad_w b += -param_delta * grad_b return w, b if __name__ == "__main__": np.random.seed(100) # get the training dataset X, Y_ = data.sample_gauss(2, 100) # train the model w, b = binlogreg_train(X, data.class_to_onehot(Y_)) # evaluate the model on the training dataset probs = binlogreg_classify(X, w, b) Y = probs > 0.5 # report performance accuracy, recall, precision = data.eval_perf_binary(Y[:, -1], Y_) AP = data.eval_AP(Y_) print(accuracy, recall, precision, AP) # graph the decision surface rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(lambda x: binlogreg_classify(x, w, b), rect, offset=0.5) # graph the data points data.graph_data(X, Y_, Y[:, -1], special=[]) plt.show()
self.clf = self.clf.fit(X, Y_) self.support = self.clf.support_ #Indeksi podataka koji su odabrani za potporne vektore self.Y_ = Y_ def predict(self, X): return self.clf.predict(X) def get_scores(self, X): return classification_report(self.Y_, self.predict(X)) if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) #X,Y_ = data.sample_gmm(4, 2, 40) X, Y_ = data.sample_gmm(6, 2, 10) Yoh_ = data.class_to_onehot(Y_) svm2 = KSVMWrap(X, Y_) print(svm2.get_scores(X)) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(svm2.predict, bbox, offset=0.5, width=256, height=256) data.graph_data(X, Y_, svm2.predict(X), special=svm2.support) plt.show()
if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) # instanciraj podatke X i labele Yoh_ X, Y = sample_gauss_2d(2, 10) #Yoh_ = class_to_onehot(Y) #X = torch.tensor(X) #Yoh_ = torch.tensor(Yoh_) # definiraj model: ptlr = PTLogreg(X.shape[1], max(Y) + 1) # nauči parametre (X i Yoh_ moraju biti tipa torch.Tensor): train(ptlr, X, Y, param_niter=1e5, param_delta=0.001) # dohvati vjerojatnosti na skupu za učenje probs = evaluation(ptlr, X) # ispiši performansu (preciznost i odziv po razredima) rect = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface(logreg_decfun(ptlr), rect, offset=0.5) graph_data(X, Y, np.argmax(evaluation(ptlr, X), axis=1), special=[]) plt.show() # iscrtaj rezultate, decizijsku plohu
if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) tf.set_random_seed(100) # instanciraj podatke X i labele Yoh_ X, Y_ = data.sample_gmm_2d(6, 2, 10) Yoh_ = data.class_to_onehot(Y_) # izgradi graf: tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.06, 1) # nauči parametre: tflr.train(X, Yoh_, 1000) # dohvati vjerojatnosti na skupu za učenje probs = tflr.eval(X) Y = np.argmax(probs, axis=1) # ispiši performansu (preciznost i odziv po razredima) accuracy, recall, precision = data.eval_perf_multi(Y, Y_) AP = data.eval_AP(Y_) print(accuracy, recall, precision, AP) # iscrtaj rezultate, decizijsku plohu rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(calc_class, rect, offset=0.5) data.graph_data(X, Y_, Y, special=[]) plt.show()
def logreg_classify(X, W, b): scores = X.dot(W.T) + b exp_scores = np.exp(scores) sumexp = exp_scores.sum(axis=1) return exp_scores / sumexp.reshape(-1,1) def logreg_decfun(W, b): return lambda X: logreg_classify(X, W, b).argmax(axis=1) if __name__ == '__main__': np.random.seed(100) X, Y_ = data.sample_gauss_2d(4, 100) W, b = logreg_train(X, Y_) probs = logreg_classify(X, W, b) Y = np.argmax(probs, axis=1) #data.eval_perf_multi(Y, Y_) rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(logreg_decfun(W, b), rect) data.graph_data(X, Y_, Y) plt.show()
def fcann2_classify(self, X): S1 = np.dot(X, self.W1.T) + self.b1 H1 = np.maximum(S1, 0) S2 = np.dot(H1, self.W2.T) + self.b2 exp_scores = np.exp(S2) sum_exp = exp_scores.sum(axis=1) probs = exp_scores / sum_exp.reshape(-1,1) return probs if __name__ == '__main__': np.random.seed(100) X, Y_ = data.sample_gmm_2d(K, C, N) clf = fcann2(H=5) clf.fcann2_train(X, Y_) dec_fun = lambda X: clf.fcann2_classify(X)[:,1] probs = dec_fun(X) Y = probs > 0.5 rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(dec_fun, rect, offset=0.5) data.graph_data(X, Y_, Y) plt.show()
- X: actual datapoints [NxD] Returns: predicted class probabilites [NxC] """ # koristiti: tf.Session.run probs = self.session.run(self.probs, {self.X: X}) return probs if __name__ == '__main__': np.random.seed(100) tf.set_random_seed(100) X, Y_, Yoh_ = data.sample_gauss_2d(3, 100, one_hot=True) _, D = X.shape _, C = Yoh_.shape tflr = TFLogreg(D, C, 0.1, 0.25) tflr.train(X, Yoh_, 1000) probs = tflr.eval(X) Y = probs.argmax(axis=1) dec_fun = lambda X: tflr.eval(X).argmax(axis=1) rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(dec_fun, rect) data.graph_data(X, Y_, Y) plt.show()
probs = self.session.run(self.probs, {self.X: X}) return probs if __name__ == '__main__': import numpy as np import data import matplotlib.pyplot as plt tf.reset_default_graph() tf.set_random_seed(130) np.random.seed(42) X, Y_, Yoh_ = data.sample_gmm_2d(4, 2, 10, one_hot=True) # izgradi graf: config = [X.shape[1], 10, 10, Yoh_.shape[1]] nn = TFDeep(config, 0.05, 1e-4, tf.nn.sigmoid ) nn.train(X, Yoh_, 4000) probs = nn.eval(X) Y = probs.argmax(axis=1) decfun = lambda x: nn.eval(x)[:,1] bbox=(np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfun, bbox, offset=0.5) data.graph_data(X, Y_, Y) plt.show()
# instanciraj podatke X i labele Yoh_ #X,Y_ = data.sample_gmm(4, 2, 40) X,Y_ = data.sample_gmm(6, 2, 10) Yoh_ = data.class_to_onehot(Y_) # izgradi graf: tfdeep = TFDeep([2,10, 10, 2], tf.nn.relu, 0.0005, 0.04) # nauči parametre: tfdeep.train(X, Yoh_, 10000) tfdeep.count_params() # dohvati vjerojatnosti na skupu za učenje probs = tfdeep.eval(X) Y = [np.argmax(ps) for ps in probs] # ispiši performansu (preciznost i odziv po razredima) accuracy, recall, precision = data.eval_perf_multi(Y_, np.array(Y)) print('acc:', accuracy, '\n rec', recall) plt.show() # iscrtaj rezultate, decizijsku plohu bbox=(np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(deep_classify_function(tfdeep), bbox, offset=0.5, width=256, height=256) # graph the data points data.graph_data(X, Y_, Y) # show the plot plt.show() tf.reset_default_graph()