<jupyter_start><jupyter_text>Initialize the environment<jupyter_code>import theano
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
src_dir='../src' # source directory
run_dir_prefix='../MDBN_run/AML_Batch_' # directory with the results of previous runs
data_dir='../data' # directory with the data files
import sys
sys.path.insert(0, src_dir)
import rbm
import MDBN
import main<jupyter_output><empty_output><jupyter_text>Load the experiment results<jupyter_code>run=0
uuid='eb6856a251bb8680da6593de98db7b5a'
timestamp='20170220T205506' # specify the date and time of the run in the format YYYYmmddTHHMMSS
run_dir=run_dir_prefix+timestamp
runfile='Exp_%s_run_%s.npz' % (uuid, run) # location of the experiment results
mdbn = MDBN.MDBN()
config, dbn_dict = mdbn.load_network(runfile, run_dir)
config<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>datafiles = main.prepare_TCGA_datafiles("AML",config,data_dir)
import utils
real_set, _ = utils.load_n_preprocess_data(datafiles['SM'],datadir=data_dir)<jupyter_output><empty_output><jupyter_text>Compute the joint layer components<jupyter_code>dbn_output_list=[]
for key in config['pathways']:
    dbn_output, _ = dbn_dict[key].MLP_output_from_datafile(datafiles[key], datadir=data_dir)
    dbn_output_list.append(dbn_output)
dbn_output = dbn_dict['SM'].get_output(theano.shared(np.zeros(real_set.get_value().shape,
                                                                        dtype=theano.config.floatX),borrow=True))
#dbn_output = np.zeros((real_set.get_value().shape[0],20),dtype=theano.config.floatX)
joint_layer = np.hstack((dbn_output_list[0],dbn_output_list[1],dbn_output))
#joint_layer = np.concatenate(dbn_output_list,axis=1)
plt.imshow(joint_layer, interpolation='none', cmap='gray')
plt.axis('tight')
plt.colorbar()
real_joint_layer = np.concatenate(dbn_output_list,axis=1)
plt.imshow(real_joint_layer, interpolation='none', cmap='gray')
plt.axis('tight')
plt.colorbar()
top_output = dbn_dict['top'].get_output(joint_layer)
plt.imshow(top_output, interpolation='none')
plt.axis('tight')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Gibbs sampling of the top layer and prop down w/ ancestral samplingWe iterate the reconstruction 50 times<jupyter_code>reload(rbm)
top_dbn_rbm0 = dbn_dict['top'].rbm_layers[0]
top_dbn_rbm1 = dbn_dict['top'].rbm_layers[1]
h1 = theano.tensor.matrix('h1')
rec_joint_layer = top_dbn_rbm0.sample_v_given_h(h1)
rec_joint_fn = theano.function([h1], rec_joint_layer)
for i in range(50):
    top_output = dbn_dict['top'].get_output(joint_layer)
    sampled_hidden_layer = top_dbn_rbm1.reverse_sampling(1,theano.shared(top_output,borrow=True),gibbs_steps=50)
    sampled_joint_layer = rec_joint_fn(sampled_hidden_layer[0])
    joint_layer = np.concatenate([joint_layer[:,:80],sampled_joint_layer[2][:,80:100]],axis=1)
plt.imshow(joint_layer, cmap='gray',interpolation='none')
plt.axis('tight')
plt.colorbar()
plt.imshow(np.sqrt(np.square(real_joint_layer[:,80:100]-joint_layer[:,80:100])))
plt.axis('tight')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Propagate down on the unimodal DBN<jupyter_code>dbn_rbm1 = dbn_dict['SM'].rbm_layers[1]
sampled_hid_layer = dbn_rbm1.reverse_sampling(1,theano.shared(joint_layer[:,80:100],borrow=True))
plt.imshow(sampled_hid_layer[0], interpolation='none', cmap='gray')
plt.axis('tight')
plt.colorbar()
dbn_rbm0 = dbn_dict['SM'].rbm_layers[0]
vis_layer = dbn_rbm0.sample_v_given_h(h1)
vis_fn = theano.function([h1], vis_layer)
_, _, sampled_vis_layer = vis_fn(sampled_hid_layer[0])
plt.imshow(sampled_vis_layer,interpolation='none',cmap='gray',clim=(-1,1))
plt.axis('tight')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Compare reconstructed and actual data<jupyter_code>#plt.imshow(np.sqrt(np.square(real_set.get_value()-sampled_vis_layer)),interpolation='none',clim=(0,2))
#plt.axis('tight')
#plt.colorbar()
plt.hist(sampled_vis_layer)
plt.hist(real_set.get_value())
plt.imshow(real_set.get_value(),cmap='gray',clim=(-1,1))
plt.axis('tight')
plt.colorbar()
c = []
d = []
rss = 0
tss = 0
for i in range(170):
    rss = np.sum(np.square(real_set.get_value()[i]-sampled_vis_layer[i])[0])
    tss = np.sum(np.square(real_set.get_value()[i]-np.mean(real_set.get_value()[0]))[0])
    if rss/tss < 1.0:
        c.append(rss/tss)
    else:
        if rss/tss < 15.0:
            d.append(rss/tss)
        else:
            print i, rss/tss
plt.hist(c)
np.mean(c)
len(c)
plt.hist(d)
np.mean(d)
len(d)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14736377674829032, 'fraction_numerical': 0.02514890800794176, 'mean_word_length': 7.140035906642729, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '293682', 'license': 'Apache-2.0', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1789, 'n_words': 235, 'path': 'notebooks/Sampling (Run 2017-02-20_2302)-AML-SM-v4.ipynb', 'repo_name': 'glgerard/cloud-mdbn', 'size': '4533'}