本文整理汇总了Python中utils.visualize_samples函数的典型用法代码示例。如果您正苦于以下问题:Python visualize_samples函数的具体用法?Python visualize_samples怎么用?Python visualize_samples使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了visualize_samples函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: visualize_attention
def visualize_attention(result, pre_tag="AAA", post_tag="AAA"):
seq_len = result[0].shape[0]
samp_count = result[0].shape[1]
# get generated predictions
x_samps = np.zeros((seq_len*samp_count, obs_dim))
y_samps = np.zeros((seq_len*samp_count, label_dim))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
x_samps[idx] = result[0][s2,s1,:obs_dim]
y_samps[idx] = result[0][s2,s1,obs_dim:]
idx += 1
file_name = "{0:s}_traj_xs_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(x_samps, file_name, num_rows=20)
file_name = "{0:s}_traj_ys_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(y_samps, file_name, num_rows=20)
# get sequential attention maps
seq_samps = np.zeros((seq_len*samp_count, x_dim))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = result[1][s2,s1,:x_dim] + result[1][s2,s1,x_dim:]
idx += 1
file_name = "{0:s}_traj_att_maps_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=20)
# get sequential attention maps (read out values)
seq_samps = np.zeros((seq_len*samp_count, x_dim))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = result[2][s2,s1,:x_dim] + result[2][s2,s1,x_dim:]
idx += 1
file_name = "{0:s}_traj_read_outs_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=20)
return
开发者ID:capybaralet,项目名称:Sequential-Generation,代码行数:35,代码来源:TestRAMBlocks.py
示例2: test_mnist_img
def test_mnist_img(occ_dim=15, drop_prob=0.0):
#########################################
# Format the result tag more thoroughly #
#########################################
dp_int = int(100.0 * drop_prob)
result_tag = RESULT_PATH + "TM_OD{}_DP{}".format(occ_dim, dp_int)
##########################
# Get some training data #
##########################
rng = np.random.RandomState(1234)
dataset = 'data/mnist.pkl.gz'
datasets = load_udm(dataset, as_shared=False, zero_mean=False)
Xtr = datasets[0][0]
Xva = datasets[1][0]
Xtr = to_fX(shift_and_scale_into_01(Xtr))
Xva = to_fX(shift_and_scale_into_01(Xva))
tr_samples = Xtr.shape[0]
va_samples = Xva.shape[0]
batch_size = 200
batch_reps = 1
all_pix_mean = np.mean(np.mean(Xtr, axis=1))
data_mean = to_fX(all_pix_mean * np.ones((Xtr.shape[1],)))
TM = TemplateMatchImputer(x_train=Xtr, x_type='bernoulli')
Xva = row_shuffle(Xva)
# record an estimate of performance on the test set
xi, xo, xm = construct_masked_data(Xva[:500], drop_prob=drop_prob, \
occ_dim=occ_dim, data_mean=data_mean)
img_match_on_known, img_match_on_unknown = TM.best_match_img(xo, xm)
display_count = 100
# visualize matches on known elements
Xs = np.zeros((2*display_count, Xva.shape[1]))
for idx in range(display_count):
Xs[2*idx] = xi[idx]
Xs[(2*idx)+1] = img_match_on_known[idx]
file_name = "{0:s}_SAMPLES_MOK.png".format(result_tag)
utils.visualize_samples(Xs, file_name, num_rows=20)
# visualize matches on unknown elements
Xs = np.zeros((2*display_count, Xva.shape[1]))
for idx in range(display_count):
Xs[2*idx] = xi[idx]
Xs[(2*idx)+1] = img_match_on_unknown[idx]
file_name = "{0:s}_SAMPLES_MOU.png".format(result_tag)
utils.visualize_samples(Xs, file_name, num_rows=20)
return
开发者ID:AjayTalati,项目名称:Sequential-Generation,代码行数:48,代码来源:TestImpTM.py
示例3: process_samples
def process_samples(step_type='add', data_name='MNIST'):
# sample several interchangeable versions of the model
if data_name == 'MNIST':
conditions = [{'occ_dim': 0, 'drop_prob': 0.8}, \
{'occ_dim': 16, 'drop_prob': 0.0}]
if data_name == 'SVHN':
conditions = [{'occ_dim': 0, 'drop_prob': 0.8}, \
{'occ_dim': 17, 'drop_prob': 0.0}]
if data_name == 'TFD':
conditions = [{'occ_dim': 0, 'drop_prob': 0.8}, \
{'occ_dim': 25, 'drop_prob': 0.0}]
for cond_dict in conditions:
occ_dim = cond_dict['occ_dim']
drop_prob = cond_dict['drop_prob']
dp_int = int(100.0 * drop_prob)
# save the samples to a pkl file, in their numpy array form
sample_pkl_name = "IMP-{}-OD{}-DP{}-{}.pkl".format(data_name, occ_dim, dp_int, step_type)
pickle_file = open(sample_pkl_name)
samples = cPickle.load(pickle_file)
pickle_file.close()
print("Loaded some samples from: {}".format(sample_pkl_name))
sample_list = []
for i in range(samples.shape[0]):
sample_list.append(samples[i,:,:])
# downsample the sequence....
#keep_idx = range(len(sample_list))
keep_idx = [0, 2, 4, 6, 9, 12, 15]
sample_list = [sample_list[i] for i in keep_idx]
seq_len = len(sample_list)
samp_count = sample_list[0].shape[0]
obs_dim = sample_list[0].shape[1]
seq_samps = np.zeros((seq_len*samp_count, obs_dim))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = sample_list[s2][s1,:].ravel()
idx += 1
sample_img_name = "IMP-{}-OD{}-DP{}-{}.png".format(data_name, occ_dim, dp_int, step_type)
row_count = int(samp_count / 16)
print("row_count: {}".format(row_count))
utils.visualize_samples(seq_samps, sample_img_name, num_rows=row_count)
return
开发者ID:AjayTalati,项目名称:Sequential-Generation,代码行数:47,代码来源:TestBlocksImpSamplesProcessing.py
示例4: visualize_attention_joint
def visualize_attention_joint(result, pre_tag="AAA", post_tag="AAA"):
seq_len = result[0].shape[0]
samp_count = result[0].shape[1]
# get generated predictions
seq_samps = np.zeros((3*seq_len*samp_count, obs_dim))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = result[3][s2,s1,:]
idx += 1
for s2 in range(seq_len):
seq_samps[idx] = result[0][s2,s1,:]
idx += 1
for s2 in range(seq_len):
seq_samps[idx] = result[1][s2,s1,:]
idx += 1
file_name = "{0:s}_traj_joint_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=(3*samp_count))
return
开发者ID:Philip-Bachman,项目名称:Sequential-Generation,代码行数:19,代码来源:TestRAMVideoFT.py
示例5: visualize_attention
def visualize_attention(sampler_result, pre_tag="AAA", post_tag="AAA"):
# get generated predictions
seq_len = sampler_result[0].shape[0]
samp_count = sampler_result[0].shape[1]
x_dim = sampler_result[0].shape[2]
seq_samps = np.zeros((samp_count, 28*28))
for samp in range(samp_count):
step = 0
samp_vals = np.zeros((28,28))
for col in range(28):
col_vals = np.zeros((28,))
for rep in range(step_reps):
if (rep == (step_reps-1)):
col_vals = sampler_result[0][step,samp,:]
step += 1
samp_vals[:,col] = col_vals
seq_samps[samp,:] = samp_vals.ravel()
file_name = "{0:s}_traj_xs_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=10)
# get sequential attention maps
seq_samps = np.zeros((samp_count, 28*28))
for samp in range(samp_count):
step = 0
samp_vals = np.zeros((28,28))
for col in range(28):
col_vals = np.zeros((28,))
for rep in range(step_reps):
col_vals = col_vals + sampler_result[1][step,samp,:x_dim]
col_vals = col_vals + sampler_result[1][step,samp,x_dim:]
step += 1
samp_vals[:,col] = col_vals / (2.0*step_reps)
seq_samps[samp,:] = samp_vals.ravel()
file_name = "{0:s}_traj_att_maps_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=10)
# get sequential attention maps (read out values)
seq_samps = np.zeros((samp_count, 28*28))
for samp in range(samp_count):
step = 0
samp_vals = np.zeros((28,28))
for col in range(28):
col_vals = np.zeros((28,))
for rep in range(step_reps):
col_vals = col_vals + sampler_result[2][step,samp,:x_dim]
col_vals = col_vals + sampler_result[2][step,samp,x_dim:]
step += 1
samp_vals[:,col] = col_vals / (2.0*step_reps)
seq_samps[samp,:] = samp_vals.ravel()
file_name = "{0:s}_traj_read_outs_{1:s}.png".format(pre_tag, post_tag)
utils.visualize_samples(seq_samps, file_name, num_rows=10)
return
开发者ID:Philip-Bachman,项目名称:Sequential-Generation,代码行数:50,代码来源:TestRAMBlocks.py
示例6: train_walk_from_pretrained_osm
#.........这里部分代码省略.........
dn_params['spawn_configs'] = [sc0]
dn_params['spawn_weights'] = [1.0]
# Set remaining params
dn_params['init_scale'] = 1.0
dn_params['lam_l2a'] = 1e-2
dn_params['vis_drop'] = 0.2
dn_params['hid_drop'] = 0.5
# Initialize a network object to use as the discriminator
DN = PeaNet(rng=rng, Xd=Xd, params=dn_params)
DN.init_biases(0.0)
#######################################################
# Load inferencer and generator from saved parameters #
#######################################################
gn_fname = RESULT_PATH+"pt_osm_params_b100000_GN.pkl"
in_fname = RESULT_PATH+"pt_osm_params_b100000_IN.pkl"
IN = load_infnet_from_file(f_name=in_fname, rng=rng, Xd=Xd)
GN = load_infnet_from_file(f_name=gn_fname, rng=rng, Xd=Xd)
########################################################
# Define parameters for the VCGLoop, and initialize it #
########################################################
print("Building the VCGLoop...")
vcgl_params = {}
vcgl_params['x_type'] = 'gaussian'
vcgl_params['xt_transform'] = 'sigmoid'
vcgl_params['logvar_bound'] = LOGVAR_BOUND
vcgl_params['cost_decay'] = 0.1
vcgl_params['chain_type'] = 'walkout'
vcgl_params['lam_l2d'] = 5e-2
VCGL = VCGLoop(rng=rng, Xd=Xd, Xc=Xc, Xm=Xm, Xt=Xt, \
i_net=IN, g_net=GN, d_net=DN, chain_len=5, \
data_dim=data_dim, prior_dim=PRIOR_DIM, params=vcgl_params)
out_file = open(RESULT_PATH+"pt_walk_results.txt", 'wb')
####################################################
# Train the VCGLoop by unrolling and applying BPTT #
####################################################
learn_rate = 0.0005
cost_1 = [0. for i in range(10)]
for i in range(100000):
scale = float(min((i+1), 5000)) / 5000.0
if ((i+1 % 25000) == 0):
learn_rate = learn_rate * 0.8
########################################
# TRAIN THE CHAIN IN FREE-RUNNING MODE #
########################################
VCGL.set_all_sgd_params(learn_rate=(scale*learn_rate), \
mom_1=0.9, mom_2=0.99)
VCGL.set_disc_weights(dweight_gn=25.0, dweight_dn=25.0)
VCGL.set_lam_chain_nll(1.0)
VCGL.set_lam_chain_kld(lam_kld)
# get some data to train with
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_size,))
Xd_batch = Xtr.take(tr_idx, axis=0)
Xc_batch = 0.0 * Xd_batch
Xm_batch = 0.0 * Xd_batch
# examples from the target distribution, to train discriminator
tr_idx = npr.randint(low=0,high=tr_samples,size=(2*batch_size,))
Xt_batch = Xtr.take(tr_idx, axis=0)
# do a minibatch update of the model, and compute some costs
outputs = VCGL.train_joint(Xd_batch, Xc_batch, Xm_batch, Xt_batch, batch_reps)
cost_1 = [(cost_1[k] + 1.*outputs[k]) for k in range(len(outputs))]
if ((i % 500) == 0):
cost_1 = [(v / 500.0) for v in cost_1]
o_str_1 = "batch: {0:d}, joint_cost: {1:.4f}, chain_nll_cost: {2:.4f}, chain_kld_cost: {3:.4f}, disc_cost_gn: {4:.4f}, disc_cost_dn: {5:.4f}".format( \
i, cost_1[0], cost_1[1], cost_1[2], cost_1[5], cost_1[6])
print(o_str_1)
cost_1 = [0. for v in cost_1]
if ((i % 1000) == 0):
tr_idx = npr.randint(low=0,high=Xtr.shape[0],size=(5,))
va_idx = npr.randint(low=0,high=Xva.shape[0],size=(5,))
Xd_batch = np.vstack([Xtr.take(tr_idx, axis=0), Xva.take(va_idx, axis=0)])
# draw some chains of samples from the VAE loop
file_name = RESULT_PATH+"pt_walk_chain_samples_b{0:d}.png".format(i)
Xd_samps = np.repeat(Xd_batch, 3, axis=0)
sample_lists = VCGL.OSM.sample_from_chain(Xd_samps, loop_iters=20)
Xs = np.vstack(sample_lists["data samples"])
utils.visualize_samples(Xs, file_name, num_rows=20)
# draw some masked chains of samples from the VAE loop
file_name = RESULT_PATH+"pt_walk_mask_samples_b{0:d}.png".format(i)
Xd_samps = np.repeat(Xc_mean[0:Xd_batch.shape[0],:], 3, axis=0)
Xc_samps = np.repeat(Xd_batch, 3, axis=0)
Xm_rand = sample_masks(Xc_samps, drop_prob=0.0)
Xm_patch = sample_patch_masks(Xc_samps, (48,48), (25,25))
Xm_samps = Xm_rand * Xm_patch
sample_lists = VCGL.OSM.sample_from_chain(Xd_samps, \
X_c=Xc_samps, X_m=Xm_samps, loop_iters=20)
Xs = np.vstack(sample_lists["data samples"])
utils.visualize_samples(Xs, file_name, num_rows=20)
# draw some samples independently from the GenNet's prior
file_name = RESULT_PATH+"pt_walk_prior_samples_b{0:d}.png".format(i)
Xs = VCGL.sample_from_prior(20*20)
utils.visualize_samples(Xs, file_name, num_rows=20)
# DUMP PARAMETERS FROM TIME-TO-TIME
if (i % 5000 == 0):
DN.save_to_file(f_name=RESULT_PATH+"pt_walk_params_b{0:d}_DN.pkl".format(i))
IN.save_to_file(f_name=RESULT_PATH+"pt_walk_params_b{0:d}_IN.pkl".format(i))
GN.save_to_file(f_name=RESULT_PATH+"pt_walk_params_b{0:d}_GN.pkl".format(i))
return
开发者ID:Philip-Bachman,项目名称:ICML-2015,代码行数:101,代码来源:TFDWalkoutTest.py
示例7: pretrain_osm
#.........这里部分代码省略.........
shared_config = [data_dim, 1000, 1000]
top_config = [shared_config[-1], PRIOR_DIM]
in_params['shared_config'] = shared_config
in_params['mu_config'] = top_config
in_params['sigma_config'] = top_config
in_params['activation'] = relu_actfun
in_params['init_scale'] = 1.4
in_params['lam_l2a'] = 0.0
in_params['vis_drop'] = 0.0
in_params['hid_drop'] = 0.0
in_params['bias_noise'] = 0.0
in_params['input_noise'] = 0.0
# Initialize the base networks for this OneStageModel
IN = InfNet(rng=rng, Xd=Xd, prior_sigma=prior_sigma, \
params=in_params, shared_param_dicts=None)
GN = InfNet(rng=rng, Xd=Xd, prior_sigma=prior_sigma, \
params=gn_params, shared_param_dicts=None)
# Initialize biases in IN and GN
IN.init_biases(0.2)
GN.init_biases(0.2)
#########################
# INITIALIZE THE GIPAIR #
#########################
osm_params = {}
osm_params['x_type'] = 'bernoulli'
osm_params['xt_transform'] = 'sigmoid'
osm_params['logvar_bound'] = LOGVAR_BOUND
OSM = OneStageModel(rng=rng, Xd=Xd, Xc=Xc, Xm=Xm, \
p_x_given_z=GN, q_z_given_x=IN, \
x_dim=data_dim, z_dim=PRIOR_DIM, params=osm_params)
OSM.set_lam_l2w(1e-5)
safe_mean = (0.9 * Xtr_mean) + 0.05
safe_mean_logit = np.log(safe_mean / (1.0 - safe_mean))
OSM.set_output_bias(safe_mean_logit)
OSM.set_input_bias(-Xtr_mean)
######################
# BASIC VAE TRAINING #
######################
out_file = open(RESULT_PATH+"pt_osm_results.txt", 'wb')
# Set initial learning rate and basic SGD hyper parameters
obs_costs = np.zeros((batch_size,))
costs = [0. for i in range(10)]
learn_rate = 0.0005
for i in range(150000):
scale = min(1.0, float(i) / 10000.0)
if ((i > 1) and ((i % 20000) == 0)):
learn_rate = learn_rate * 0.9
# do a minibatch update of the model, and compute some costs
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_size,))
Xd_batch = Xtr.take(tr_idx, axis=0)
Xc_batch = 0.0 * Xd_batch
Xm_batch = 0.0 * Xd_batch
# do a minibatch update of the model, and compute some costs
OSM.set_sgd_params(lr_1=(scale*learn_rate), mom_1=0.5, mom_2=0.98)
OSM.set_lam_nll(1.0)
OSM.set_lam_kld(lam_kld_1=(1.0 + (scale*(lam_kld-1.0))), lam_kld_2=0.0)
result = OSM.train_joint(Xd_batch, Xc_batch, Xm_batch, batch_reps)
costs = [(costs[j] + result[j]) for j in range(len(result))]
if ((i % 1000) == 0):
# record and then reset the cost trackers
costs = [(v / 1000.0) for v in costs]
str_1 = "-- batch {0:d} --".format(i)
str_2 = " joint_cost: {0:.4f}".format(costs[0])
str_3 = " nll_cost : {0:.4f}".format(costs[1])
str_4 = " kld_cost : {0:.4f}".format(costs[2])
str_5 = " reg_cost : {0:.4f}".format(costs[3])
costs = [0.0 for v in costs]
# print out some diagnostic information
joint_str = "\n".join([str_1, str_2, str_3, str_4, str_5])
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
if ((i % 2000) == 0):
Xva = row_shuffle(Xva)
model_samps = OSM.sample_from_prior(500)
file_name = RESULT_PATH+"pt_osm_samples_b{0:d}_XG.png".format(i)
utils.visualize_samples(model_samps, file_name, num_rows=20)
# compute information about free-energy on validation set
file_name = RESULT_PATH+"pt_osm_free_energy_b{0:d}.png".format(i)
fe_terms = OSM.compute_fe_terms(Xva[0:2500], 20)
fe_mean = np.mean(fe_terms[0]) + np.mean(fe_terms[1])
fe_str = " nll_bound : {0:.4f}".format(fe_mean)
print(fe_str)
out_file.write(fe_str+"\n")
utils.plot_scatter(fe_terms[1], fe_terms[0], file_name, \
x_label='Posterior KLd', y_label='Negative Log-likelihood')
# compute information about posterior KLds on validation set
file_name = RESULT_PATH+"pt_osm_post_klds_b{0:d}.png".format(i)
post_klds = OSM.compute_post_klds(Xva[0:2500])
post_dim_klds = np.mean(post_klds, axis=0)
utils.plot_stem(np.arange(post_dim_klds.shape[0]), post_dim_klds, \
file_name)
if ((i % 5000) == 0):
IN.save_to_file(f_name=RESULT_PATH+"pt_osm_params_b{0:d}_IN.pkl".format(i))
GN.save_to_file(f_name=RESULT_PATH+"pt_osm_params_b{0:d}_GN.pkl".format(i))
IN.save_to_file(f_name=RESULT_PATH+"pt_osm_params_IN.pkl")
GN.save_to_file(f_name=RESULT_PATH+"pt_osm_params_GN.pkl")
return
开发者ID:Philip-Bachman,项目名称:ICML-2015,代码行数:101,代码来源:MnistWalkoutTest.py
示例8: test_with_model_init
#.........这里部分代码省略.........
batch_idx += batch_size
if (np.max(batch_idx) >= tr_samples):
# we finished an "epoch", so we rejumble the training set
Xtr = row_shuffle(Xtr)
batch_idx = np.arange(batch_size)
# set sgd and objective function hyperparams for this update
MSM.set_sgd_params(lr_1=scale*learn_rate, lr_2=scale*learn_rate, \
mom_1=scale*momentum, mom_2=0.99)
MSM.set_train_switch(1.0)
MSM.set_lam_nll(lam_nll=1.0)
MSM.set_lam_kld(lam_kld_z=1.0, lam_kld_q2p=0.8, lam_kld_p2q=0.2)
MSM.set_lam_kld_l1l2(lam_kld_l1l2=1.0)
MSM.set_lam_l2w(1e-4)
MSM.set_drop_rate(0.0)
MSM.q_hi_given_x_si.set_bias_noise(0.0)
MSM.p_hi_given_si.set_bias_noise(0.0)
MSM.p_sip1_given_si_hi.set_bias_noise(0.0)
# perform a minibatch update and record the cost for this batch
Xb_tr = to_fX( Xtr.take(batch_idx, axis=0) )
result = MSM.train_joint(Xb_tr, Xb_tr, batch_reps)
costs = [(costs[j] + result[j]) for j in range(len(result)-1)]
if ((i % 500) == 0):
costs = [(v / 500.0) for v in costs]
str1 = "-- batch {0:d} --".format(i)
str2 = " joint_cost: {0:.4f}".format(costs[0])
str3 = " nll_cost : {0:.4f}".format(costs[1])
str4 = " kld_cost : {0:.4f}".format(costs[2])
str5 = " reg_cost : {0:.4f}".format(costs[3])
joint_str = "\n".join([str1, str2, str3, str4, str5])
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
costs = [0.0 for v in costs]
if (((i % 2000) == 0) or ((i < 10000) and ((i % 1000) == 0))):
MSM.set_drop_rate(0.0)
MSM.q_hi_given_x_si.set_bias_noise(0.0)
MSM.p_hi_given_si.set_bias_noise(0.0)
MSM.p_sip1_given_si_hi.set_bias_noise(0.0)
# Get some validation samples for computing diagnostics
Xva = row_shuffle(Xva)
Xb_va = to_fX( Xva[0:2000] )
# draw some independent random samples from the model
samp_count = 200
model_samps = MSM.sample_from_prior(samp_count)
seq_len = len(model_samps)
seq_samps = np.zeros((seq_len*samp_count, model_samps[0].shape[1]))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = model_samps[s2][s1]
idx += 1
file_name = "MSM_A_SAMPLES_IND_b{0:d}.png".format(i)
utils.visualize_samples(seq_samps, file_name, num_rows=20)
# draw some conditional random samples from the model
samp_count = 200
Xs = np.vstack((Xb_tr[0:(samp_count/4)], Xb_va[0:(samp_count/4)]))
Xs = np.repeat(Xs, 2, axis=0)
# draw some conditional random samples from the model
model_samps = MSM.sample_from_input(Xs, guided_decoding=False)
model_samps.append(Xs)
seq_len = len(model_samps)
seq_samps = np.zeros((seq_len*samp_count, model_samps[0].shape[1]))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = model_samps[s2][s1]
idx += 1
file_name = "MSM_A_SAMPLES_CND_b{0:d}.png".format(i)
utils.visualize_samples(seq_samps, file_name, num_rows=20)
# compute information about posterior KLds on validation set
raw_klds = MSM.compute_raw_klds(Xb_va, Xb_va)
init_kld, q2p_kld, p2q_kld = raw_klds
file_name = "MSM_A_H0_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(init_kld.shape[1]), \
np.mean(init_kld, axis=0), file_name)
file_name = "MSM_A_HI_Q2P_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(q2p_kld.shape[1]), \
np.mean(q2p_kld, axis=0), file_name)
file_name = "MSM_A_HI_P2Q_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(p2q_kld.shape[1]), \
np.mean(p2q_kld, axis=0), file_name)
Xb_tr = to_fX( Xtr[0:2000] )
fe_terms = MSM.compute_fe_terms(Xb_tr, Xb_tr, 30)
fe_nll = np.mean(fe_terms[0])
fe_kld = np.mean(fe_terms[1])
fe_joint = fe_nll + fe_kld
joint_str = " vfe-tr: {0:.4f}, nll: ({1:.4f}, {2:.4f}, {3:.4f}), kld: ({4:.4f}, {5:.4f}, {6:.4f})".format( \
fe_joint, fe_nll, np.min(fe_terms[0]), np.max(fe_terms[0]), fe_kld, np.min(fe_terms[1]), np.max(fe_terms[1]))
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
fe_terms = MSM.compute_fe_terms(Xb_va, Xb_va, 30)
fe_nll = np.mean(fe_terms[0])
fe_kld = np.mean(fe_terms[1])
fe_joint = fe_nll + fe_kld
joint_str = " vfe-va: {0:.4f}, nll: ({1:.4f}, {2:.4f}, {3:.4f}), kld: ({4:.4f}, {5:.4f}, {6:.4f})".format( \
fe_joint, fe_nll, np.min(fe_terms[0]), np.max(fe_terms[0]), fe_kld, np.min(fe_terms[1]), np.max(fe_terms[1]))
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
开发者ID:Philip-Bachman,项目名称:NN-Python,代码行数:101,代码来源:TestMSM.py
示例9: test_with_model_init
#.........这里部分代码省略.........
msm_params['x_type'] = x_type
msm_params['obs_transform'] = 'sigmoid'
MSM = MultiStageModel(rng=rng, x_in=X_sym, \
p_s0_obs_given_z_obs=p_s0_obs_given_z_obs, \
p_hi_given_si=p_hi_given_si, \
p_sip1_given_si_hi=p_sip1_given_si_hi, \
q_z_given_x=q_z_given_x, \
q_hi_given_x_si=q_hi_given_x_si, \
obs_dim=obs_dim, z_dim=z_dim, h_dim=h_dim, \
model_init_obs=True, ir_steps=2, \
params=msm_params)
obs_mean = (0.9 * np.mean(Xtr, axis=0)) + 0.05
obs_mean_logit = np.log(obs_mean / (1.0 - obs_mean))
MSM.set_input_bias(-obs_mean)
MSM.set_obs_bias(0.1*obs_mean_logit)
################################################################
# Apply some updates, to check that they aren't totally broken #
################################################################
costs = [0. for i in range(10)]
learn_rate = 0.0003
momentum = 0.8
for i in range(300000):
scale = min(1.0, ((i+1) / 10000.0))
extra_kl = max(0.0, ((50000.0 - i) / 50000.0))
if (((i + 1) % 10000) == 0):
learn_rate = learn_rate * 0.95
# randomly sample a minibatch
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_size,))
Xb = binarize_data(Xtr.take(tr_idx, axis=0))
Xb = Xb.astype(theano.config.floatX)
# set sgd and objective function hyperparams for this update
MSM.set_sgd_params(lr_1=scale*learn_rate, lr_2=scale*learn_rate, \
mom_1=(scale*momentum), mom_2=0.98)
MSM.set_train_switch(1.0)
MSM.set_l1l2_weight(1.0)
MSM.set_lam_nll(lam_nll=1.0)
MSM.set_lam_kld(lam_kld_1=(1.0+extra_kl), lam_kld_2=(1.0+extra_kl))
MSM.set_lam_l2w(1e-6)
MSM.set_kzg_weight(0.01)
# perform a minibatch update and record the cost for this batch
result = MSM.train_joint(Xb, batch_reps)
costs = [(costs[j] + result[j]) for j in range(len(result))]
if ((i % 500) == 0):
costs = [(v / 500.0) for v in costs]
print("-- batch {0:d} --".format(i))
print(" joint_cost: {0:.4f}".format(costs[0]))
print(" nll_cost : {0:.4f}".format(costs[1]))
print(" kld_cost : {0:.4f}".format(costs[2]))
print(" reg_cost : {0:.4f}".format(costs[3]))
costs = [0.0 for v in costs]
if (((i % 2000) == 0) or ((i < 10000) and ((i % 1000) == 0))):
Xva = row_shuffle(Xva)
# draw some independent random samples from the model
samp_count = 200
model_samps = MSM.sample_from_prior(samp_count)
seq_len = len(model_samps)
seq_samps = np.zeros((seq_len*samp_count, model_samps[0].shape[1]))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = model_samps[s2][s1]
idx += 1
file_name = "MX_SAMPLES_b{0:d}.png".format(i)
utils.visualize_samples(seq_samps, file_name, num_rows=20)
# visualize some important weights in the model
file_name = "MX_INF_1_WEIGHTS_b{0:d}.png".format(i)
W = MSM.inf_1_weights.get_value(borrow=False).T
utils.visualize_samples(W[:,:obs_dim], file_name, num_rows=20)
file_name = "MX_INF_2_WEIGHTS_b{0:d}.png".format(i)
W = MSM.inf_2_weights.get_value(borrow=False).T
utils.visualize_samples(W[:,:obs_dim], file_name, num_rows=20)
file_name = "MX_GEN_1_WEIGHTS_b{0:d}.png".format(i)
W = MSM.gen_1_weights.get_value(borrow=False)
utils.visualize_samples(W[:,:obs_dim], file_name, num_rows=20)
file_name = "MX_GEN_2_WEIGHTS_b{0:d}.png".format(i)
W = MSM.gen_2_weights.get_value(borrow=False)
utils.visualize_samples(W[:,:obs_dim], file_name, num_rows=20)
file_name = "MX_GEN_INF_WEIGHTS_b{0:d}.png".format(i)
W = MSM.gen_inf_weights.get_value(borrow=False).T
utils.visualize_samples(W[:,:obs_dim], file_name, num_rows=20)
# compute information about posterior KLds on validation set
post_klds = MSM.compute_post_klds(Xva[0:5000])
file_name = "MX_H0_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(post_klds[0].shape[1]), \
np.mean(post_klds[0], axis=0), file_name)
file_name = "MX_HI_COND_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(post_klds[1].shape[1]), \
np.mean(post_klds[1], axis=0), file_name)
file_name = "MX_HI_GLOB_KLDS_b{0:d}.png".format(i)
utils.plot_stem(np.arange(post_klds[2].shape[1]), \
np.mean(post_klds[2], axis=0), file_name)
# compute information about free-energy on validation set
file_name = "MX_FREE_ENERGY_b{0:d}.png".format(i)
fe_terms = MSM.compute_fe_terms(binarize_data(Xva[0:5000]), 20)
fe_mean = np.mean(fe_terms[0]) + np.mean(fe_terms[1])
print(" nll_bound : {0:.4f}".format(fe_mean))
utils.plot_scatter(fe_terms[1], fe_terms[0], file_name, \
x_label='Posterior KLd', y_label='Negative Log-likelihood')
return
开发者ID:AjayTalati,项目名称:Sequential-Generation,代码行数:101,代码来源:TestMSM.py
示例10: print
out_file.write("{}\n".format(o_str_su))
if ((i % 2000) == 0):
# check classification error on training and validation set
train_err = GIS.classification_error(Xtr_su, Ytr_su)
va_err = GIS.classification_error(Xva, Yva)
o_str = " tr_err: {0:.4f}, va_err: {1:.4f}".format(train_err, va_err)
print(o_str)
out_file.write("{}\n".format(o_str))
out_file.flush()
if ((i % 5000) == 0):
file_name = "GIS_SAMPLES_b{0:d}.png".format(i)
tr_idx = npr.randint(low=0,high=un_samples,size=(5,))
va_idx = npr.randint(low=0,high=va_samples,size=(5,))
Xd_samps = np.vstack([Xtr_un[tr_idx,:], Xva[va_idx,:]])
Xd_samps = np.repeat(Xd_samps, 3, axis=0)
sample_lists = GIS.sample_gis_from_data(Xd_samps, loop_iters=20)
Xs = np.vstack(sample_lists["data samples"])
Ys = GIS.class_probs(Xs)
Xs = mnist_prob_embed(Xs, Ys)
utils.visualize_samples(Xs, file_name, num_rows=20)
out_file.close()
print("TESTING COMPLETE!")
##############
# EYE BUFFER #
##############
开发者ID:Philip-Bachman,项目名称:NN-Python,代码行数:29,代码来源:GIStack.py
示例11: test_lstm_structpred
#.........这里部分代码省略.........
pol_mlp_in=pol_mlp_in,
pol_mlp_out=pol_mlp_out,
pol_rnn=pol_rnn,
var_mlp_in=var_mlp_in,
var_mlp_out=var_mlp_out,
var_rnn=var_rnn,
dec_mlp_in=dec_mlp_in,
dec_mlp_out=dec_mlp_out,
dec_rnn=dec_rnn)
model.initialize()
compile_start_time = time.time()
# build the cost gradients, training function, samplers, etc.
model.build_sampling_funcs()
print("Testing model sampler...")
# draw some independent samples from the model
samp_count = 10
samp_reps = 3
x_in = Xtr[:10,:].repeat(samp_reps, axis=0)
y_in = Ytr[:10,:].repeat(samp_reps, axis=0)
x_samps, y_samps = model.sample_model(x_in, y_in, sample_source='p')
# TODO: visualize sample prediction trajectories
img_seq = seq_img_join(x_samps, y_samps, im_dim=(28,28), transposed=True)
seq_len = len(img_seq)
samp_count = img_seq[0].shape[0]
seq_samps = np.zeros((seq_len*samp_count, img_seq[0].shape[1]))
idx = 0
for s1 in range(samp_count):
for s2 in range(seq_len):
seq_samps[idx] = img_seq[s2][s1]
idx += 1
file_name = "{0:s}_samples_b{1:d}.png".format(res_tag, 0)
utils.visualize_samples(seq_samps, file_name, num_rows=samp_count)
model.build_model_funcs()
compile_end_time = time.time()
compile_minutes = (compile_end_time - compile_start_time) / 60.0
print("THEANO COMPILE TIME (MIN): {}".format(compile_minutes))
################################################################
# Apply some updates, to check that they aren't totally broken #
################################################################
print("Beginning to train the model...")
out_file = open("{}_results.txt".format(res_tag), 'wb')
costs = [0. for i in range(10)]
learn_rate = 0.0002
momentum = 0.9
batch_idx = np.arange(batch_size) + tr_samples
for i in range(300000):
scale = min(1.0, ((i+1) / 5000.0))
if (((i + 1) % 10000) == 0):
learn_rate = learn_rate * 0.95
# get the indices of training samples for this batch update
batch_idx += batch_size
if (np.max(batch_idx) >= tr_samples):
# we finished an "epoch", so we rejumble the training set
Xtr, Ytr = row_shuffle(Xtr, Ytr)
batch_idx = np.arange(batch_size)
# set sgd and objective function hyperparams for this update
model.set_sgd_params(lr=scale*learn_rate, mom_1=scale*momentum, mom_2=0.98)
model.set_lam_kld(lam_kld_q2p=1.0, lam_kld_p2q=0.1)
model.set_grad_noise(grad_noise=0.02)
# perform a minibatch update and record the cost for this batch
Xb = to_fX(Xtr.take(batch_idx, axis=0))
开发者ID:Philip-Bachman,项目名称:Sequential-Generation,代码行数:67,代码来源:TestStructPred.py
示例12: range
for i in range(MCS.chain_len):
Xtr_chains.append(0.0*Xtr)
print("Testing chain sampler....")
loop_times = []
# TESTING SAMPLING SPEED!
for i in range(batch_count):
start_time = time.clock()
batch_start = i * batch_size
batch_end = min(tr_samples, (batch_start + batch_size))
Xd_batch = Xtr[batch_start:batch_end]
Xd_chain = MCS.sample_from_chain(Xd_batch)
Xs = [Xd_batch[0:50]]
Xs.extend([xd[0:50] for xd in Xd_chain])
file_name = "MCS_TEST_{0:d}.png".format(i)
utils.visualize_samples(np.vstack(Xs), file_name, num_rows=10)
loop_times.append((time.clock() - start_time))
total_time = sum(loop_times)
mean_time = total_time / batch_count
time_std = sum([(t - mean_time)**2.0 for t in loop_times]) / batch_count
print("total_time: {0:.4f}".format(total_time))
print("mean_time: {0:.4f}, time_std: {1:.4f}".format(mean_time, time_std))
start_time = time.clock()
Xtr_chains = resample_chain_steps(MCS, Xtr_chains)
total_time = time.clock() - start_time
print("total_time: {0:.4f}".format(total_time))
开发者ID:Philip-Bachman,项目名称:NN-Python,代码行数:26,代码来源:MCSampler.py
示例13: pretrain_gip
#.........这里部分代码省略.........
# IN = INet.load_infnet_from_file(f_name=in_fname, rng=rng, Xd=Xd, \
# Xc=Xc, Xm=Xm, new_params=new_in_params)
# GN = GNet.load_gennet_from_file(f_name=gn_fname, rng=rng, Xp=Xp, \
# new_params=new_gn_params)
# in_params = IN.params
# gn_params = GN.params
#########################
# INITIALIZE THE GIPAIR #
#########################
GIP = GIPair(rng=rng, Xd=Xd, Xc=Xc, Xm=Xm, g_net=GN, i_net=IN, \
data_dim=data_dim, prior_dim=PRIOR_DIM, params=None)
GIP.set_lam_l2w(1e-4)
####################
# RICA PRETRAINING #
####################
IN.W_rica.set_value(0.05 * IN.W_rica.get_value(borrow=False))
GN.W_rica.set_value(0.05 * GN.W_rica.get_value(borrow=False))
for i in range(6000):
scale = min(1.0, (float(i+1) / 6000.0))
l_rate = 0.0001 * scale
lam_l1 = 0.025
tr_idx = npr.randint(low=0,high=tr_samples,size=(1000,))
Xd_batch = Xtr.take(tr_idx, axis=0)
inr_out = IN.train_rica(Xd_batch, l_rate, lam_l1)
gnr_out = GN.train_rica(Xd_batch, l_rate, lam_l1)
inr_out = [v for v in gnr_out]
if ((i % 1000) == 0):
print("rica batch {0:d}: in_recon={1:.4f}, in_spars={2:.4f}, gn_recon={3:.4f}, gn_spars={4:.4f}".format( \
i, 1.*inr_out[1], 1.*inr_out[2], 1.*gnr_out[1], 1.*gnr_out[2]))
# draw inference net first layer weights
file_name = RESULT_PATH+"pt_rica_inf_weights.png".format(i)
utils.visualize_samples(IN.W_rica.get_value(borrow=False).T, file_name, num_rows=20)
# draw generator net final layer weights
file_name = RESULT_PATH+"pt_rica_gen_weights.png".format(i)
if ('gaussian' in gn_params['out_type']):
lay_num = -2
else:
lay_num = -1
utils.visualize_samples(GN.W_rica.get_value(borrow=False), file_name, num_rows=20)
######################
# BASIC VAE TRAINING #
######################
out_file = open(RESULT_PATH+"pt_gip_results.txt", 'wb')
# Set initial learning rate and basic SGD hyper parameters
cost_1 = [0. for i in range(10)]
learn_rate = 0.0002
for i in range(300000):
scale = min(1.0, float(i) / 40000.0)
if ((i + 1) % 100000 == 0):
learn_rate = learn_rate * 0.8
# do a minibatch update of the model, and compute some costs
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_size,))
Xd_batch = Xtr.take(tr_idx, axis=0)
Xd_batch = np.repeat(Xd_batch, batch_reps, axis=0)
Xc_batch = 0.0 * Xd_batch
Xm_batch = 0.0 * Xd_batch
# do a minibatch update of the model, and compute some costs
GIP.set_all_sgd_params(lr_gn=(scale*learn_rate), \
lr_in=(scale*learn_rate), mom_1=0.9, mom_2=0.999)
GIP.set_lam_nll(1.0)
GIP.set_lam_kld(1.0 + extra_lam_kld*scale)
outputs = GIP.train_joint(Xd_batch, Xc_batch, Xm_batch)
cost_1 = [(cost_1[k] + 1.*outputs[k]) for k in range(len(outputs))]
开发者ID:Philip-Bachman,项目名称:NN-Python,代码行数:67,代码来源:SVHNWalkoutTest.py
示例14: train_walk_from_pretrained_gip
#.........这里部分代码省略.........
vcgl_params['lam_l2d'] = 5e-2
VCGL = VCGLoop(rng=rng, Xd=Xd, Xc=Xc, Xm=Xm, Xt=Xt, i_net=IN, \
g_net=GN, d_net=DN, chain_len=6, data_dim=data_dim, \
prior_dim=PRIOR_DIM, params=vcgl_params)
VCGL.set_lam_l2w(1e-4)
out_file = open(RESULT_PATH+"pt_walk_results.txt", 'wb')
####################################################
# Train the VCGLoop by unrolling and applying BPTT #
####################################################
learn_rate = 0.0002
cost_1 = [0. for i in range(10)]
for i in range(1000000):
scale = float(min((i+1), 25000)) / 25000.0
if ((i+1 % 50000) == 0):
learn_rate = learn_rate * 0.8
########################################
# TRAIN THE CHAIN IN FREE-RUNNING MODE #
########################################
VCGL.set_all_sgd_params(learn_rate=(scale*learn_rate), \
mom_1=0.9, mom_2=0.999)
VCGL.set_disc_weights(dweight_gn=20.0, dweight_dn=4.0)
VCGL.set_lam_chain_nll(1.0)
VCGL.set_lam_chain_kld(1.0 + extra_lam_kld)
VCGL.set_lam_chain_vel(0.0)
VCGL.set_lam_mask_nll(0.0)
VCGL.set_lam_mask_kld(0.0)
# get some data to train with
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_size,))
Xd_batch = Xtr.take(tr_idx, axis=0)
Xc_batch = 0.0 * Xd_batch
Xm_batch = 0.0 * Xd_batch
# do 5 repetitions of the batch
Xd_batch = np.repeat(Xd_batch, batch_reps, axis=0)
Xc_batch = np.repeat(Xc_batch, batch_reps, axis=0)
Xm_batch = np.repeat(Xm_batch, batch_reps, axis=0)
# examples from the target distribution, to train discriminator
tr_idx = npr.randint(low=0,high=tr_samples,size=(batch_reps*batch_size,))
Xt_batch = Xtr.take(tr_idx, axis=0)
# do a minibatch update of the model, and compute some costs
outputs = VCGL.train_joint(Xd_batch, Xc_batch, Xm_batch, Xt_batch)
cost_1 = [(cost_1[k] + 1.*outputs[k]) for k in range(len(outputs))]
if ((i % 1000) == 0):
cost_1 = [(v / 1000.0) for v in cost_1]
o_str_1 = "batch: {0:d}, joint_cost: {1:.4f}, chain_nll_cost: {2:.4f}, chain_kld_cost: {3:.4f}, disc_cost_gn: {4:.4f}, disc_cost_dn: {5:.4f}".format( \
i, cost_1[0], cost_1[1], cost_1[2], cost_1[6], cost_1[7])
print(o_str_1)
out_file.write(o_str_1+"\n")
out_file.flush()
cost_1 = [0. for v in cost_1]
if ((i % 5000) == 0):
tr_idx = npr.randint(low=0,high=Xtr.shape[0],size=(5,))
va_idx = npr.randint(low=0,high=Xva.shape[0],size=(5,))
Xd_batch = np.vstack([Xtr.take(tr_idx, axis=0), Xva.take(va_idx, axis=0)])
# draw some chains of samples from the VAE loop
file_name = RESULT_PATH+"pt_walk_chain_samples_b{0:d}.png".format(i)
Xd_samps = np.repeat(Xd_batch, 3, axis=0)
sample_lists = VCGL.GIP.sample_from_chain(Xd_samps, loop_iters=20)
Xs = np.vstack(sample_lists["data samples"])
utils.visualize_samples(Xs, file_name, num_rows=20)
# draw some masked chains of samples from the VAE loop
file_name = RESULT_PATH+"pt_walk_mask_samples_b{0:d}.png".format(i)
Xd_samps = np.repeat(Xc_mean[0:Xd_batch.shape[0],:], 3, axis=0)
Xc_samps = np.repeat(Xd_batch, 3, axis=0)
Xm_rand = sample_masks(Xc_samps, drop_prob=0.2)
Xm_patch = sample_patch_masks(Xc_samps, (32,32), (16,16))
Xm_samps = Xm_rand * Xm_patch
sample_lists = VCGL.GIP.sample_from_chain(Xd_samps, \
X_c=Xc_samps, X_m=Xm_samps, loop_iters=20)
Xs = np.vstack(sample_lists["data samples"])
utils.visualize_samples(Xs, file_name, num_rows=20)
# draw some samples independently from the GenNet's prior
file_name = RESULT_PATH+"pt_walk_prior_samples_b{0:d}.png".format(i)
Xs = VCGL.sample_from_prior(20*20)
utils.visualize_samples(Xs, file_name, num_rows=20)
# draw discriminator network's weights
file_name = RESULT_PATH+"pt_walk_dis_weights_b{0:d}.png".format(i)
utils.visualize_net_layer(VCGL.DN.proto_nets[0][0], file_name)
# draw inference net first layer weights
file_name = RESULT_PATH+"pt_walk_inf_weights_b{0:d}.png".format(i)
utils.visualize_net_layer(VCGL.IN.shared_layers[0], file_name)
# draw generator net final layer weights
file_name = RESULT_PATH+"pt_walk_gen_weights_b{0:d}.png".format(i)
if GN.out_type == 'sigmoid':
utils.visualize_net_layer(VCGL.GN.mlp_layers[-1], file_name, use_transpose=True)
else:
utils.visualize_net_layer(VCGL.GN.mlp_layers[-2], file_name, use_transpose=True)
#########################
# Check posterior KLds. #
#########################
post_klds = posterior_klds(IN, Xtr, 5000, 5)
file_name = RESULT_PATH+"pt_walk_post_klds_b{0:d}.png".format(i)
utils.plot_kde_histogram2( \
np.asarray(post_klds), np.asarray(post_klds), file_name, bins=30)
# DUMP PARA
|
请发表评论