PyTorch - "Attribute Error: module 'torch' has no attribute 'float'
I'm trying to implement the Spatial Transformer Network from here and I am running into this issue:

class STNLayer(torch.nn.Module):
    def __init__(self, input_size):
        super(STNLayer, self).__init__()
        self.input_size = input_size
        self.localization = nn.Sequential(
            nn.Conv2d(self.input_size, 8, kernel_size = 7),
            nn.MaxPool2d(2, stride = 2),
            nn.ReLU(True),
            nn.Conv2d(8, 10, kernel_size = 5),
            nn.MaxPool2d(2, stride = 2),
            nn.ReLU(True)
        )

        self.fc_loc = nn.Sequential(
            nn.Linear(10 * 12 * 12, 32),
            nn.ReLU(True),
            #nn.BatchNorm1d(32),
            nn.Linear(32, 3*2)
        )

        # Initialize weights to identity transformation
        self.fc_loc[2].weight.data.zero_()
        self.fc_loc[2].bias.data = torch.cuda.FloatTensor([1,0,0,0,1,0])


The line 

self.fc_loc[2].bias.data = torch.cuda.FloatTensor([1,0,0,0,1,0])


is giving the error:

*** AttributeError: module 'torch' has no attribute 'float'


How do I fix this?


About the AttributeError

AttributeError: module 'torch' has no attribute 'float'


This AttributeError implies that somewhere in the code must be something like torch.float.

In your code example I cannot find anything like it. 
However, the link you referenced for the code contains the following line:

self.fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))


PyTorch data types like torch.float came with PyTorch 0.4.0, so when you use something like torch.float in earlier versions like 0.3.1 you will see this error, because torch then actually has no attribute float. 

If you have a line like in the example you've linked, it makes perfectly sense to get an error like this. For the code you've posted it makes no sense.

As you did not include a full error traceback I can only conjecture what the problem is. So probably you either have somewhere used torch.float in your code or you have imported some code with torch.float.
So what can you do to fix it?

Easiest way would be just updating PyTorch to 0.4.0 or higher. 

If you don't want to update or if you are not able to do so for some reason. You just need to find the line (or lines) where torch.float is used and change it. 

So probably something like this:

torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float)


and change it to:

torch.FloatTensor([1,0,0,0,1,0])


Similarly to the line you posted in your question.

I hope this helps!





Short Add-on for Jupyter Notebook:

This is just a side node, because your code and error message do not match: When importing code to Jupyter Notebook it is safest to restart the kernel after doing changes to the imported code. Otherwise already loaded modules are omitted during import and changes are not applied. So if there was an error in the old code this error might still occur and the traceback then points to the line you have just corrected.

So for example when changing in the imported code: torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float) to torch.FloatTensor([1,0,0,0,1,0]) it might still complain about torch.float even if the line then doesn't contain a torch.floatanymore (it even shows the new code in the traceback). This is kind of confusing because the traceback then shows an error which doesn't make sense for the given line. 

In such a case restarting the kernel helps. 

