Type of x.size(1)

Hi,

x is a torch.Tensor and I define c=x.size(1).

I found that there are two possible types of c:

  • <class 'torch.Tensor'>
  • <class 'int'>

How can I get my desired type <class 'int'> rather than <class 'torch.Tensor'>?

if

x = torch.randn(5, 5)

then

type(x.size(1))

would return int, how do you get torch.Tensor?

Thank you for your reply, but I do get the torch.Tensor.

Here is part of my code:

    def forward(self, x):
        # Explicitly forward every layer.
        # Branch2a.
        in_channel = x.size(1)
        l_size = list(x.size())
        import torch
        fake = torch.ones(3,4,5).cuda()
        type(fake.size(2))
        import pdb; pdb.set_trace()

it might be for x, for example,

class A(nn.Module):
  def __init__(self):
    super().__init__()
    self.a = 10
  def forward(self, x):
    print('x type:', type(x))
    # Explicitly forward every layer.
    # Branch2a.
    in_channel = x.size(1)
    print('in_channel type:', type(in_channel))
    l_size = list(x.size())
    print('l_size type:', type(l_size))
    import torch
    fake = torch.ones(3,4,5).cuda()
    print('fake type:', type(fake.size(2)))
    # import pdb; pdb.set_trace()
    return x
net = A()
net(torch.randn(1, 2, 3))

would return

x type: <class 'torch.Tensor'>
in_channel type: <class 'int'>
l_size type: <class 'list'>
fake type: <class 'int'>
tensor([[[-1.2244, -1.2652, -0.6877],
         [ 1.5777, -1.5478,  1.1681]]])

Thanks very much for your kind reply.

After debugging, I found my problem is resulted from the fvcore.nn.flop_count.flop_count.

During the forward in above function, the type will be <class 'torch.Tensor'>