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 = 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()
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)
model.forward(torch.from_numpy(X), test=True) return model.probs.detach().numpy() def pt_deep_decfun(model): return lambda X: eval(model, X)[np.arange(len(X)), 1] if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) config = [2, 10, 10, 2] # instanciraj podatke X i labele Yoh_ X, Y_ = data.sample_gmm_2d(6, 2, 10) Yoh_ = F.one_hot(torch.from_numpy(Y_), config[-1]) # definiraj model: ptdeep = PTDeep(config, torch.relu) # nauči parametre (X i Yoh_ moraju biti tipa torch.Tensor): train(ptdeep, torch.from_numpy(X), Yoh_, 10_000, 0.1) # dohvati vjerojatnosti na skupu za učenje probs = eval(ptdeep, X) Y = np.argmax(probs, axis=1) # ispiši performansu (preciznost i odziv po razredima) accuracy, pr, _ = data.eval_perf_multi(Y, Y_) print(f'accuracy: {accuracy}, precision: {pr[0]}, recall: {pr[1]}')
gh1 = np.dot(gs2, w_2.T) gs1 = gh1 * (h_1 > 0) grad_w1 = np.dot(X.T, gs1) grad_b1 = np.sum(gs1, axis=0) w_1 += -delta * grad_w1 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)
feed_dict={ self.X: X, self.Y_: Y_ }) if i % 10 == 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, 10) y_ = OneHotEncoder().fit_transform(y).toarray() deep = TFDeep([2, 3]) deep.train(X, y_, 10000) y = y.flatten() probs = deep.eval(X) Y = np.argmax(probs[0], axis=1) print eval_perf_binary(Y, y) bbox = (np.min(X, axis=0), np.max(X, axis=0)) graph_surface(lambda x: np.argmax(deep.eval(x)[0], axis=1),
def predict(self, X): return self.model.predict(X) def get_scores(self, X): return self.model.predict_proba(X) def support(self): return self.model.support_ if __name__ == '__main__': import numpy as np import data import matplotlib.pyplot as plt np.random.seed(100) C = 2 n = 10 X, Y_, Yoh_ = data.sample_gmm_2d(6, 2, 20, one_hot=True) model = SVMWrapper(X, Y_, c=1, g='auto') decfun = lambda x: model.get_scores(x)[:, 1] probs = model.get_scores(X) Y = probs.argmax(axis=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, model.support()) plt.show()
import numpy as np import matplotlib.pyplot as plt from data import sample_gmm_2d, graph_surface, graph_data, myDummyDecision, eval_perf_multi from fcann2 import fcann2_train, fcann2_classify 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=[])
func=torch.relu, param_niter=1e5, param_delta=0.001) # Example of dimensions ptdeep1, result = test(X, Y, dims=[2, 5, C], func=torch.relu, param_niter=1, param_delta=0.001, plot=False) ptdeep1.count_params() # Extra tasks. X1, Y1 = data.sample_gmm_2d(4, 2, 40) X2, Y2 = data.sample_gmm_2d(6, 2, 10) m11, r11 = test(X1, Y1, dims=[2, 2], func=torch.relu) m12, r12 = test(X1, Y1, dims=[2, 10, 2], func=torch.relu) m13, r13 = test(X1, Y1, dims=[2, 10, 10, 2], func=torch.relu) n21, r21 = test(X2, Y2, dims=[2, 2], func=torch.relu) n22, r22 = test(X2, Y2, dims=[2, 10, 2], func=torch.relu) n23, r23 = test(X2, Y2, dims=[2, 10, 10, 2], func=torch.relu) apr_print(data.eval_perf_binary(np.argmax(r11, axis=1), Y1)) apr_print(data.eval_perf_binary(np.argmax(r12, axis=1), Y1)) apr_print(data.eval_perf_binary(np.argmax(r13, axis=1), Y1)) apr_print(data.eval_perf_binary(np.argmax(r21, axis=1), Y2))
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)
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()
for v in tf.trainable_variables(): print(v.name) tmp = 1 for i in v.shape: tmp *= i params_count += tmp print("Params count = ", params_count) if __name__ == "__main__": # initialize the random number generator np.random.seed(100) tf.set_random_seed(100) # instantiate the data X and the labels Yoh_ X, Y_ = data.sample_gmm_2d(5, 3, 40) Yoh_ = data.class_to_one_hot(Y_) # build the graph: tfdeep = TFDeep([2, 10, 3], param_delta=0.01) tfdeep.count_params() # perform the training with given hyper-parameters: tfdeep.train(X, Yoh_, 100000) # predict probabilities of the data points probs = tfdeep.eval(X) Y = probs.argmax(axis=1) # print performance (per-class precision and recall) accuracy = data.eval_perf_multi(Y, Y_)
""" return self.sess.run(self.yp, feed_dict={self.X: X}) def predict(self, X): return np.argmax(self.eval(X), axis=1) if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) tf.set_random_seed(100) # instanciraj podatke X i labele Yoh D = 2 C = 2 X, Y = data.sample_gmm_2d(5, C, 10) oh = OneHotEncoder(sparse=False) oh.fit(Y) Yoh = oh.transform(Y) Yoh.shape X.shape model = LogisticRegression(C=1e5) model.fit(X, Y) data.graph_data_pred(X, Y, model) plt.savefig("lab1/logreg.png") for ll in np.logspace(-6, 4, num=50):