def __init__(self, decoder=None, train_xs=None, train_ys=None, test_xs=None, test_ys=None, mean=None, std=None): """Wrapper for cifar10 dataset, or encoded latent_cifar10 dataset.""" self.normalized = False self.scaled = False if decoder is None: self.is_latent = False ((self.train_xs, self.train_ys), (self.test_xs, self.test_ys)) = cifar10.load_data() self.train_xs = utils.preprocess_cifar10(self.train_xs) self.test_xs = utils.preprocess_cifar10(self.test_xs) self.mean = 0.0 self.std = 1.0 else: assert (train_xs is not None and train_ys is not None and test_xs is not None and test_ys is not None) self.is_latent = True self.decoder = decoder assert train_xs.shape[1:] == test_xs.shape[1:] self.latent_shape = train_xs.shape[1:] self.train_xs = _flattern(train_xs) self.train_ys = train_ys self.test_xs = _flattern(test_xs) self.test_ys = test_ys if mean is None: self.mean = np.vstack((self.train_xs, self.test_xs)).mean() print 'computed dataset mean:', self.mean else: self.mean = mean if std is None: self.std = np.vstack((self.train_xs, self.test_xs)).std() print 'computed dataset std:', self.std else: self.std = std
def load_default(cls): ((train_xs, train_ys), (test_xs, test_ys)) = cifar10.load_data() train_xs = utils.preprocess_cifar10(train_xs) test_xs = utils.preprocess_cifar10(test_xs) return cls(train_xs, train_ys, test_xs, test_ys)
def split_dataset(xs, ys, idx): loc = np.where(ys == idx)[0] return xs[loc], ys[loc] if __name__ == '__main__': keras.backend.set_session(utils.get_session()) (train_xs, train_ys), (test_xs, test_ys) = cifar10.load_data() train_xs, train_ys = split_dataset(train_xs, train_ys, 7) test_xs, test_ys = split_dataset(test_xs, test_ys, 7) print 'Training set shape:', train_xs.shape train_xs, mean, std = utils.preprocess_cifar10(train_xs) test_xs, _, _ = utils.preprocess_cifar10(test_xs) batch_size = 128 auto, loss = deep_model1(train_xs.shape[1:]) # opt = keras.optimizers.SGD(lr=0.1, momentum=0.9, decay=0.0, nesterov=True) opt = keras.optimizers.Adam() auto.compile(optimizer=opt, loss=loss) model_name = 'vae2_horse' if not os.path.exists(model_name): os.makedirs(model_name) plot(auto, to_file=os.path.join(model_name, 'graph.png'), show_shapes=True) print 'model graph plotted' # auto.load_weights(os.path.join(model_name, 'encoder_decoder.h5'))
if __name__ == '__main__': if len(sys.argv) < 3 or len(sys.argv) > 4: print 'usage: python fc.py pcd/cd cd-k [output_dir]' sys.exit() else: use_pcd = (sys.argv[1] == 'pcd') cd_k = int(sys.argv[2]) output_dir = None if len(sys.argv) == 3 else sys.argv[3] if keras.backend.image_dim_ordering() != 'tf': keras.backend.set_image_dim_ordering('tf') print "INFO: temporarily set 'image_dim_ordering' to 'tf'" (train_xs, _), (_, _) = cifar10.load_data() train_xs, mean, std = utils.preprocess_cifar10(train_xs) batch_size = 20 pcd_chain_size = 100 lr = 0.0001 if use_pcd else 1e-5 train_xs = train_xs[:, :, :, :1] #.reshape(-1, 32*32) rbm = GaussianCRBM((32, 32, 1), (12, 12, 1, 256), (2, 2), 'VALID', output_dir, {}) # rbm = GaussianRBM(32*32, 1000, output_dir) # drbn = DRBN([32*32], output_dir) # drbn.add_fc_layer(500, 'fc1', use_gaussian=True) # drbn.add_fc_layer(500, 'fc2') # drbn.add_fc_layer(1000, 'fc3') # drbn.print_network() train_rbm.train(rbm,