Cannot assign FloatTensor as Parameter

CopyOfA: <p>I am writing a class for a restricted boltzmann machine using PyTorch. I am using the weights and the biases within a call to LBFGS optimizer, so I’d like to have all the weights and biases as Parameters. However, when I try to initialize them as cuda tensors, I am getting the following error:</p>
<pre><code class="lang-auto">TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'W' (torch.nn.Parameter or None expected) 
</code></pre>
<p>Here is my code:</p>
<pre><code class="lang-auto">class RBM(nn.Module):
    
    def __init__(self,
                    visible_units   = 256,
                    hidden_units    = 64,
                    epsilonw        = 0.1, #learning rate for weights
                    epsilonvb       = 0.1, #learning rate for visible unit biases
                    epsilonhb       = 0.1, #learning rate for hidden unit biases
                    weightcost      = 0.0002,
                    initialmomentum = 0.5,
                    finalmomentum   = 0.9,
                    use_gpu = False
                    ):
        super(RBM,self).__init__()
        
        self.desc               = "RBM"
        self.visible_units      = visible_units
        self.hidden_units       = hidden_units
        self.epsilonw           = epsilonw
        self.epsilonvb          = epsilonvb
        self.epsilonhb          = epsilonhb
        self.weightcost         = weightcost
        self.initialmomentum    = initialmomentum
        self.finalmomentum      = finalmomentum
        self.use_gpu            = use_gpu
        self.device             = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        
        self.h_bias = nn.Parameter(torch.zeros(self.hidden_units)) #hidden layer bias
        self.v_bias = nn.Parameter(torch.zeros(self.visible_units)) #visible layer bias
           
        if self.use_gpu:
            self.W      = nn.Parameter(0.1*torch.randn(self.visible_units, self.hidden_units), 
                                       device=self.device)
            self.v_bias = nn.Parameter(torch.zeros(self.hidden_units),
                                       device=self.device)
            self.h_bias = nn.Parameter(torch.zeros(self.visible_units),
                                       device=self.device)
        else:
            self.W      = nn.Parameter(0.1*torch.randn(self.visible_units, self.hidden_units))
            self.h_bias = nn.Parameter(torch.zeros(self.hidden_units)) #hidden layer bias
            self.v_bias = nn.Parameter(torch.zeros(self.visible_units)) #visible layer bias
</code></pre>
<p>The full traceback for the call is:</p>
<pre><code class="lang-auto">from RBM import RBM
rbm = RBM(use_gpu=1)
Traceback (most recent call last):  
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
File "/home/deep_autoencoder/RBM.py", line 54, in __init__                                                   
    self.W = nn.Parameter(0.1*torch.randn(self.visible_units, self.hidden_units),
File "/home/anaconda3/envs/torch_env/lib/python3.8/site-packages/torch/nn/modules/module.py", line 792, in __setattr__                          
    raise TypeError("cannot assign '{}' as parameter '{}' "  
TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'W' (torch.nn.Parameter or None expected) 
</code></pre>
<p>I’m not sure what I’m doing wrong here. The code I’ve written seems to reflect the best advice I’ve seen in this forum.</p>

albanD: <p>Hi,</p>
<p>As the message mentions, it should be either a Parameter or None.<br>
So you most likely want to wrap your Tensor into a Parameter before setting it?</p>