<jupyter_start><jupyter_text>RBM Name Generation<jupyter_code>import argparse
import pickle
from sklearn.model_selection import train_test_split
from GridEncoder import GridEncoder
import Utils
from ShortTextCodec import ShortTextCodec, BinomialShortTextCodec
from RBM import BernoulliRBM
import Sampling
import sample
import sys
import colorama
import numpy as np
import sys
import os
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent =  currentdir + '\RBM_Git'
sys.path.insert(0,parent)
%reload_ext autoreload
%autoreload 2
%matplotlib inline
import time
import matplotlib.pyplot as plt
import numexpr  as ne
import profile
import rbm as Rbm
import pandas
from random import randint
from timeit import default_timer as timer
BIASED_PRIOR = 0

class CharBernoulli(BernoulliRBM):

    def __init__(self, **kwargs):
        """
        codec is the ShortTextCodec used to create the vectors being fit. The
        most important function of the codec is as a proxy to the shape of the
        softmax units in the visible layer (if you're using the CharBernoulliRBMSoftmax
        subclass). It's also used to decode and print
        fantasy particles at the end of each epoch.
        """
        # Attaching this to the object is really helpful later on when models
        # are loaded from pickle in visualize.py and sample.py
        self.codec = kwargs.pop("codec")
        self.softmax_shape = codec.shape()
        # Old-style class :(
        BernoulliRBM.__init__(self, **kwargs)

    def wellness_check(self, epoch, duration, train, validation):
        BernoulliRBM.wellness_check(self, epoch, duration, train, validation)
        fantasy_samples = '|'.join([self.codec.decode(vec) for vec in
                                    self._sample_visibles(self.h_samples_[:3], temperature=0.1)])
        print ("Fantasy samples: {}".format(fantasy_samples))

    def corrupt(self, v):
        n_softmax, n_opts = self.softmax_shape
        # Select a random index in to the indices of the non-zero values of each input
        # TODO: In the char-RBM case, if I wanted to really challenge the model, I would avoid selecting any
        # trailing spaces here. Cause any dumb model can figure out that it should assign high energy to
        # any instance of /  [^ ]/
        meta_indices_to_corrupt = self.rng_.randint(0, n_softmax, v.shape[0]) + np.arange(0, n_softmax * v.shape[0], n_softmax)

        # Offset these indices by a random amount (but not 0 - we want to actually change them)
        offsets = self.rng_.randint(1, n_opts, v.shape[0])
        # Also, do some math to make sure we don't "spill over" into a different softmax.
        # E.g. if n_opts=5, and we're corrupting index 3, we should choose offsets from {-3, -2, -1, +1}
        # 1-d array that matches with meta_i_t_c but which contains the indices themselves
        indices_to_corrupt = v.indices[meta_indices_to_corrupt]
        # Sweet lucifer
        offsets = offsets - (n_opts * (((indices_to_corrupt % n_opts) + offsets.ravel()) >= n_opts))

        v.indices[meta_indices_to_corrupt] += offsets
        return v, (meta_indices_to_corrupt, offsets)

    def uncorrupt(self, visibles, state):
        mitc, offsets = state
        visibles.indices[mitc] -= offsets
        
class CharBernoulliSoftmax(CharBernoulli):
    def __init__(self,**kwargs):
        CharBernoulli.__init__(self, **kwargs)
        
    def _sample_visibles(self, h, temperature=1.0):
        """Sample from the distribution P(v|h). This obeys the softmax constraint
        on visible units. i.e. sum(v) == softmax_shape[0] for any visible
        configuration v.

        h : array-like, shape (n_samples, n_components)
            Values of the hidden layer to sample from.

        Returns
        -------
        v : array-like, shape (n_samples, n_features)
            Values of the visible layer.
        """
        p = np.dot(h, self.components_/temperature)
        p += self.intercept_visible_/(min(1.0, temperature) if BIASED_PRIOR else temperature)
        nsamples, nfeats = p.shape
        reshaped = np.reshape(p, (nsamples,) + self.softmax_shape)
        return Utils.softmax_and_sample(reshaped).reshape((nsamples, nfeats))

codec_kls = ShortTextCodec
codec = codec_kls('',10,0,True,False)
codec.debug_description()
model_kwargs = {'codec': codec,
                        'n_components': 300,
                        'learning_rate': 0.1,
                        'lr_backoff': False,
                        'n_iter': 500,
                        'verbose': 1,
                        'batch_size': 32,
                        'weight_cost': 0.0001,
                        }
kls = CharBernoulliSoftmax
rbm = kls(**model_kwargs)
codec.alphabet<jupyter_output><empty_output><jupyter_text>English Names File<jupyter_code>vecs = Utils.vectors_from_txtfile("./names.txt", codec)
train, validation = train_test_split(vecs, test_size=0.5)
print(train.shape,validation.shape)
%%time
rbm.fit(train,validation)
SAMPLES = []
def horizontal_cb(strings, i, energy=None):
    global SAMPLES
    
    if energy is not None:
        SAMPLES.append(zip(strings, energy))
    else:
        SAMPLES.append(strings)
        
def print_columns(maxlen):
    col_width = maxlen+2
    for fantasy_index in range(len(SAMPLES[0])):
        particles = [s[fantasy_index] for s in SAMPLES]
        print ("".join(s[fantasy_index].ljust(col_width) for s in SAMPLES))
        
sample_indices = [1000-1]
kwargs = dict(start_temp=1.0, final_temp=1.0, sample_energy=False, 
                    callback=horizontal_cb)

vis = Sampling.sample_model(rbm, 30, 1000, sample_indices, **kwargs)
print_columns(rbm.codec.maxlen)
fe = rbm._free_energy(vis)
print('Final energy: {:.2f} (stdev={:.2f})\n'.format(fe.mean(), fe.std()))<jupyter_output><empty_output><jupyter_text>Spanish Name File<jupyter_code>codec_kls = ShortTextCodec
codec = codec_kls('áéíóúñÁÉÍÓÚÑ',10,0,True,False)
codec.debug_description()
model_kwargs = {'codec': codec,
                        'n_components': 300,
                        'learning_rate': 0.1,
                        'lr_backoff': False,
                        'n_iter': 500,
                        'verbose': 1,
                        'batch_size': 32,
                        'weight_cost': 0.0001,
                        }
print(codec.alphabet)
kls = CharBernoulliSoftmax
rbm = kls(**model_kwargs)
vecs = Utils.vectors_from_txtfile("./spanish_Dict.txt", codec)
train, validation = train_test_split(vecs, test_size=0.5)
print(train.shape,validation.shape)
%%time
rbm.fit(train,validation)
SAMPLES = []
def horizontal_cb(strings, i, energy=None):
    global SAMPLES
    if energy is not None:
        SAMPLES.append(zip(strings, energy))
    else:
        SAMPLES.append(strings)
def print_columns(maxlen):
    col_width = maxlen+2
    for fantasy_index in range(len(SAMPLES[0])):
        particles = [s[fantasy_index] for s in SAMPLES]
        print ("".join(s[fantasy_index].ljust(col_width) for s in SAMPLES))
sample_indices = [1000-1]
kwargs = dict(start_temp=1.0, final_temp=1.0, sample_energy=False, 
                    callback=horizontal_cb)

vis = Sampling.sample_model(rbm, 30, 1000, sample_indices, **kwargs)
print_columns(rbm.codec.maxlen)
fe = rbm._free_energy(vis)
print('Final energy: {:.2f} (stdev={:.2f})\n'.format(fe.mean(), fe.std()))<jupyter_output><empty_output><jupyter_text>Test with RBM Implementation<jupyter_code>import sys
import os
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent =  currentdir + '\RBM_Git'
sys.path.insert(0,parent)
%reload_ext autoreload
%autoreload 2
%matplotlib inline
import time
import matplotlib.pyplot as plt
import numexpr  as ne
import profile
import rbm as Rbm
import pandas
from random import randint
from timeit import default_timer as timer
codec_kls = ShortTextCodec
codec = codec_kls('áéíóúñÁÉÍÓÚÑ',10,0,True,False)
print(codec.debug_description)
vecs = Utils.vectors_from_txtfile("./spanish_Dict.txt", codec)
visible_dim = vecs.shape[1]
hidden_dim = 200
epochs = 100
K = 1
lr = 0.1
batch_size = 10
rbm_ = Rbm.RBM(visible_dim=visible_dim,
               hidden_dim=hidden_dim,
               seed=42,
               mu=0, 
               sigma=0.3,
               monitor_time=True)
rbm_.W.shape, rbm_.b.shape, rbm_
test_Data_Vector_Aux = np.array(vecs.toarray(), dtype="float64")
%%time
rbm_.fit(test_Data_Vector_Aux, 
         method='CDK',
         K=K,
         lr=0.01,
         epochs=1,
         batch_size=128,
         plot_weights=False)
%%time
rbm_.fit(test_Data_Vector_Aux, 
         method='vectorized_CDK',
         K=K,
         lr=0.01,
         epochs=1,
         batch_size=128,
         plot_weights=False)
%%time
rbm_.fit(test_Data_Vector_Aux, 
         method='vectorized_CDK',
         K=K,
         lr=0.01,
         epochs=200,
         batch_size=128,
         plot_weights=False)
word = codec.encode_onehot("acabar")
print(word.shape)
x_hat, x_hat_p = rbm_.sample_visible_from_visible(word, n_gibbs=2000)
final_result = []
for i in range(x_hat.shape[0]):
    if x_hat[i] == True:
        final_result.append(1)
    else:
        final_result.append(0)
result = []
result.append(final_result)
result = np.asarray(result)
result = result.ravel()
print(result)
print(codec.decode(result))
fW = codec.decode(x_hat)
print(fW)
# for i in range(10):
#     x_hat1,x_hat_p1 = rbm_.sample_visible_from_visible(x_hat, n_gibbs=2000)
#     print(codec.decode(x_hat1))<jupyter_output>?os???$$$$<jupyter_text>SKLEARN BERNOULLI<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import BernoulliRBM
%matplotlib inline   
import pandas as pd
rbm_Bernoulli = BernoulliRBM(n_components=100, learning_rate=0.01, random_state=0, verbose=True)
vecs = Utils.vectors_from_txtfile("./spanish_Dict.txt", codec)
rbm_Bernoulli.fit(vecs)
test = rbm_Bernoulli.gibbs(vecs[0])
test = test.ravel()
def transformFromOneHotVector(vec):
    finalWord = []
    for k in range(len(vec)):
        tmp = []
        for i in range(len(vec[k])):
            if vec[k][i] == 1:
                index = i%maxLength
                if(index != maxLength):
                    tmp.append(index)
        finalWord.append(tmp)
    return finalWord
xx = vecs[:40].copy()
maxLength = 26
for ii in range(1000):
    for n in range(40):
        xx[n] = rbm_Bernoulli.gibbs(xx[n])
for i in range(xx.shape[0]):
    tmp = xx[i].toarray()
    print(transformFromOneHotVector(tmp))
print(xx.shape)<jupyter_output>C:\Users\mferrer\Anaconda3\lib\site-packages\scipy\sparse\compressed.py:774: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)<jupyter_text>BERNOULLI GRIDS TEST<jupyter_code>from sklearn.preprocessing import OneHotEncoder
from sklearn.neural_network import BernoulliRBM
import numpy as np
def generate_data_fake(): 
    vec = [] 
    for i in range(1000): 
        tmp = [] 
        for k in range(48): 
            tmp.append(0) 
        vec.append(tmp) 
    return np.asarray(vec) 

grid_data = generate_data_fake()
for i in range(1000):
    grid_data[i][0] = 1
oneHotEncoder = OneHotEncoder(255, sparse=True).fit(grid_data)
oneHotData = oneHotEncoder.transform(grid_data)
RBM_Machine = BernoulliRBM(n_components=250, learning_rate=0.01, n_iter=10, random_state=0, verbose=True)
RBM_Machine.fit(oneHotData)
x_visible = RBM_Machine.gibbs(oneHotData[0])
x_visible = x_visible.ravel()
final_vector = []
for k in range(len(x_visible)):
    if(x_visible[k] == True):
        final_vector.append(1)
    else:
        final_vector.append(0)
output_data = []
for i in range(48):
    tmp = final_vector[(255*i):((i+1)*255)]
    output_data.append(tmp.index(1))
print (output_data)<jupyter_output>[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
{'fraction_non_alphanumeric': 0.11173974540311174, 'fraction_numerical': 0.023130044096846658, 'mean_word_length': 3.1534208707671043, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '333762', 'license': 'MIT', 'n_tokens_mistral': 4321, 'n_tokens_neox': 4040, 'n_words': 969, 'path': 'RBM_Name_Generation.ipynb', 'repo_name': 'MarcFerrerMargarit/NameGenerationRBM', 'size': '12019'}