def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # dnn = DNN([1000, 750, 500]) # dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3) # vs dnn = DNN([1000, 750, 500]) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=False, epochs=10)
def main(loadfile=None, savefile=None): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() if loadfile: dbn = DBN.load(loadfile) else: dbn = DBN([1000, 750, 500, 10]) # AutoEncoder is default # dbn = DBN([1000, 750, 500, 10], UnsupervisedModel=RBM) dbn.fit(Xtrain, pretrain_epochs=2) if savefile: dbn.save(savefile) # first layer features # initial weight is D x M W = dbn.hidden_layers[0].W.eval() for i in range(dbn.hidden_layers[0].M): imgplot = plt.imshow(W[:, i].reshape(28, 28), cmap='gray') plt.show() should_quit = input("Show more? Enter 'n' to quit\n") if should_quit == 'n': break # features learned in the last layer for k in range(dbn.hidden_layers[-1].M): # activate the kth node X = dbn.fit_to_input(k) imgplot = plt.imshow(X.reshape(28, 28), cmap='gray') plt.show() if k < dbn.hidden_layers[-1].M - 1: should_quit = input("Show more? Enter 'n' to quit\n") if should_quit == 'n': break
def test_single_autoencoder(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() autoencoder = AutoEncoder(300, 0) autoencoder.fit(Xtrain, epochs=2, show_fig=True) done = False while not done: i = np.random.choice(len(Xtest)) x = Xtest[i] y = autoencoder.predict([x]) plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original') plt.subplot(1, 2, 2) plt.imshow(y.reshape(28, 28), cmap='gray') plt.title('Reconstructed') plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def main(loadfile=None, savefile=None): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() if loadfile: dbn = DBN.load(loadfile) else: dbn = DBN([1000, 750, 500, 10]) # AutoEncoder is default dbn = DBN([1000, 750, 500, 10], UnsupervisedModel=RBM) dbn.fit(Xtrain, pretrain_epochs=15) if savefile: dbn.save(savefile) # first layer features # initial weight is D x M # W = dbn.hidden_layers[0].W.eval() # for i in xrange(dbn.hidden_layers[0].M): # imgplot = plt.imshow(W[:,i].reshape(28, 28), cmap='gray') # plt.show() # should_quit = raw_input("Show more? Enter 'n' to quit\n") # if should_quit == 'n': # break # features learned in the last layer for k in xrange(dbn.hidden_layers[-1].M): # activate the kth node X = dbn.fit_to_input(k) imgplot = plt.imshow(X.reshape(28, 28), cmap='gray') plt.show() if k < dbn.hidden_layers[-1].M - 1: should_quit = raw_input("Show more? Enter 'n' to quit\n") if should_quit == 'n': break
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dbn = DBN([1000, 750, 500], UnsupervisedModel=AutoEncoder) # dbn = DBN([1000, 750, 500, 10]) output = dbn.fit(Xtrain, pretrain_epochs=2) print "output.shape", output.shape # sample before using t-SNE because it requires lots of RAM sample_size = 600 tsne = TSNE() reduced = tsne.fit_transform(output[:sample_size]) plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain[:sample_size], alpha=0.5) plt.title("t-SNE visualization") plt.show() # t-SNE on raw data reduced = tsne.fit_transform(Xtrain[:sample_size]) plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain[:sample_size], alpha=0.5) plt.title("t-SNE visualization") plt.show() pca = PCA() reduced = pca.fit_transform(output) plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain, alpha=0.5) plt.title("PCA visualization") plt.show()
def test_single_autoencoder(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape autoencoder = AutoEncoder(D, 300, 0) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) autoencoder.set_session(session) autoencoder.fit(Xtrain, show_fig=True) done = False while not done: i = np.random.choice(len(Xtest)) x = Xtest[i] y = autoencoder.predict([x]) plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original') plt.subplot(1, 2, 2) plt.imshow(y.reshape(28, 28), cmap='gray') plt.title('Reconstructed') plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def singleAE(): X, Y, _, _ = getKaggleMNIST() N, D = X.shape #print("D ", D) AE = Autoencoder(D, 20) AE.fit(X, plot_fig=True) done = False while not done: i = np.random.choice(len(X)) x = X[i] # print(x) im = AE.predict([x]).reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title("Original") plt.subplot(1, 2, 2) plt.imshow(im, cmap='gray') plt.title("Reconstruction") plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def test_single_autoencoder(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() autoencoder = AutoEncoder(300, 0) autoencoder.fit(Xtrain, learning_rate=0.5, epochs=3, show_fig=True) # 假如這裡epoch=0 就只會initialize weights 但不做training done = False while not done: i = np.random.choice(len(Xtest)) x = Xtest[ i] # 這裡取出來的x.shape = (784,), 是一個vector,但 input of predict() need to be a matrix (1, 784) y = autoencoder.predict( [x]) # 這裡為什麼要寫 [x]? 這樣才可以把shape轉成 (1, 784) 作為input plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original') plt.subplot(1, 2, 2) plt.imshow(y.reshape(28, 28), cmap='gray') plt.title('Reconstructed') plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): # 第一次看到的寫法 in ('n' or 'N') , 還不太懂 done = True
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5) plt.show()
def main(): Xtrain, Ytrain, _, _ = getKaggleMNIST() bay = BayesClassifier() bay.fit(Xtrain, Ytrain) # = bay.sample() for k in range(bay.K): sample = bay.sample_given_y(k).reshape(28, 28) mean = bay.gaussians[k]['m'].reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(sample, cmap='gray') plt.title("sample") plt.subplot(1, 2, 2) plt.imshow(mean, cmap='gray') plt.title("mean") plt.show() # generate rdm sample sample = bay.sample().reshape(28, 28) plt.imshow(sample, cmap='gray') plt.title("rdm sample") plt.show()
def main(): xtr, ytr, xts, yts = getKaggleMNIST() PCA_ = PCA() reduced = PCA_.fit_transform(xtr) print(xtr[:]) print(xtr.shape) plt.figure() plt.scatter(xtr[:,0], xtr[:,1], s=70, alpha=0.7) plt.title("Image") plt.figure() plt.scatter(reduced[:,0],reduced[:,1],s=70,c=ytr,alpha=0.5) plt.title("Image transformed") plt.show() plt.plot(PCA_.explained_variance_ratio_) # i.e. eigenvalues plt.title("Variance/Eigenvalues") plt.show() cumulative_variance = [] last = 0 for v in PCA_.explained_variance_ratio_: cumulative_variance.append(last+v) last = cumulative_variance[-1] plt.figure() plt.plot(cumulative_variance) plt.title("Cumulative variance") plt.show()
def test_single_autoencoder(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape autoencoder = AutoEncoder(D, 300, 0) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) autoencoder.set_session(session) autoencoder.fit(Xtrain, show_fig=True) done = False while not done: i = np.random.choice(len(Xtest)) x = Xtest[i] y = autoencoder.predict([x]) plt.subplot(1,2,1) plt.imshow(x.reshape(28,28), cmap='gray') plt.title('Original') plt.subplot(1,2,2) plt.imshow(y.reshape(28,28), cmap='gray') plt.title('Reconstructed') plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) start_time = time.time() # 時間戳記 dae.fit(Xtrain, show_fig=True) end_time = time.time() # 時間戳記 mapping = dae.map2center(Xtrain) plt.scatter(mapping[:, 0], mapping[:, 1], c=Ytrain, s=100, alpha=0.5) print(end_time - start_time)
def main(): train_X, train_Y, _, _, = getKaggleMNIST() sample = 1000 train_X = train_X[:sample] train_Y = train_Y[:sample] tsne = TSNE() Z = tsne.fit_transform(train_X) plt.scatter(Z[:, 0], Z[:, 1], s=100, c=train_Y, alpha=0.5) plt.show()
def main(): Xtrain, Ytrain, _, _ = getKaggleMNIST() sample_size = 1000 X = Xtrain[:sample_size] Y = Ytrain[:sample_size] tsne = TSNE() Z = tsne.fit_transform(X) plt.scatter(Z[:,0], Z[:,1], s=100, c=Y, alpha=0.5) plt.show()
def test_pretraining_dnn(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() _, D = Xtrain.shape K = len(set(Ytrain)) dnn = DNN(D, [1000, 750, 500], K) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def main(): train_X, train_Y, test_X, test_Y = getKaggleMNIST() train_X = train_X.astype(np.float32) test_X = test_X.astype(np.float32) N, D = train_X.shape K = len(set(train_Y)) model = DNN(D, [1000, 750, 500], K) init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) model.set_session(session) model.fit(train_X, train_Y, test_X, test_Y, pretrain=True, epochs=10)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) # 500 300 2 300 500 dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 _, Rfull = gmm(X, 10, max_iter=30) print("full purity:", purity(Y, Rfull)) _, Rreduced = plot_k_means(Z, 10, max_iter=30) print("reduced purity:", purity(Y, Rreduced))
def main(): xtr, ytr, xts, yts = getKaggleMNIST() index = 0 print(xtr[index].shape) print(xtr[index]) im = xtr[index].reshape((28,28)) #plt.figure() #map(lambda x:plt.scatter(x,np.linspace(0,1)),xtr[index]) plt.figure() plt.imshow(im) plt.title("Label {}".format(ytr[index])) plt.show()
def main(): train_X, train_Y, test_X, test_Y = getKaggleMNIST() train_X = train_X.astype(np.float32) N, D = train_X.shape model = DeepAutoEncoder(D, [500, 300, 2]) init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) model.set_session(session) model.fit(train_X) mapping = model.transform(train_X) plt.scatter(mapping[:, 0], mapping[:, 1], c=train_Y, s=100, alpha=0.5) plt.show()
def main(): X_train, Y_train, X_test, Y_test = getKaggleMNIST() X_train = X_train.astype(np.float32) X_test = X_test.astype(np.float32) _, D = X_train.shape K = len(set(Y_train)) dnn = DNN(D, [1000, 750, 500], K, UnsupervisedModel=RBM) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(X_train, Y_train, X_test, Y_test, pretrain=True, epochs=10)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # same as autoencoder_tf.py Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape K = len(set(Ytrain)) dnn = DNN(D, [1000, 750, 500], K, UnsupervisedModel=RBM) init_op = tf.compat.v1.global_variables_initializer() with tf.compat.v1.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # same as autoencoder_tf.py Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape K = len(set(Ytrain)) dnn = DNN(D, [1000, 750, 500], K, UnsupervisedModel=RBM) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def singleVAE(): # get data X, _, _, _ = getKaggleMNIST() #X = (X > 0.5).astype(np.float32) N, D = X.shape print(D) # used 768 in tutorial vae = VAE(784, [200, 100]) vae.fit(X) # plot reconstruction done = False while not done: i = np.random.choice(len(X)) x = X[i] im = vae.posterior_predictive_sample([x]).reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title("Original") plt.subplot(1, 2, 2) plt.imshow(im, cmap='gray') plt.title("Sampled") plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True # plot output from random samples in latent space done = False while not done: im, probs = vae.prior_predictive_sample_with_probs() im = im.reshape(28, 28) probs = probs.reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(im, cmap='gray') plt.title("Prior predictive sample") plt.subplot(1, 2, 2) plt.imshow(probs, cmap='gray') plt.title("Prior predictive probs") plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def test_pretraining_dnn(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # dnn = DNN([1000, 750, 500]) # dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3) # vs Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape K = len(set(Ytrain)) dnn = DNN(D, [1000, 750, 500], K) init_op = tf.global_variables_initializer() with tf.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def test_pretraining_dnn(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # dnn = DNN([1000, 750, 500]) # dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3) # vs Xtrain = Xtrain.astype(np.float32) Xtest = Xtest.astype(np.float32) _, D = Xtrain.shape K = len(set(Ytrain)) dnn = DNN(D, [1000, 750, 500], K) init_op = tf.compat.v1.global_variables_initializer() with tf.compat.v1.Session() as session: session.run(init_op) dnn.set_session(session) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def main(): train_X, train_Y, test_X, test_Y = getKaggleMNIST() pca = PCA() train_Z = pca.fit_transform(train_X) plt.scatter(train_Z[:,0], train_Z[:,1], s=100, c=train_Y, alpha=0.5) plt.show() plt.plot(pca.explained_variance_ratio_) plt.show() cumulative_variance = [] last_variance = 0 for v in pca.explained_variance_ratio_: cumulative_variance.append(last_variance + v) last_variance = cumulative_variance[-1] plt.plot(cumulative_variance) plt.show()
def main(): Xtrain, Ytrain, _, _ = getKaggleMNIST() sample_size = 1000 X = Xtrain[:sample_size] Y = Ytrain[:sample_size] tsne = TSNE() Z = tsne.fit_transform(X) plt.scatter(Z[:, 0], Z[:, 1], s=100, c=Y, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 _, Rfull = gmm(X, 10, max_iter=30, smoothing=10e-1) print "full purity:", purity(Y, Rfull) _, Rreduced = gmm(Z, 10, max_iter=30, smoothing=10e-1) print "reduced purity:", purity(Y, Rreduced)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:, 0], mapping[:, 1], c=Ytrain, s=100, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 gmm = GaussianMixture(n_components=10) gmm.fit(Xtrain) responsibilities_full = gmm.predict_proba(Xtrain) print "full purity:", purity(Ytrain, responsibilities_full) gmm.fit(mapping) responsibilities_reduced = gmm.predict_proba(mapping) print "reduced purity:", purity(Ytrain, responsibilities_reduced)
def main(): X, Y, Xt, Yt = getKaggleMNIST() sample_size = 500 print(X[0:sample_size].shape, Y[0:sample_size].shape) #plt.scatter(X[0:10,0],X[0:10,1], s=70,c=Y[0:10],alpha=.5) #plt.show() tsne = TSNE() Z = tsne.fit_transform(X[0:sample_size], Y[:sample_size]) plt.subplot(211) plt.scatter(Z[:, 0], Z[:, 1], s=70, c=Y[:sample_size], alpha=.5) plt.colorbar() Z1 = tsne.fit_transform(Z) plt.subplot(212) plt.scatter(Z1[:, 0], Z1[:, 1], s=70, c=Y[:sample_size], alpha=.5) plt.colorbar() plt.show()
def main(): Xtrain, Ytrain, _, _ = getKaggleMNIST() sample_size = 1000 X = Xtrain[:sample_size] Y = Ytrain[:sample_size] tsne = TSNE() Z = tsne.fit_transform(X) plt.scatter(Z[:,0], Z[:,1], s=100, c=Y, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 _, Rfull = gmm(X, 10, max_iter=30, smoothing=10e-1) print "full purity:", purity(Y, Rfull) _, Rreduced = gmm(Z, 10, max_iter=30, smoothing=10e-1) print "reduced purity:", purity(Y, Rreduced)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 gmm = GaussianMixture(n_components=10) gmm.fit(Xtrain) responsibilities_full = gmm.predict_proba(Xtrain) print "full purity:", purity(Ytrain, responsibilities_full) gmm.fit(mapping) responsibilities_reduced = gmm.predict_proba(mapping) print "reduced purity:", purity(Ytrain, responsibilities_reduced)
def main(): X, _, _, _ = getKaggleMNIST() X = (X > 0.5).astype(np.float32) # we do bernuli vae = VariationalAutoencoder(784, [200, 100]) vae.fit(X) # plot reconstruction done = False while not done: i = np.random.chocie(len(X)) x = X[i] im = vae.posterior_predictive_sample([x]).reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title("Originial") plt.subplot(1, 2, 2) plt.imshow(im, cmap='gray') plt.title("Sampled") plt.show() # ans = input("Gen next one?") # if ans and ans[0] in ('n' or 'N'): # done = True done = True # plot reconstruction done = False while not done: im, probs = vae.prior_predictive_sample_with_probs() im = im.reshape(28, 28) probs = probs.reshape(28, 28) plt.subplot(1, 2, 1) plt.imshow(im, cmap='gray') plt.title("Originial") plt.subplot(1, 2, 2) plt.imshow(probs, cmap='gray') plt.title("Sampled") plt.show() # ans = input("Gen next one?") # if ans and ans[0] in ('n' or 'N'): # done = True done = True
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() pca = PCA() reduced = pca.fit_transform(Xtrain) plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain, alpha=0.5) plt.show() plt.plot(pca.explained_variance_ratio_) plt.show() cumulative = [] last = 0 for v in pca.explained_variance_ratio_: cumulative.append(last + v) last = cumulative[-1] plt.plot(cumulative) plt.show()
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 # NOTE: this will take a long time (i.e. just leave it overnight) gmm = GaussianMixture(n_components=10) gmm.fit(Xtrain) print("Finished GMM training") responsibilities_full = gmm.predict_proba(Xtrain) print("full purity:", purity(Ytrain, responsibilities_full)) gmm.fit(mapping) responsibilities_reduced = gmm.predict_proba(mapping) print("reduced purity:", purity(Ytrain, responsibilities_reduced))
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dae = DeepAutoEncoder([500, 300, 2]) dae.fit(Xtrain) mapping = dae.map2center(Xtrain) plt.scatter(mapping[:, 0], mapping[:, 1], c=Ytrain, s=100, alpha=0.5) plt.show() # purity measure from unsupervised machine learning pt 1 # NOTE: this will take a long time (i.e. just leave it overnight) gmm = GaussianMixture(n_components=10) gmm.fit(Xtrain) print("Finished GMM training") responsibilities_full = gmm.predict_proba(Xtrain) print("full purity:", purity(Ytrain, responsibilities_full)) gmm.fit(mapping) responsibilities_reduced = gmm.predict_proba(mapping) print("reduced purity:", purity(Ytrain, responsibilities_reduced))
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() pca = PCA() reduced = pca.fit_transform(Xtrain) plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain, alpha=0.5) plt.show() plt.plot(pca.explained_variance_ratio_) plt.show() # cumulative variance # choose k = number of dimensions that gives us 95-99% variance cumulative = [] last = 0 for v in pca.explained_variance_ratio_: cumulative.append(last + v) last = cumulative[-1] plt.plot(cumulative) plt.show()
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() pca = PCA() reduced = pca.fit_transform(Xtrain) plt.scatter(reduced[:, 0], reduced[:, 1], s=100, c=Ytrain, alpha=0.5) plt.show() plt.plot(pca.explained_variance_ratio_) plt.show() # cumulative variance # choose k = number of dimensions that gives us 95-99% variance cumulative = [] last = 0 for v in pca.explained_variance_ratio_: cumulative.append(last + v) last = cumulative[-1] plt.plot(cumulative) plt.show()
def test_single_autoencoder(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() autoencoder = AutoEncoder(300, 0) autoencoder.fit(Xtrain, epochs=2, show_fig=True) done = False while not done: i = np.random.choice(len(Xtest)) x = Xtest[i] y = autoencoder.predict([x]) plt.subplot(1,2,1) plt.imshow(x.reshape(28,28), cmap='gray') plt.title('Original') plt.subplot(1,2,2) plt.imshow(y.reshape(28,28), cmap='gray') plt.title('Reconstructed') plt.show() ans = input("Generate another?") if ans and ans[0] in ('n' or 'N'): done = True
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dnn = DNN([1000, 750, 500], UnsupervisedModel=RBM) dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3)
def main(): Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() dnn = ANN([1000, 750, 500]) dnn.fit(Xtrain, Ytrain)
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow from __future__ import print_function, division from future.utils import iteritems from builtins import range, input # Note: you may need to update your version of future # sudo pip install -U future import numpy as np from sklearn.neural_network import MLPRegressor from util import getKaggleMNIST # get data X, _, Xt, _ = getKaggleMNIST() # create the model and train it model = MLPRegressor() model.fit(X, X) # test the model print("Train R^2:", model.score(X, X)) print("Test R^2:", model.score(Xt, Xt)) Xhat = model.predict(X) mse = ((Xhat - X)**2).mean() print("Train MSE:", mse) Xhat = model.predict(Xt) mse = ((Xhat - Xt)**2).mean()
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python # https://www.udemy.com/unsupervised-deep-learning-in-python from __future__ import print_function, division from builtins import range, input # Note: you may need to update your version of future # sudo pip install -U future import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA, TruncatedSVD from util import getKaggleMNIST X, Y, _, _ = getKaggleMNIST() m = X.mean(axis=0) s = X.std(axis=0) np.place(s, s == 0, 1) X = (X - m) / s pca = PCA() svd = TruncatedSVD() Z1 = pca.fit_transform(X) Z2 = svd.fit_transform(X) plt.subplot(1,2,1) plt.scatter(Z1[:,0], Z1[:,1], c=Y) plt.subplot(1,2,2) plt.scatter(Z2[:,0], Z2[:,1], c=Y) plt.show()
from __future__ import print_function, division from builtins import range # Note: you may need to update your version of future # sudo pip install -U future import numpy as np import matplotlib.pyplot as plt from util import getKaggleMNIST from keras.models import Model from keras.layers import Dense, Activation, Input, Dropout # get the data Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST() # get shapes N, D = Xtrain.shape K = len(set(Ytrain)) # ANN with layers [784] -> [500] -> [300] -> [10] i = Input(shape=(D,)) x = Dropout(0.2)(i) x = Dense(500, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(300, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(K, activation='softmax')(x) # instantiate the model object