start_time = time.time() counter = 1 mse_loss = torch.nn.MSELoss(reduction='mean') mse_loss_sum = torch.nn.MSELoss(reduction='sum') latent_mse_loss = torch.nn.MSELoss(reduction='sum') from covNetModel import Encoder from covNetModel import Decoder num_samp = opt.num_samp encoder = Encoder(z_dim, opt.c_dim, opt.img_size) decoder = Decoder(z_dim, opt.c_dim, opt.img_size) transNet = TransOpt() sampler_c = Sample_c() # Load a pretrained file - make sure the model parameters fit the parameters of rht pre-trained models if data_use == 'rotDigits': checkpoint = torch.load( './pretrained_models/rotMNIST/network_batch32_zdim10.pt') elif data_use == 'natDigits': checkpoint = torch.load( './pretrained_models/natMNIST/network_batch32_zdim6.pt') # Or specify a network that's been trained independently #checkpoint = torch.load(save_folder + 'network_batch' + str(batch_orig) + '_zdim' + str(opt.z_dim) + '_step' + str(opt.stepUse) + '.pt') encoder.load_state_dict(checkpoint['model_state_dict_encoder']) decoder.load_state_dict(checkpoint['model_state_dict_decoder']) sample_X, sample_labels, _ = load_mnist("test")
data_X, data_y, y_orig = load_mnist("train") sample_labels = data_y sample_X_torch = torch.from_numpy(data_X) sample_X_torch = sample_X_torch.permute(0, 3, 1, 2) sample_X_torch = sample_X_torch.float() batch_idxs = len(data_X) // opt.batch_size anchor_X, anchor_y = select_mnist_anchors(data_X, data_y, opt.num_anchor) anchor_X_torch = torch.from_numpy(anchor_X) anchor_X_torch = anchor_X_torch.permute(0, 3, 1, 2) anchor_X_torch = anchor_X_torch.float() encoder = Encoder(z_dim, opt.c_dim, opt.img_size) decoder = Decoder(z_dim, opt.c_dim, opt.img_size) anchor_orig = 1 num_class = 10 transNet = TransOpt() sampler_c = Sample_c() # Initialize weights encoder.apply(weights_init_normal) decoder.apply(weights_init_normal) ## Initialize dictionary Psi = Variable(torch.mul(torch.randn(N_use, M, dtype=torch.double), 0.01), requires_grad=True) # Load previously trained networks - replace checkpoint_folder with a checkpoint that is useful to you if load_checkpoint == 1: checkpoint_folder = './results/checkpoint_folder/' checkpoint = torch.load(checkpoint_folder + 'network_batch32_zdim10_step31000.pt')
os.makedirs(save_folder) if data_use == 'rotDigits': sample_dir = './' + opt.model + opt.data_use + '_batch' + str( opt.batch_size) + '_zdim' + str( opt.z_dim) + '_zeta' + str(zeta) + '_gamma' + str( opt.gamma) + '_scale' + str(scale) + '_M' + str( M) + '_' + opt.training_phase + '_samples/' if not os.path.exists(sample_dir): os.makedirs(sample_dir) # Define the loss functions mse_loss = torch.nn.MSELoss(reduction='mean') latent_mse_loss = torch.nn.MSELoss(reduction='sum') transNet = TransOpt() # Load data nTrain = 4000 nTest = 500 noise_std = 0.01 if data_use == 'concen_circle': from fullyConnectedModel import Encoder from fullyConnectedModel import Decoder mapMat = np.random.uniform(-1, 1, (x_dim, z_dim)) if opt.training_phase == 'AE_train': sio.savemat( save_folder + 'mapMat_circleHighDim_z' + str(z_dim) + '_x' + str(x_dim) + '.mat', {'mapMat': mapMat}) else:
sample_labels = data_y sample_X_torch = torch.from_numpy(data_X) sample_X_torch = sample_X_torch.permute(0,3,1,2) sample_X_torch = sample_X_torch.float() batch_idxs = len(data_X) // opt.batch_size anchor_X,anchor_y = select_mnist_anchors(data_X,data_y,opt.num_anchor) anchor_X_torch = torch.from_numpy(anchor_X) anchor_X_torch= anchor_X_torch.permute(0,3,1,2) anchor_X_torch = anchor_X_torch.float() encoder = Encoder(z_dim,opt.c_dim,opt.img_size) decoder = Decoder(z_dim,opt.c_dim,opt.img_size) anchor_orig = 1 num_class = 10 transNet = TransOpt() sampler_c = Sample_c() # Initialize weights encoder.apply(weights_init_normal) decoder.apply(weights_init_normal) ## Initialize dictionary Psi = Variable(torch.mul(torch.randn(N_use, M, dtype=torch.double),0.01), requires_grad=True) # Load previously trained networks - replace checkpoint_folder with a checkpoint that is useful to you if load_checkpoint == 1: checkpoint_folder = './results/checkpoint_folder/' checkpoint = torch.load(checkpoint_folder + 'network_batch32_zdim10_step31000.pt') encoder.load_state_dict(checkpoint['model_state_dict_encoder'])