def main(): X, Y = get_mnist_data(normalize=True) N, D = X.shape Xtest, Ytest = X[-100:], Y[-100:] X, Y = X[:-100], Y[:-100] model = Autoencoder(D, [300], loss_fn='sigmoid_cross-entropy') model.fit(X, epochs=15) # display reconstruction: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] image = model.predict(np.expand_dims(x, axis=0)).reshape( 28, 28) # or just use [x] in model.predict(): # we need to input a matrix, not a vector plt.subplot(1, 2, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(image, cmap='gray') plt.title('Reconstruction') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break
def __init__(self, sess, args): super(NN_Classifier, self).__init__() self.dataset_dir = args.dataset_dir self.checkpoint_dir = args.checkpoint_dir self.result_dir = args.result_dir self.nn_classifier = nn_classifier self.sess = sess self.lr = args.lr self.hidden_dim = args.hidden_dim self.layers_num = args.layers_num self.dropout = args.dropout self.keep_prob = args.keep_prob self.model_name = 'fc_classifier_' + str(self.layers_num) + '_' + str( self.hidden_dim) if args.data_name == 'mnist': self.image_size = 28 self.c_dim = 1 self.mnist = utils.get_mnist_data(os.path.join( self.dataset_dir, args.data_name), one_hot=True) self.batch_size = args.batch_size self.vali_batch_size = args.vali_batch_size self._build_model() self.saver = tf.train.Saver()
def mnist(): g_params = { "latent_dim": 64, "g_dropout_rate": 0.6, #Projection params "pp1": [4, 1], "pp2": [7, 7], #Filters, kernel_size, stride "dp1": [64, 5, 2], "dp2": [64, 5, 2], "dp3": [64, 5, 1], "dp4": [1, 5, 1], } d_params = { "d_dropout_rate": 0.5, "conv1_params": [64, 5, 2], "conv2_params": [64, 5, 1], "conv3_params": [64, 5, 1], "dense1_params": [128], "dense2_params": [1] } X_train = get_mnist_data() model = GAN(d_params, g_params) mnist_iterator = MnistIterator(X_train, len(X_train), 64) model.fit(mnist_iterator, title="Mnist")
def main(): X, Y = get_mnist_data(normalize=True) # binarize the pictures to get proper Bernoulli random variables # (isn't neccessary; one could try both): X = (X > 0.5).astype(np.float32) Xtest, Ytest = X[-100:], Y[-100:] X, Y = X[:-100], Y[:-100] # X, Y, Xtest, Ytest = get_fashion_mnist_data(normalize=True) # binarize the pictures: # X, Xtest = (X > 0.5).astype(np.float32), (Xtest > 0.5).astype(np.float32) N, D = X.shape vae = VariationalAutoencoder(D, [200, 100]) vae.fit(X) # display original images, posterior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] sample, probs = vae.posterior_predictive_sample_and_probs([x]) plt.subplot(1, 3, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original Image') plt.subplot(1, 3, 2) plt.imshow(sample.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Sample') plt.subplot(1, 3, 3) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break # display prior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] image, probs = vae.prior_predictive_sample_and_probs() plt.subplot(1, 2, 1) plt.imshow(image.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Sample') plt.subplot(1, 2, 2) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break
def main(): # load the data: X, Y = get_mnist_data(normalize=True) # classifier: cl = BayesClassifier() cl.fit(X, Y) for i in range(cl.K): plt.subplot(1, 2, 1) plt.imshow(cl.sample(i).reshape(28, 28), cmap='gray') plt.title('Generated Sample') plt.subplot(1, 2, 2) plt.imshow(cl.gauss[i]['mean'].reshape(28, 28), cmap='gray') plt.title('Mean') plt.suptitle('class "%s"' % i) plt.show() sample = cl.random_sample() plt.imshow(sample.reshape(28, 28), cmap='gray') plt.title('a drawn sample') plt.show()
def __init__(self, sess, args): super(LeNet_5, self).__init__() self.dataset_dir = args.dataset_dir self.checkpoint_dir = args.checkpoint_dir self.result_dir = args.result_dir self.lenet_5 = lenet_5 self.model_name = 'lenet5_classifier' self.sess = sess self.lr = args.lr if args.data_name == 'mnist': self.image_size = 32 self.c_dim = 1 self.mnist = utils.get_mnist_data(os.path.join( self.dataset_dir, args.data_name), one_hot=True) self.batch_size = args.batch_size self.vali_batch_size = args.vali_batch_size self._build_model() self.saver = tf.train.Saver()
from gan_utils import train_gan from utils import get_mnist_data from ConvDiscriminator import ConvDiscriminatorMNIST from ConvGenerator import ConvGeneratorMNIST from CapsDiscriminator import CapsDiscriminatorMNIST from metrics import get_inception_score batch_size = 128 epochs = 25 save_images = True train_data, test_data = get_mnist_data(batch_size=batch_size) #DCGAN -> G: Conv D: Conv G = ConvGeneratorMNIST() D = ConvDiscriminatorMNIST() # train_gan(G,D, train_data, epochs, batch_size, save_gen_images=save_images, filename_prefix="ConvConv/train") #CapsGAN -> G: Conv D: Caps G = ConvGeneratorMNIST() # D = CapsDiscriminatorMNIST(input_size=[1, 28, 28], classes=1, routings=3) D = CapsDiscriminatorMNIST(input_size=[1, 28, 28], classes=1, routings=3, d=128, num_dims=8, num_maps=32) train_gan( generator=G, discriminator=D, image_loader=train_data, num_epochs=epochs, batch_size=batch_size, cuda=True, g_lr=1e-3, d_lr=0.0002,
def main(dataset='fashion_mnist'): # load the data: if dataset == 'fashion_mnist': print('\nbenchmarking on fashion_mnist (no preprocessing):') print( 'optimizer: sgd + momentum, mini-batch size: 64, initial lr: 0.01, mu: 0.9, epochs: 40\n' ) epochs = 40 (Xtrain, Ytrain), (Xtest, Ytest), label_names = get_fashion_mnist_data( normalize=False, add_label_names=True) else: print('\nbenchmarking on mnist (no preprocessing):') print( 'optimizer: sgd + momentum, mini-batch size: 64, initial lr: 0.01, mu: 0.9, epochs: 25\n' ) epochs = 25 (Xtrain, Ytrain), (Xtest, Ytest) = get_mnist_data(normalize=False) N = Xtrain.shape[0] K = len(set(Ytrain.ravel())) # plot randomly selected training samples: for n in range(3): i = np.random.choice(N) sample = Xtrain[i] plt.imshow(sample[:, :, 0], cmap='gray') if dataset == 'fashion_mnist': plt.title(label_names[Ytrain[i]]) else: plt.title(Ytrain[i]) plt.show() # define our model model = melnyk_net(input_shape=Xtrain.shape[1:], num_classes=K) # model.summary() # learning rate: lr = 0.01 sgd = optimizers.SGD(lr, momentum=0.9) model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) csv_logger = CSVLogger('training_%s.log' % dataset, append=False) lr_reducer = ReduceLROnPlateau(monitor='acc', factor=0.1, patience=0, verbose=1, mode='auto', min_delta=0.0001, min_lr=0.000001) callbacks_list = [csv_logger, lr_reducer] r = model.fit( Xtrain, Ytrain, validation_data=(Xtest, Ytest), # validation_split=0.2, epochs=1, batch_size=64, shuffle=True, verbose=1, callbacks=callbacks_list, ) score = model.evaluate(Xtest, Ytest, verbose=1) model.save('%s-model.h5' % dataset) print('Competition loss:', score[0]) print('Competition accuracy:', score[1]) # plot and save the losses and accuracies: df = pd.read_csv('training_%s.log' % dataset) # plot the train and the validation cost: plt.plot(df['loss'], label='train_loss') plt.plot(df['val_loss'], label='val_loss') plt.xlabel('epochs') plt.ylabel('cost') plt.legend() plt.savefig('%s-loss.png' % dataset) plt.show() plt.gcf().clear() # clear the figure before plotting another # plot the train and the validation accuracy: plt.plot(df['acc'], label='train_acc') plt.plot(df['val_acc'], label='val_acc') plt.xlabel('epochs') plt.ylabel('accuracy') plt.legend() plt.savefig('%s-accuracies.png' % dataset) plt.show()
import torch import torch.nn as nn import torch.optim as optim from modele import Net_Z, Net_H, Net_G from utils import get_mnist_data, plot_images, freeze, unfreeze, momentum_correction, \ weights_init, sample_z, select_white_line_images workers = 2 # Decide which device we want to run on device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") batch_size = 16 # Get MNIST data train_dataloader, test_dataloader = get_mnist_data(batch_size, workers=workers) # Plot some training images real_batch = next(iter(train_dataloader)) plot_images(real_batch[0].to(device), title="Training Images") # Create mask mask = torch.ones((1, 32, 32), device=device, dtype=torch.bool) mask[0, 4:14, 4:20] = 0 masked_batch = real_batch[0] * mask.unsqueeze(0).cpu() plot_images(masked_batch.to(device), title="Masked Images") # White line real_batch[0][:, 0, 8:11, 6:16] = 1 white_line_batch = real_batch[0] plot_images(white_line_batch.to(device), title="Images with a white line")
hidden_1 = tf.nn.relu(tf.matmul(x, W_1) + b_1) W_2 = tf.Variable(initial_value=tf.random_normal(shape=[hidden_dim, n_classes]), name='l2_weights') b_2 = tf.Variable(initial_value=tf.zeros(shape=[n_classes]), name='l2_biases') logits = tf.matmul(hidden_1, W_2) + b_2 y = tf.nn.softmax(logits) return y if __name__ == '__main__': # Load MNIST data mnist = get_mnist_data('/tmp/mnist', verbose=True) # Placeholders x = tf.placeholder(dtype=tf.float32, shape=[None, 784]) # input placeholder # Placeholder for targets targets = tf.placeholder(dtype=tf.float32, shape=[None, 10]) # Define model output y = multi_layer_net(x) # Define loss function loss = tf.reduce_mean(-tf.reduce_sum(targets * tf.log(y + np.finfo('float32').eps), axis=1)) # Define train step train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
def main(): X, Y = get_mnist_data(normalize=True) # binarize the pictures: X = (X > 0.5).astype(np.float32) Xtest, Ytest = X[-100:], Y[-100:] X, Y = X[:-100], Y[:-100] # X, Y, Xtest, Ytest = get_fashion_mnist_data(normalize=True) # # binarize the pictures: # X, Xtest = (X > 0.5).astype(np.float32), (Xtest > 0.5).astype(np.float32) N, D = X.shape # create a vae: vae = VariationalAutoencoder('./tf.model') vae.fit(D, [200, 100], X) # print('Print some params:') # params = vae.get_params() # [print(p[0,:5]) for p in params[::2]] # display original images, posterior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] sample, probs = vae.posterior_predictive([x]) plt.subplot(1, 3, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original Image') plt.subplot(1, 3, 2) plt.imshow(sample.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Sample') plt.subplot(1, 3, 3) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break # display prior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] image, probs = vae.prior_predictive_and_probs() plt.subplot(1, 2, 1) plt.imshow(image.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Sample') plt.subplot(1, 2, 2) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break # save the model: print('\nsaving the model.....') vae.save('my_trained_vae.json') # load the saved model and display samples again: print('\nloading the model.....') vae = VariationalAutoencoder.load('my_trained_vae.json') # print('Print some params after loading:') # params = vae.get_params() # [print(p[0,:5]) for p in params[::2]] # display original images, posterior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] sample, probs = vae.posterior_predictive([x]) plt.subplot(1, 3, 1) plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Original Image') plt.subplot(1, 3, 2) plt.imshow(sample.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Sample') plt.subplot(1, 3, 3) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Posterior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break # display prior predictive samples and probabilities: while True: i = np.random.choice(len(Xtest)) x = Xtest[i] image, probs = vae.prior_predictive_and_probs() plt.subplot(1, 2, 1) plt.imshow(image.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Sample') plt.subplot(1, 2, 2) plt.imshow(probs.reshape(28, 28), cmap='gray') plt.title('Prior Predictive Probs') plt.show() prompt = input('Continue generating?\n') if prompt and prompt[0] in ['n', 'N']: break
def main(args): args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") num_samples_plot = min(args.batch_size, 9) model = get_mnist_model() # Either train new model or load pretrained weights prepare_mnist_model(model, epochs=args.epochs, train_new=args.train_new) model = model.to(args.device) train_loader, test_loader = get_mnist_data(transform=torchvision.transforms.ToTensor(), batch_size=args.batch_size) # Sample batch from test_loader for x, y in test_loader: break x = x[:num_samples_plot].to(args.device) y = y[:num_samples_plot].to(args.device) x.requires_grad_(True) with torch.no_grad(): y_hat = model(x) pred = y_hat.max(1)[1] def compute_and_plot_explanation(rule, ax_, title=None, postprocess=None, pattern=None, cmap='seismic'): # # # # For the interested reader: # This is where the LRP magic happens. # Reset gradient x.grad = None # Forward pass with rule argument to "prepare" the explanation y_hat = model.forward(x, explain=True, rule=rule, pattern=pattern) # Choose argmax y_hat = y_hat[torch.arange(x.shape[0]), y_hat.max(1)[1]] # y_hat *= 0.5 * y_hat # to use value of y_hat as starting point y_hat = y_hat.sum() # Backward pass (compute explanation) y_hat.backward() attr = x.grad if postprocess: # Used to compute input * gradient with torch.no_grad(): attr = postprocess(attr) attr = heatmap_grid(attr, cmap_name=cmap) if title is None: title = rule plot_attribution(attr, ax_, pred, title, cmap=cmap) # # # # Patterns for PatternNet and PatternAttribution all_patterns_path = (base_path / 'examples' / 'patterns' / 'pattern_all.pkl').as_posix() if not os.path.exists(all_patterns_path): # Either load of compute them patterns_all = fit_patternnet(model, train_loader, device=args.device) store_patterns(all_patterns_path, patterns_all) else: patterns_all = [torch.tensor(p, device=args.device, dtype=torch.float32) for p in load_patterns(all_patterns_path)] pos_patterns_path = (base_path / 'examples' / 'patterns' / 'pattern_pos.pkl').as_posix() if not os.path.exists(pos_patterns_path): patterns_pos = fit_patternnet_positive(model, train_loader, device=args.device)#, max_iter=1) store_patterns(pos_patterns_path, patterns_pos) else: patterns_pos = [torch.from_numpy(p).to(args.device) for p in load_patterns(pos_patterns_path)] # # # Plotting fig, ax = plt.subplots(2, 5, figsize=(10, 5)) with torch.no_grad(): x_plot = heatmap_grid(x*2-1, cmap_name="gray") plot_attribution(x_plot, ax[0, 0], pred, "Input") # compute_and_plot_explanation("gradient", ax[1, 0], title="gradient") compute_and_plot_explanation("gradient", ax[1, 0], title="input $\\times$ gradient", postprocess = lambda attribution: attribution * x) compute_and_plot_explanation("epsilon", ax[0, 1]) compute_and_plot_explanation("gamma+epsilon", ax[1, 1]) # compute_and_plot_explanation("alpha1beta0", ax[0, 2]) compute_and_plot_explanation("alpha2beta1", ax[1, 2]) # compute_and_plot_explanation("patternnet", ax[0, 3], pattern=patterns_all, title="PatternNet $S(x)$", cmap='gray') compute_and_plot_explanation("patternnet", ax[1, 3], pattern=patterns_pos, title="PatternNet $S(x)_{+-}$", cmap='gray') compute_and_plot_explanation("patternattribution", ax[0, 4], pattern=patterns_all, title="PatternAttribution $S(x)$") compute_and_plot_explanation("patternattribution", ax[1, 4], pattern=patterns_pos, title="PatternAttribution $S(x)_{+-}$") fig.tight_layout() fig.savefig((base_path / 'examples' / 'plots' / "mnist_explanations.png").as_posix(), dpi=280) plt.show()
def main(args): num_samples_plot = min(args.batch_size, 9) model = get_mnist_model() prepare_mnist_model(model, epochs=args.epochs, train_new=args.train_new) model = model.cpu() train_loader, test_loader = get_mnist_data( transform=torchvision.transforms.ToTensor(), batch_size=args.batch_size) # Sample batch for x, y in train_loader: break x = x[:num_samples_plot] y = y[:num_samples_plot] x.requires_grad_(True) with torch.no_grad(): y_hat = model(x) pred = y_hat.max(1)[1] def run_and_plot_rule(rule, ax_, title=None, postprocess=None, pattern=None, cmap='seismic'): # Reset gradient x.grad = None # Forward pass and select argmax y_hat = model.forward(x, explain=True, rule=rule, pattern=pattern) y_hat = y_hat[torch.arange(x.shape[0]), y_hat.max(1)[1]] y_hat = y_hat.sum() # Backward pass y_hat.backward() attr = x.grad if postprocess: # Used to compute input * gradient with torch.no_grad(): attr = postprocess(attr) attr = prepare_batch_for_plotting(attr) if title is None: title = rule plot_attribution(attr, ax_, pred, title, cmap=cmap) # Patterns all_patterns_path = (base_path / 'examples' / 'pattern_all.pkl').as_posix() if not os.path.exists(all_patterns_path): patterns_all = fit_patternnet(model, train_loader) store_patterns(all_patterns_path, patterns_all) else: patterns_all = load_patterns(all_patterns_path) pos_patterns_path = (base_path / 'examples' / 'pattern_pos.pkl').as_posix() if not os.path.exists(pos_patterns_path): patterns_pos = fit_patternnet_positive(model, train_loader) store_patterns(pos_patterns_path, patterns_pos) else: patterns_pos = load_patterns(pos_patterns_path) # Plotting fig, ax = plt.subplots(2, 5, figsize=(10, 5)) with torch.no_grad(): x_plot = prepare_batch_for_plotting(x * 2. - 1) plot_attribution(x_plot, ax[0, 0], pred, "Input", cmap='gray') # run_and_plot_rule("gradient", ax[0, 0]) run_and_plot_rule("gradient", ax[1, 0], title="input $\\times$ gradient", postprocess=lambda attribution: attribution * x) run_and_plot_rule("epsilon", ax[0, 1]) run_and_plot_rule("gamma+epsilon", ax[1, 1]) run_and_plot_rule("alpha1beta0", ax[0, 2]) run_and_plot_rule("alpha2beta1", ax[1, 2]) run_and_plot_rule("patternattribution", ax[0, 3], pattern=list(patterns_all), title="PatternAttribution $S(x)$") run_and_plot_rule("patternattribution", ax[1, 3], pattern=list(patterns_pos), title="PatternAttribution $S(x)_{+-}$") run_and_plot_rule("patternnet", ax[0, 4], pattern=patterns_all, title="PatternNet $S(x)$", cmap='gray') run_and_plot_rule("patternnet", ax[1, 4], pattern=patterns_pos, title="PatternNet $S(x)_{+-}$", cmap='gray') fig.tight_layout() fig.savefig( (base_path / 'examples' / "Example_explanations.png").as_posix(), dpi=280) plt.show()
argParser.add_argument("-train", "--{}".format(ARG_TRAIN_DATA), required=True, help="path to train_split") argParser.add_argument("-test", "--{}".format(ARG_TEST_DATA), required=True, help="path to test_split") argParser.add_argument("-percent", "--{}".format(ARG_PERCENT), required=True, help="percent of train data") args = vars(argParser.parse_args()) print("Getting data...") X, y = utils.get_mnist_data() #X.shape = (70k, 784), y.shape = (70000,) # normalize the mnist dataset X = X / 255 # plt.imshow(X[20000, :].reshape(28,28), cmap = matplotlib.cm.binary) # # plt.imshow(X_train[:, 20000].reshape(28,28), cmap = matplotlib.cm.binary) # plt.axis("off") # plt.show() #X = np.where(X >= 0.4, 1.0, 0.0) utils.saveToFile("data/mnist.dat", (X, y)) print("Saved") n_digits = 10
def main(): X, Y = get_mnist_data(normalize=True) # binarize the pictures: # X = (X > 0.5).astype(np.float32) N, D = X.shape # display some images: for i in range(len(X)): x = X[i] plt.imshow(x.reshape(28, 28), cmap='gray') plt.title('Image of \'%d\'' % Y[i]) plt.show() prompt = input('Continue displaying?\n') if prompt and prompt[0] in ['n', 'N']: break # in order to visualize the latent space, # we'll make it of dimensionality 2, so # the last layer of the ENCODER will have just 2 units: vae = VariationalAutoencoder(784, [200, 100, 2]) # the fit() method of the VariationalAutoencoder class # performs mini-batch SGD - it shuffles the input data; # you can make sure of it: # vae.fit(X) # for i in range(len(X)): # x = X[i] # plt.imshow(x.reshape(28, 28), cmap='gray') # plt.title('Image of \'%d\'' % Y[i]) # plt.show() # prompt = input('Continue displaying?\n') # if prompt and prompt[0] in ['n', 'N']: # exit() # for later visualization we need to retain the original order, # to do this, we will pass in a copy of X: vae.fit(X.copy(), epochs=15) # get the latent space: Z = vae.transform(X) # returns 2 values for each sample - calculated means # make a scatter plot of how an instance of a class maps to the latent space Z: # (we want to show a discrete colorbar) K = len(set(Y)) # number of classes = number of discrete color levels # get a colormap: cmap = plt.get_cmap('viridis', K) # make a plot: plt.scatter(Z[:, 0], Z[:, 1], c=Y, s=15, alpha=0.5) # plot a discrete colormap: norm = matplotlib.colors.BoundaryNorm(np.arange(0, K+1)-0.5, K) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) plt.colorbar(sm, ticks=np.arange(0, K)) # name and display the plot: plt.title('Visualization of the Latent Space') plt.show() # plot what image is reproduced for different parts of Z: # we are going to use the prior_predictive_probs_given_input() method; # first, we generate an empty image to fill it in later: n = 30 # number of images per side image = np.empty((28 * n, 28 * n)) # D is the height and width of the input samples # generate the latent vectors: Z = [] # a list of latern vectors x_values = np.linspace(-1, 1, n) y_values = np.linspace(-1, 1, n) # we are going to make a prediction for all input latent vectors simultaneously: for x in x_values: for y in y_values: z = [x, y] Z.append(z) # reconstructed images: X_reconstructed = vae.prior_predictive_probs_given_input(Z) idx = 0 for i, x in enumerate(x_values): for j, y in enumerate(y_values): x_reconstructed = X_reconstructed[idx].reshape(28, 28) idx += 1 # map it to a particular part of the pre-created image image[(n - 1 - i) * 28 : (n - i) * 28, j * 28 : (j + 1) * 28] = x_reconstructed plt.imshow(image, cmap='gray') plt.axis('off') plt.show()
import numpy as np import matplotlib.pyplot as plt from datetime import datetime import import_ipynb from perceptron_intuition.ipynb import Perceptron import sys sys.path.append('../functions/') from utils import get_mnist_data, generate_simple_xor fn = '../mnist/train.csv' X, Y = get_mnist_data(fn) # perceptron is only capable of binary classification # therfore only take samples where Y==0 and Y==1 then # change these values to -1 and +1 idx = np.logical_or(Y == 0, Y == 1) X, Y = X[idx], Y[idx] Y[Y == 0] == -1 model = Perceptron() t0 = datetime.now() model.fit(X, Y, lr=10e-3) print('MNIST train accuracy:', model.score(X, Y)) print('\n XOR results:') X, Y = generate_simple_xor() model.fit(X, Y) print('XOR accuracy:', model.score(X, Y))
[0, 2, 3, 1]) # Back to batch first # print("Call Fcn: shape of output", outputs.shape) # 5. Add the lateral and the feed-forward activations outputs += inputs return outputs if __name__ == "__main__": input_dims = (IMG_ROWS, IMG_COL, 1) # Input dimensions for a single sample # 1. Get Data # -------------------------------------------------------------------------- x_train, y_train, x_test, y_test, x_sample, y_sample = utils.get_mnist_data( ) # 2. Define the model # ------------------------------------------- model = Sequential() # First Convolution layer, First sublayer processes feed-forward inputs, second layer adds the # lateral connections. Third sublayer adds the activation function. # Output = sigma_fcn([W_ff*x + W_l*(W_ff*x)]). # Where sigma_fcn is the activation function model.add( Conv2D(32, kernel_size=(3, 3), input_shape=input_dims, padding='same')) model.add(ContourIntegrationLayer(n=5)) model.add(Activation('relu')) # Rest of the layers. model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
def train(opt_dict): train_set, valid_set, test_set = get_mnist_data() x_train, y_train = instances_to_bags(ds=train_set, n_inst=FLAGS.n_inst, target=FLAGS.target, n_bags=FLAGS.n_bags, p=FLAGS.prob_target) x_test, y_test = instances_to_bags(ds=test_set, n_inst=FLAGS.n_inst, target=FLAGS.target, n_bags=1000, p=FLAGS.prob_target) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(FLAGS.n_epoches): # Train true_train = np.empty([ 0, ]) pred_train = np.empty([ 0, ]) for i in range(x_train.shape[0]): st_i = time.time() xi = x_train[i] yi = np.asarray([y_train[i]]) feed_dict = {opt_dict.x: xi, opt_dict.y: yi} ops = [opt_dict.train_op, opt_dict.loss, opt_dict.logits] _, loss, logits = sess.run(ops, feed_dict=feed_dict) et_i = time.time() print ("Training", epoch, "-th epoch\t", \ i, "-th bag\t Loss=", loss, "\t Time:", round(et_i-st_i,3), "(s)") true_train = np.concatenate([true_train, yi], axis=0) pred_train = np.concatenate( [pred_train, np_sigmoid(logits)], axis=0) print_metrics(true_train, pred_train) # Test true_test = np.empty([ 0, ]) pred_test = np.empty([ 0, ]) for i in range(x_train.shape[0]): st_i = time.time() xi = x_test[i] yi = np.asarray([y_test[i]]) feed_dict = {opt_dict.x: xi, opt_dict.y: yi} ops = [opt_dict.loss, opt_dict.logits] loss, logits = sess.run(ops, feed_dict=feed_dict) et_i = time.time() true_test = np.concatenate([true_test, yi], axis=0) pred_test = np.concatenate( [pred_test, np_sigmoid(logits)], axis=0) print_metrics(true_test, pred_test) print("Finish training and test") return