class mean_std_plot(object): def __init__(self,device,scale_size,exp,noise): self.device= device self.scale = scale_size self.prediction = Prediction(self.scale,self.device) self.exp = exp self.noise = noise def compute_mean_std(self,pos,truth,save_dir): model = self.prediction.model(self.exp) num = pos.shape[1] pos_samples = np.full([num,self.scale*self.scale],0.0) permeability_samples = np.full([num,self.scale,self.scale],0.0) if self.exp =='inversion_16' or self.exp =='inversion_32' or self.exp =='inversion_64': zc = pos for i in range(num): pos_samples[i,:] = self.prediction.permeability(self.exp,model,zc[:,i],zf=None).cpu().numpy().reshape(self.scale*self.scale,) permeability_samples[i,:,:]=pos_samples[i,:].reshape(self.scale,self.scale) if self.exp =='inversion_16_64': zc = pos[:16,:] zf = pos[16:,:] for i in range(num): pos_samples[i,:] = self.prediction.permeability(self.exp,model,zc[:,i],zf=zf[:,i]).cpu().numpy().reshape(self.scale*self.scale,) permeability_samples[i,:,:]=pos_samples[i,:].reshape(self.scale,self.scale) mean = np.mean(permeability_samples,axis = 0) std = np.std(permeability_samples,axis = 0) sample1 = permeability_samples[0,:,:].reshape(self.scale,self.scale) sample2 = permeability_samples[1000,:,:].reshape(self.scale,self.scale) sample3 = permeability_samples[1999,:,:].reshape(self.scale,self.scale) #np.savetxt(save_dir+f'/pos_samples.dat',pos_samples) np.savetxt(save_dir+f'/mean.dat',mean) np.savetxt(save_dir+f'/std.dat',std) self.plot(truth,mean,std,sample1,sample2,sample3,save_dir) return pos_samples def plot(self,truth,mean,std,sample1,sample2,sample3,save_dir): samples = [np.log(truth),mean,std,sample1,sample2,sample3] fig, _ = plt.subplots(2,3, figsize=(9, 6)) vmin1 = [np.amin(samples[0]), np.amin(samples[0]),np.amin(samples[2]),np.amin(samples[0]),np.amin(samples[0]),np.amin(samples[0])] vmax1 = [np.amax(samples[0]), np.amax(samples[0]),np.amax(samples[2]),np.amax(samples[0]),np.amax(samples[0]),np.amax(samples[0])] for j, ax in enumerate(fig.axes): ax.set_aspect('equal') ax.set_axis_off() cax = ax.imshow(samples[j], cmap='jet', origin='upper',vmin=vmin1[j],vmax=vmax1[j]) cbar = plt.colorbar(cax, ax=ax, fraction=0.046, pad=0.04, format=ticker.ScalarFormatter(useMathText=True)) if j == 0: ax.set_title('Reference',fontsize=12) if j == 1: ax.set_title('Mean',fontsize=12) if j == 2: ax.set_title('Standard deviation',fontsize=12) if j == 3: ax.set_title('Posterior sample 1',fontsize=12) if j == 4: ax.set_title('Posterior sample 2',fontsize=12) if j == 5: ax.set_title('Posterior sample 3',fontsize=12) plt.savefig(save_dir+f'/gaussian_statistics_{self.exp}.pdf',bbox_inches = 'tight', dpi=1000,pad_inches=0.0) plt.close()
def __init__(self): device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu") # training on GPU or CPU self.prediction = Prediction('scale_64',device) self.simulation = simulation() self.true_permeability = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/true_permeability_0.05.dat').reshape(64,64) self.obs = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/obs_0.05.dat') self.sigma = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/sigma_0.05.dat') self.ref_fx,_,_,_,_,_ = self.simulation.demo64(self.true_permeability) self.ref_sswr = np.sum(np.power((self.obs-self.ref_fx)/self.sigma,2.0) ) self.ref_rss = np.sum(np.power((self.obs-self.ref_fx),2.0) )
def __init__(self, device): self.device = device self.prediction = Prediction(64, self.device) dir = os.getcwd() model_dir16 = dir + f'/Gaussian/generative_model/encoder_16_VAE_0.5_epoch30.pth' model16 = Encoder() model16.load_state_dict( torch.load(model_dir16, map_location=self.device), False) self.encoder_model16 = model16.to(self.device) self.encoder_model16.eval()
def __init__(self,step,burn_in,dim,obs,sigma,true_permeability,true_pressure,obs_position,scale,device): self.dim = dim self.burn_in = burn_in self.step = step self.obs = obs self.sigma = sigma self.true_permeability = true_permeability self.true_pressure = true_pressure self.pos = obs_position self.plot_interval = 1000 self.num_obs = 64 self.prediction = Prediction(scale,device)
def __init__(self, device): self.device = device self.prediction = Prediction(64, self.device) dir = os.getcwd() model_dir16 = dir + f'/Channel/generative_model/encoder_16_VAE_1_epoch30.pth' model16 = Encoder() model16.load_state_dict( torch.load(model_dir16, map_location=self.device), False) self.encoder_model16 = model16.to(self.device) self.encoder_model16.eval() dir = os.getcwd() model_dir_16_32 = dir + f'/Channel/generative_model/encoder_16_32_VAE_1_0.7_epoch50.pth' model_16_32 = Encoder() model_16_32.load_state_dict( torch.load(model_dir_16_32, map_location=self.device), False) self.encoder_model_16_32 = model_16_32.to(self.device) self.encoder_model_16_32.eval()
class latent_c(object): def __init__(self, device): self.device = device self.prediction = Prediction(64, self.device) dir = os.getcwd() model_dir16 = dir + f'/Gaussian/generative_model/encoder_16_VAE_0.5_epoch30.pth' model16 = Encoder() model16.load_state_dict( torch.load(model_dir16, map_location=self.device), False) self.encoder_model16 = model16.to(self.device) self.encoder_model16.eval() def coarse16(self, x16): with torch.no_grad(): x16 = x16.reshape(1, 1, 16, 16) z, mu, logvar = self.encoder_model16(x16) return z, mu, logvar def upsampling(self, img, factor): (x, y) = img.shape x_new = int(np.ceil(x / factor)) y_new = int(np.ceil(y / factor)) img_new = np.full((x_new, y_new), 0.0) img = np.exp(img) for i in range(x_new): for j in range(y_new): if i == x_new and j < y_new: mesh = img[i * factor:x, j * factor:(j + 1) * factor] elif i < x_new and j == y_new: mesh = img[i * factor:(i + 1) * factor, j * factor:y] elif i == x_new and j == y_new: mesh = img[i * factor:x, j * factor:y] else: mesh = img[i * factor:(i + 1) * factor, j * factor:(j + 1) * factor] img_new[i, j] = np.mean(mesh) img_new = np.log(img_new) return img_new def coarse_latent(self, exp, coarse_mean): if exp == 'inversion_16_64': model = self.prediction.model('inversion_16') latent = np.full((1, 16), 0.0) _, mu16, _ = self.coarse16( torch.FloatTensor(coarse_mean).reshape(1, 1, 16, 16).to(self.device)) latent = mu16.data.cpu().numpy().reshape(16, ) return latent
format=ticker.ScalarFormatter(useMathText=True)) output_dir = os.getcwd() + f'/Gaussian/MDGM_plot' if not os.path.exists(output_dir): os.makedirs(output_dir) plt.savefig(output_dir + filename, bbox_inches='tight', dpi=1000, pad_inches=0.0) plt.close() if __name__ == "__main__": torch.random.manual_seed(10) device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu") latent_c = latent_c(device) prediction = Prediction('scale_64', device) nz_x, nz_y = 4, 4 z = torch.randn(4, 1, nz_x, nz_y) model = prediction.model('inversion_16') gen_imgs = prediction.permeability('inversion_16', model, z) samples = np.squeeze(gen_imgs.data.cpu().numpy()) condition = np.loadtxt(os.getcwd() + f'/Gaussian/MDGM_plot/gaussian_16.dat') zc = latent_c.coarse_latent('inversion_16_64', condition).reshape(16, ) zc = (np.ones([3, 1, 4, 4])) * zc.reshape(4, 4) z64 = torch.randn(3, 1, 16, 16) model = prediction.model('inversion_16_64') gen_imgs1 = prediction.permeability('inversion_16_64', model, zc, zf=z64) samples1 = np.squeeze(gen_imgs1.data.cpu().numpy()) samples2 = [ samples[0], samples[1], samples[2], samples[3], condition, samples1[0],
class Plot_convergence(object): def __init__(self): device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu") # training on GPU or CPU self.prediction = Prediction('scale_64',device) self.simulation = simulation() self.true_permeability = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/true_permeability_0.05.dat').reshape(64,64) self.obs = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/obs_0.05.dat') self.sigma = np.loadtxt(os.getcwd()+f'/Gaussian/test_data/sigma_0.05.dat') self.ref_fx,_,_,_,_,_ = self.simulation.demo64(self.true_permeability) self.ref_sswr = np.sum(np.power((self.obs-self.ref_fx)/self.sigma,2.0) ) self.ref_rss = np.sum(np.power((self.obs-self.ref_fx),2.0) ) def compute_para_rss(self,z, exp): num = z.shape[1] permeability_samples = np.full((num,4096),0.0) para_rss = np.full((num,),0.0) if exp=='inversion_64': model = self.prediction.model(exp) for i in range(num): permeability_samples[i,:] = self.prediction.permeability(exp,model,z[:,i]).cpu().numpy().reshape(4096,) para_rss[i,] = np.sum(np.power((permeability_samples[i,:]-np.log(self.true_permeability.reshape(4096,))),2)) if exp=='inversion_16_64': model = self.prediction.model(exp) for i in range(num): zc = z[:16,i] zf = z[16:,i] permeability_samples[i,:] = self.prediction.permeability(exp,model,zc,zf =zf).cpu().numpy().reshape(4096,) para_rss[i,] = np.sum(np.power((permeability_samples[i,:]-np.log(self.true_permeability.reshape(4096,))),2)) return para_rss def plot_para_rss(self,para_rss_64,para_rss_16_64): fig,ax =plt.subplots() # ax.set_yscale('log') x_axix=np.linspace(0,10000,10000) x_axix1=np.linspace(0,5000,5000) plt.ylabel('RSS of parameter field') plt.xlabel('Iterations') plt.plot(x_axix, para_rss_64[0:], color='green', label='reference') plt.plot(x_axix1, para_rss_16_64[0:], color='blue', label='16-64') plt.legend() plt.savefig(os.getcwd()+ f'/Gaussian/convergence/images/gaussian_para_rss_convergence.pdf',bbox_inches = 'tight', dpi=1000,pad_inches=0.0) plt.close() def para_rss_convergence(self): z_64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_64/pCN_step0.08_0.05_10000_8000_all_samples.dat') z_16_64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_16_64/pCN_step0.08_0.05_5000_3000_all_samples.dat') para_rss_64 = self.compute_para_rss(z_64 , 'inversion_64') para_rss_16_64 = self.compute_para_rss(z_16_64, 'inversion_16_64') self.plot_para_rss(para_rss_64,para_rss_16_64) def Comp_log_likelihood(self,fx): e=self.obs-fx rss = np.sum(np.power(e,2.0) ) sswr = np.sum(np.power(e/self.sigma,2.0) ) nsswr = sswr/self.ref_sswr nrss = rss/self.ref_rss return rss,nrss, sswr,nsswr def plot_nsswr(self,nsswr64,nsswr16_64): fig,ax =plt.subplots() ax.set_yscale('log') x_axix=np.linspace(0,10000,10000) x_axix1=np.linspace(0,5000,5000) plt.ylabel('NSSWR') plt.xlabel('Iterations') plt.plot(x_axix, nsswr64[0:], color='green', label='reference') plt.plot(x_axix1, nsswr16_64[0:], color='blue', label='16-64') plt.legend() plt.savefig(os.getcwd()+ f'/Gaussian/convergence/images/gaussian_nsswr_convergence.pdf',bbox_inches = 'tight', dpi=1000,pad_inches=0.0) plt.close() def plot_obs_rss(self,rss64,rss16_64): fig,ax =plt.subplots() ax.set_yscale('log') x_axix=np.linspace(0,10000,10000) x_axix1=np.linspace(0,5000,5000) plt.ylabel('RSS of observable pressure values') plt.xlabel('Iterations') plt.plot(x_axix, rss64[0:], color='green', label='reference') plt.plot(x_axix1, rss16_64[0:], color='blue', label='16-64') plt.legend() plt.savefig(os.getcwd()+ f'/Gaussian/convergence/images/gaussian_obsrss_convergence.pdf',bbox_inches = 'tight', dpi=1000,pad_inches=0.0) plt.close() def nsswr_convergence(self): fx64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_64/pCN_step0.08_0.05_10000_8000_fx_obs.dat') fx16_64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_16_64/pCN_step0.08_0.05_5000_3000_fx_obs.dat') rss64 = np.full((fx64.shape[1],1),0.0) nrss64 = np.full((fx64.shape[1],1),0.0) sswr64 = np.full((fx64.shape[1],1),0.0) nsswr64 = np.full((fx64.shape[1],1),0.0) nrss16_64 = np.full((fx16_64.shape[1],1),0.0) rss16_64 = np.full((fx16_64.shape[1],1),0.0) sswr16_64 = np.full((fx16_64.shape[1],1),0.0) nsswr16_64 = np.full((fx16_64.shape[1],1),0.0) for i in range(fx64.shape[1]): rss64[i,:],nrss64[i,:], sswr64[i,:],nsswr64[i,:]= self.Comp_log_likelihood(fx64[:,i]) for i in range(fx16_64.shape[1]): rss16_64[i,:],nrss16_64, sswr16_64[i,:],nsswr16_64[i,:]= self.Comp_log_likelihood(fx16_64[:,i]) self.plot_nsswr(nsswr64,nsswr16_64) self.plot_obs_rss(rss64,rss16_64) def plot_para_state(self): all_samples64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_64/pCN_step0.08_0.05_10000_8000_all_samples.dat') all_samples16_64 = np.loadtxt(os.getcwd()+f'/Gaussian/prediction/inversion_16_64/pCN_step0.08_0.05_5000_3000_all_samples.dat') truth = np.log(np.loadtxt(os.getcwd()+f'/Gaussian/test_data/true_permeability_0.05.dat')) samples = np.full((10,64,64),0.0) latent_sample64 = all_samples64[:,[0,999,2999,4999,9999]] latent_sample16_64 = all_samples16_64[:,[0,499,999,2999,4999]] for i in range(15): if i<5: exp = 'inversion_64' model = self.prediction.model(exp) samples[i,:,:] = self.prediction.permeability(exp,model,latent_sample64[:,i]).cpu().numpy().reshape(64,64) elif 4<i<10: exp = 'inversion_16_64' model = self.prediction.model(exp) zc = latent_sample16_64[:16,i-5] zf = latent_sample16_64[16:,i-5] samples[i,:,:] = self.prediction.permeability(exp,model,zc,zf=zf).cpu().numpy().reshape(64,64) fig, _ = plt.subplots(2,5, figsize=(15, 6)) vmin1 = np.amin(truth) vmax1 = np.amax(truth) for j, ax in enumerate(fig.axes): ax.set_aspect('equal') ax.set_axis_off() cax = ax.imshow(samples[j], cmap='jet', origin='upper',vmin=vmin1,vmax=vmax1) cbar = plt.colorbar(cax, ax=ax, fraction=0.046, pad=0.04, format=ticker.ScalarFormatter(useMathText=True)) if j == 0: ax.set_title('initial state',fontsize=12) if j == 1: ax.set_title('1000-th state',fontsize=12) if j == 2: ax.set_title('3000-th state',fontsize=12) if j == 3: ax.set_title('5000-th state',fontsize=12) if j == 4: ax.set_title('10000-th state',fontsize=12) if j == 5: ax.set_title('initial state',fontsize=12) if j == 6: ax.set_title('499-th state',fontsize=12) if j == 7: ax.set_title('1000-th state',fontsize=12) if j == 8: ax.set_title('3000-th state',fontsize=12) if j == 9: ax.set_title('5000-th state',fontsize=12) plt.savefig(os.getcwd()+f'/Gaussian/convergence/images/gaussian_para_state.pdf',bbox_inches = 'tight', dpi=1000,pad_inches=0.0) plt.close()
class inference(object): def __init__(self,step,burn_in,dim,obs,sigma,true_permeability,true_pressure,obs_position,scale,device): self.dim = dim self.burn_in = burn_in self.step = step self.obs = obs self.sigma = sigma self.true_permeability = true_permeability self.true_pressure = true_pressure self.pos = obs_position self.plot_interval = 1000 self.num_obs = 64 self.prediction = Prediction(scale,device) def pCN(self,old_params,beta,dim): ''' Here the prior distribution is standart Normal ''' new_params = np.sqrt(1-beta*beta)*old_params+beta*np.random.randn(dim,) return new_params def fx(self,model,exp,zc,zf): ''' compute the permeability field X using MDGM, and compute f(X) using forward model ''' input = self.prediction.permeability(exp,model,zc,zf=zf) like , pre = self.prediction.forward(input) f_x = np.reshape(like,(1,-1)) return f_x,input,pre def Comp_log_likelihood(self,obs,sigma,exp,model,zc,zf=None): ''' compute likelihood function for inference ''' f_x,input,pre = self.fx(model,exp,zc,zf) e=obs-f_x log_likelihood = -0.5 * np.sum( np.power(e/sigma,2.0)) print('log_like:',log_likelihood) return f_x,input,pre, log_likelihood def pCN_MH(self,init_c,init_state_f,beta,exp,noise,output_dir): ''' MH algorithm with MDGM, two proposal distributions for latent zc and zf respectively, where zc can capture global feature, zf is latent variable for parameter local adaption. proposal for zc with small step size(0.01) proposal for zf with big step size(first 50% state using 0.08, rest with 0.04) ''' coarse_beta = 0.01 model = self.prediction.model(exp) old_params_c = init_c dim_c=old_params_c.shape[0] accept_num = 0 dim = dim_c+self.dim samples = np.zeros([dim,self.step]) fx_obs = np.zeros([self.num_obs,self.step]) log_likelihood = np.zeros([1,self.step]) old_params_f = init_state_f old_fx,old_input,old_pre,old_log_l= self.Comp_log_likelihood(self.obs,self.sigma,exp,model,old_params_c,zf = old_params_f) old_params = np.concatenate((old_params_c,old_params_f),axis=0) samples[:,0] = old_params.reshape([dim,]) fx_obs[:,0] = old_fx log_likelihood[:,0] = old_log_l self.plot_para_field(0,old_input,old_pre,self.true_permeability, self.true_pressure,beta,'pCN',exp,noise,output_dir) for i in tqdm(range(1,self.step)): if i > 0.5*self.step: beta = 0.04 new_params_c = self.pCN(old_params_c,coarse_beta,dim_c) new_params_f = self.pCN(old_params_f,beta,self.dim) new_fx,new_input,new_pre,new_log_l= self.Comp_log_likelihood(self.obs,self.sigma,exp,model,new_params_c,zf = new_params_f) new_params = np.concatenate((new_params_c,new_params_f),axis=0) ratio = np.exp(new_log_l - old_log_l) alpha = min(1,ratio) np.random.seed(i) z = np.random.rand() if z<= alpha: print('-----------------------------------------------------------------') log_likelihood[:,i] = new_log_l fx_obs[:,i] = new_fx old_fx= new_fx samples[:,i] = new_params old_input = new_input old_pre = new_pre old_params = new_params old_params_c = new_params_c old_params_f = new_params_f old_log_l = new_log_l accept_num = accept_num +1 elif z>alpha: samples[:,i] = old_params fx_obs[:,i] = old_fx log_likelihood[:,i] = old_log_l if (i+1)%self.plot_interval == 0: self.plot_para_field(i,old_input,old_pre,self.true_permeability, self.true_pressure,beta,'pCN',exp,noise,output_dir) post_samples = samples[:,self.burn_in:] accept_ratio = (accept_num/self.step) print('accept_ratio:',accept_ratio) accept = [accept_ratio,accept_num] return samples, post_samples,fx_obs,accept,log_likelihood def plot_para_field(self,i,input,pre_pressure, true_permeability, true_pressure,beta,type,exp,noise,output_dir): ''' Plot Markov chain state(log permeability) ''' samples = [np.log(true_permeability),input.data.cpu().numpy(), true_pressure.reshape(64,64),pre_pressure] fig, _ = plt.subplots(2,2, figsize=(6,6)) vmin1 = [np.amin(samples[0]), np.amin(samples[0]),np.amin(samples[2]), np.amin(samples[2])] vmax1 = [np.amax(samples[0]), np.amax(samples[0]),np.amax(samples[2]), np.amax(samples[2])] for j, ax in enumerate(fig.axes): ax.set_aspect('equal') ax.set_axis_off() cax = ax.imshow(samples[j], cmap='jet',vmin=vmin1[j],vmax=vmax1[j]) cbar = plt.colorbar(cax, ax=ax, fraction=0.046, pad=0.04, format=ticker.ScalarFormatter(useMathText=True)) plt.savefig(output_dir+f'/{type}_step_size{beta}_state_{i+1}.pdf') plt.close()
class inference(object): def __init__(self,step,burn_in,dim,obs,sigma,true_permeability,true_pressure,obs_position,scale,device): self.dim = dim self.burn_in = burn_in self.step = step self.obs = obs self.sigma = sigma self.true_permeability = true_permeability self.true_pressure = true_pressure self.pos = obs_position self.Prior_type = 'Standard Normal' self.plot_interval = 1000 self.num_obs = 64 self.prediction = Prediction(scale,device) def pCN(self,old_params,beta): ''' Here the prior distribution is standart Normal ''' new_params = np.sqrt(1-beta*beta)*old_params+beta*np.random.randn(self.dim,) return new_params def fx(self,model,exp,z): ''' compute the permeability field X using MDGM, and compute f(X) using forward model ''' input = self.prediction.permeability(exp,model,z) like , pre = self.prediction.forward(input) fx = np.reshape(like,(1,-1)) return fx def Comp_log_likelihood(self,z,obs,sigma,exp,model): ''' compute likelihood function for inference ''' fx = self.fx(model,exp,z) e=obs-fx log_likelihood = -0.5 * np.sum( np.power(e/sigma,2.0)) print('log_like:',log_likelihood) return fx, log_likelihood def pCN_MH(self,init_state,beta,exp,noise,output_dir): ''' MH algorithm with DGM on the first scale(the coarsest scale) proposal for latent variable z1 with big step size to explore the whole space (first 50% state using 0.08, rest with 0.04) ''' model = self.prediction.model(exp) accept_num = 0 old_params = init_state samples = np.zeros([self.dim,self.step]) log_likelihood = np.zeros([1,self.step]) fx_obs = np.zeros([self.num_obs,self.step]) old_fx ,old_log_l= self.Comp_log_likelihood(old_params,self.obs,self.sigma,exp,model) fx_obs[:,0] = old_fx log_likelihood[:,0] = old_log_l samples [:,0] = old_params.reshape([self.dim,]) self.plot_para_field(0,old_params,self.true_permeability, self.true_pressure,beta,'pCN',exp,model,noise,output_dir) for i in tqdm(range(1,self.step)): if i > 0.5*self.step: beta = 0.04 new_params = self.pCN(old_params,beta) new_fx ,new_log_l = self.Comp_log_likelihood(new_params,self.obs,self.sigma,exp,model) ratio = np.exp(new_log_l - old_log_l) alpha = min(1,ratio) np.random.seed(i) z = np.random.rand() if z<= alpha: print('-----------------------------------------------------------------') old_params = new_params.reshape([self.dim,]) fx_obs[:,i] = new_fx log_likelihood[:,i] = new_log_l samples[:,i] = new_params.reshape([self.dim,]) old_log_l = new_log_l old_fx = new_fx accept_num = accept_num +1 elif z>alpha: old_params = old_params samples[:,i] = old_params.reshape([self.dim,]) fx_obs[:,i] = old_fx log_likelihood[:,i] = old_log_l if (i+1)%self.plot_interval == 0: self.plot_para_field(i,old_params,self.true_permeability, self.true_pressure,beta,'pCN',exp,model,noise,output_dir) post_samples = samples[:,self.burn_in:] accept_ratio = accept_num/self.step print('accept_ratio:',accept_ratio) accept = [accept_ratio,accept_num] return samples, post_samples,fx_obs,accept,log_likelihood def plot_para_field(self,i,params, true_permeability, true_pressure,beta,type,exp,model,noise,output_dir): ''' Plot Markov chain state(log permeability) ''' pre_permeability = self.prediction.permeability(exp,model,params) _, pre_pressure = self.prediction.forward(pre_permeability) if exp=='inversion_16': pre_permeability = pre_permeability.data.cpu().numpy().reshape(16,16) if exp=='inversion_64': pre_permeability = pre_permeability.data.cpu().numpy().reshape(64,64) samples = [np.log(true_permeability),pre_permeability, true_pressure,pre_pressure] fig, _ = plt.subplots(2,2, figsize=(6,6)) vmin1 = [np.amin(samples[0]), np.amin(samples[0]),np.amin(samples[2]),np.amin(samples[2])] vmax1 = [np.amax(samples[0]), np.amax(samples[0]),np.amax(samples[2]),np.amax(samples[2])] for j, ax in enumerate(fig.axes): ax.set_aspect('equal') ax.set_axis_off() cax = ax.imshow(samples[j], cmap='jet',vmin=vmin1[j],vmax=vmax1[j]) cbar = plt.colorbar(cax, ax=ax, fraction=0.046, pad=0.04, format=ticker.ScalarFormatter(useMathText=True)) plt.savefig(output_dir+f'/{type}_step_size{beta}_state_{i+1}.pdf') plt.close()
def __init__(self, device, scale_size, exp, noise): self.device = device self.scale = scale_size self.prediction = Prediction(self.scale, self.device) self.exp = exp self.noise = noise