Access weights in RESTRICTED BOLTZMANN MACHINES

Kunal_Dapse: <p>How to access weights</p>

Usama_Hasan: <p>Hy, for any given layer of a model which you define in pytorch, it’s weights can be accessed using this.</p>
<pre><code class="lang-auto">model.layer[index].weight
</code></pre>

Kunal_Dapse: <p>But I am not able to figure it out for Restricted Boltzmann Machines. Will you help me with this?</p>
<pre><code class="lang-auto">import pandas as pd
import torch

import torch.nn.parallel

import torch.utils.data

x_dataset = pd.read_csv(r"C:\Users\Kunal Dapse\Downloads\AI-DataTrain.csv")
#y_dataset = pd.read_csv(r"C:\Users\Kunal Dapse\Downloads\AI-DataTest.csv")
training_dataset = x_dataset.iloc[:900,1:11].values
test_dataset     =x_dataset.iloc[900:,1:11].values

training_dataset = torch.FloatTensor(training_dataset)
test_dataset = torch.FloatTensor(test_dataset)


class RBM():
    def __init__(self,nv,nh):
        self.W = torch.randn(nh,nv)
        self.a = torch.randn(1, nh)
        self.b = torch.randn(1, nv)
    def sample_h(self, x):
        wx = torch.mm(x, self.W.t())
        activation = wx + self.a.expand_as(wx)
        p_h_given_v = torch.sigmoid(activation)
        return p_h_given_v, torch.bernoulli(p_h_given_v)
    def sample_v(self, y):
        wy = torch.mm(y,self.W)
        activation = wy + self.b.expand_as(wy)
        p_v_given_h = torch.sigmoid(activation)
        return p_v_given_h, torch.bernoulli(p_v_given_h)
    def train(self, v0, vk, ph0, phk):
        self.W += (torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)).t()
        self.b += torch.sum((v0-vk), 0)
        self.a += torch.sum((ph0-phk), 0)

nv = len(training_dataset[0])
nh = 100
batch_size = 100
rbm = RBM(nv, nh)


#Training the RBM
nb_epoch = 10
for epoch in range(1, nb_epoch + 1):
    train_loss = 0
    s = 0.
    for id_examinee in range(0,1000 -  batch_size, batch_size):
        vk = training_dataset[id_examinee:id_examinee + batch_size]
        v0 = training_dataset[id_examinee:id_examinee + batch_size]
        ph0,_ =rbm.sample_h(v0)
        for k in range(10):
            _,hk = rbm.sample_h(vk)
            _,vk = rbm.sample_v(hk)
        phk,_ = rbm.sample_h(vk)
        rbm.train(v0, vk, ph0, phk)
        train_loss += torch.mean(torch.abs(v0 - vk))
        s += 1.
    print('epoch: '+ str(epoch)+' loss: '+str(train_loss/s))

#Testing the RBM

test_loss = 0
s = 0.
for id_examinee in range(0, 1000):
    v = training_dataset[id_examinee:id_examinee+1]
    vt = test_dataset[id_examinee:id_examinee +1]

    if len(vt[vt&gt;=0]) &gt; 0:
       _,h = rbm.sample_h(v)
       _,v = rbm.sample_v(h)

       test_loss += torch.mean(torch.abs(vt[vt&gt;=0] - v[vt&gt;=0]))
       s += 1.
print('test loss: '+str(test_loss/s))
</code></pre>

Usama_Hasan: <p>Hy Kunal, Sure.<br>
First you need to extend your class from torch.nn.Module to create model class.</p>
<aside class="quote no-group" data-username="Kunal_Dapse" data-post="3" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kunal_dapse/48/26998_2.png" class="avatar"> Kunal_Dapse:</div>
<blockquote>
<p><code>class RBM():</code></p>
</blockquote>
</aside>
<pre><code class="lang-auto">class RBM(nn.Module):
       def __init__(self):
              super(RBM,self).__init()
              //code 
              self.W = nn.Parameter(torch.randn(nh,nv))
              self.v_bias = nn.Parameter(torch.randn(1,nh))
              self.h_bias = nn.Parameter(torch.zeros(1,nv))
      def forward(self,input):
              self.layer(input)
</code></pre>
<p>You can define the rest of the function inside the class and call them in forward function.</p>

Kunal_Dapse: <aside class="quote no-group" data-username="Usama_Hasan" data-post="2" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/usama_hasan/48/29254_2.png" class="avatar"> Usama_Hasan:</div>
<blockquote>
<p><code>model.layer[index].weight</code></p>
</blockquote>
</aside>
<p>I need help again. I tried to figure it out but I am stuck. I want a list of weights but I am not able to solve this error <em>AttributeError: ‘RBM’ object has no attribute 'layer</em>.</p>
<pre><code class="lang-auto">import pandas as pd
import torch
import torch.nn as nn
import torch.nn.parallel

import torch.utils.data

x_dataset = pd.read_csv(r"C:\Users\Kunal Dapse\Downloads\AI-DataTrain.csv")
y_dataset = pd.read_csv(r"C:\Users\Kunal Dapse\Downloads\AI-DataTest.csv")
training_dataset = x_dataset.iloc[:900,1:11].values
test_dataset     =x_dataset.iloc[900:,1:11].values

training_dataset = torch.FloatTensor(training_dataset)
test_dataset = torch.FloatTensor(test_dataset)


class RBM(nn.Module):
    def __init__(self, nv, nh):
              super(RBM,self).__init__()
              
              self.W = nn.Parameter(torch.randn(nh,nv))
              self.a = nn.Parameter(torch.randn(1,nh))
              self.b = nn.Parameter(torch.zeros(1,nv))
    def forward(self,input):
              out = self.layer(input)
              return out
    def sample_h(self, x):
        wx = torch.mm(x, self.W.t())
        activation = wx + self.a.expand_as(wx)
        p_h_given_v = torch.sigmoid(activation)
        return p_h_given_v, torch.bernoulli(p_h_given_v)
    def sample_v(self, y):
        wy = torch.mm(y,self.W)
        activation = wy + self.b.expand_as(wy)
        p_v_given_h = torch.sigmoid(activation)
        return p_v_given_h, torch.bernoulli(p_v_given_h)
    def train(self, v0, vk, ph0, phk):
        with torch.no_grad():
            
            self.W += (torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)).t()
            self.b += torch.sum((v0-vk), 0)
            self.a += torch.sum((ph0-phk), 0)

nv = len(training_dataset[0])
nh = 100
batch_size = 100
rbm = RBM(nv, nh)


#Training the RBM
nb_epoch = 10
for epoch in range(1, nb_epoch + 1):
    train_loss = 0
    s = 0.
    for id_examinee in range(0,1000 -  batch_size, batch_size):
        vk = training_dataset[id_examinee:id_examinee + batch_size]
        v0 = training_dataset[id_examinee:id_examinee + batch_size]
        ph0,_ =rbm.sample_h(v0)
        for k in range(10):
            _,hk = rbm.sample_h(vk)
            _,vk = rbm.sample_v(hk)
        phk,_ = rbm.sample_h(vk)
        rbm.train(v0, vk, ph0, phk)
        train_loss += torch.mean(torch.abs(v0 - vk))
        s += 1.
    print('epoch: '+ str(epoch)+' loss: '+str(train_loss/s))

#Testing the RBM

test_loss = 0
s = 0.
for id_examinee in range(0, 1000):
    v = training_dataset[id_examinee:id_examinee+1]
    vt = test_dataset[id_examinee:id_examinee +1]

    if len(vt[vt&gt;=0]) &gt; 0:
       _,h = rbm.sample_h(v)
       _,v = rbm.sample_v(h)

       test_loss += torch.mean(torch.abs(vt[vt&gt;=0] - v[vt&gt;=0]))
       s += 1.
print('***test loss: '+str(test_loss/s))

Questions = ['Q1', 'Q2', 'Q3','Q4','Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10']
model_1 = RBM(nv,nh) ## ERROR!!!
Weights = model_1.layer[0].weight
df = pd.DataFrame({'Questions': Questions,
                   'Weights': Weights
                   })
</code></pre>

Usama_Hasan: <aside class="quote no-group" data-username="Kunal_Dapse" data-post="5" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kunal_dapse/48/26998_2.png" class="avatar"> Kunal_Dapse:</div>
<blockquote>
<p><code> self.W = nn.Parameter(torch.randn(nh,nv))</code></p>
</blockquote>
</aside>
<p>Since in RBM implementation, that you have done weights are initialized here, you can just access them by a return call.</p>
<pre><code class="lang-auto">def get_weights():
        return self.W
</code></pre>
<p>Also you should look at some other implementation of rbm, I liked this one much better.</p><aside class="onebox githubblob" data-onebox-src="https://github.com/GabrielBianconi/pytorch-rbm/blob/master/rbm.py">
  <header class="source">

      <a href="https://github.com/GabrielBianconi/pytorch-rbm/blob/master/rbm.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/GabrielBianconi/pytorch-rbm/blob/master/rbm.py" target="_blank" rel="noopener nofollow ugc">GabrielBianconi/pytorch-rbm/blob/master/rbm.py</a></h4>


      <pre><code class="lang-py">import torch


class RBM():

    def __init__(self, num_visible, num_hidden, k, learning_rate=1e-3, momentum_coefficient=0.5, weight_decay=1e-4, use_cuda=True):
        self.num_visible = num_visible
        self.num_hidden = num_hidden
        self.k = k
        self.learning_rate = learning_rate
        self.momentum_coefficient = momentum_coefficient
        self.weight_decay = weight_decay
        self.use_cuda = use_cuda

        self.weights = torch.randn(num_visible, num_hidden) * 0.1
        self.visible_bias = torch.ones(num_visible) * 0.5
        self.hidden_bias = torch.zeros(num_hidden)

        self.weights_momentum = torch.zeros(num_visible, num_hidden)
        self.visible_bias_momentum = torch.zeros(num_visible)
</code></pre>



  This file has been truncated. <a href="https://github.com/GabrielBianconi/pytorch-rbm/blob/master/rbm.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


Kunal_Dapse: <p>But I am trying to create the list of weights assigned which I couldn’t do it. As you said I used <em>model.layer[index].weight</em>  but I am facing an Attribute Error.</p>
<p>Would you please guide me I am new to Deep learning currently working on a project.</p>

Usama_Hasan: <p>Hy <a class="mention" href="/u/kunal_dapse">@Kunal_Dapse</a>, I would highly recommend you read some tutorials first, you’re totaly misunderstanding me here. The way we construct models in pytorch is by inheriting them through nn.Module class.<br>
Something like this</p>
<pre><code class="lang-auto">import torch.nn as nn
class Net(nn.Module):
       nn.Linear(input_channels,output_channels)
       /// Further layer.
</code></pre>
<p>Now I  have declared a single Linear (MLP) inside my model using torch.nn.Linear, this layer contains all the attributes an MLP should have, weights bias etc.<br>
Which we can later access like this which I explained first.</p>
<aside class="quote no-group" data-username="Usama_Hasan" data-post="2" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/usama_hasan/48/29254_2.png" class="avatar"> Usama_Hasan:</div>
<blockquote>
<p><code>model.layer[index].weight</code></p>
</blockquote>
</aside>
<p>Now, for this what you can do is.</p>
<aside class="quote no-group" data-username="Kunal_Dapse" data-post="7" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kunal_dapse/48/26998_2.png" class="avatar"> Kunal_Dapse:</div>
<blockquote>
<p>But I am trying to create the list of weights assigned which I couldn’t do it</p>
</blockquote>
</aside>
<p>You can append these weights in a list during training and access them later.</p>
<aside class="quote no-group" data-username="Kunal_Dapse" data-post="5" data-topic="90154">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kunal_dapse/48/26998_2.png" class="avatar"> Kunal_Dapse:</div>
<blockquote>
<p><code>            self.W += (torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)).t()</code></p>
</blockquote>
</aside>

Kunal_Dapse: <p>Thanks <a class="mention" href="/u/usama_hasan">@Usama_Hasan</a>, I really appreciate your help. My problem is solved <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"> <img src="https://discuss.pytorch.org/images/emoji/apple/innocent.png?v=9" title=":innocent:" class="emoji" alt=":innocent:"></p>

Usama_Hasan: <p>That’s good to hear. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>