Reputation: 545
I am trying to learn PyTorch and have tried running some code that I got from the Kaggle website.
# Get all hidden layers' weights
for i in range(len(hidden_units)):
fc_layers.extend([
TrainNet.model.hidden_layers[i].weight.T.tolist(), # weights
TrainNet.model.hidden_layers[i].bias.tolist() # bias
])
This gives the following error:
AttributeError Traceback (most recent call last)
<ipython-input-12-65f871b6f0b7> in <module>
4 for i in range(len(hidden_units)):
5 fc_layers.extend([
----> 6 TrainNet.model.hidden_layers[i].weight.T.tolist(), # weights
7 TrainNet.model.hidden_layers[i].bias.tolist() # bias
8 ])
AttributeError: 'Parameter' object has no attribute 'T'
If I print out the type of 'TrainNet.model.hidden_layers[i].weight' it is indeed type Parameter. This block of code works without error on the Kaggle website run in there notebook (Google colab I believe) where the version of Torch is 1.3.0.
On my home machine where the error occurs my Anaconda distribution which I have just updated is running Torch 1.1.0.
Is this the source of the error and how do I sort it?
Thanks
Upvotes: 0
Views: 81
Reputation: 1518
class Parameter
is a subclass of Tensor hence has all the attributes of a torch.Tensor
class
Tensor.T
was introduced in 1.2.0 and hence is not available in your 1.1.0
You can either update the pytorch version or use the permute method in it's place as below example
>>> t = torch.Tensor(np.random.randint(0,100,size=(2,3,4))) # -> random Tensor with t.shape = (2,3,4)
>>> t.shape
torch.Size([2, 3, 4])
>>> list(range(len(t.shape)))[::-1]
[2,1,0] # This is the sequence of dimensions t.T will return in 1.2.0 onwards
>>> t = t.permute(list(range(len(t.shape)))[::-1])
>>> t.shape
torch.Size([4, 3, 2])
It is equivalent to doing transpose of a matrix, i.e., reversing the sequence of dimensions but in N dimensional tensors
Upvotes: 2