Reputation: 89
This is my code:
l1 = nn.Conv2d(3, 2, kernel_size=3, stride=2).double() #Layer
l1wt = l1.weight.data #filter
inputs = np.random.rand(3, 3, 5, 5) #input
it = torch.from_numpy(inputs) #input tensor
output1 = l1(it) #output
output2 = torch.nn.functional.conv2d(it, l1wt, stride=2) #output
print(output1)
print(output2)
I would expect to get the same result for output1 and output2, but they are not. Am I doing something wrong are do nn and nn.functional work different?
Upvotes: 1
Views: 1302
Reputation: 476
As mentioned by @Coolness, the bias is off by default in functional version.
Documentation Reference: https://pytorch.org/docs/stable/nn.html#conv2d https://pytorch.org/docs/stable/nn.functional.html#conv2d
import torch
from torch import nn
import numpy as np
# Bias Off
l1 = nn.Conv2d(3, 2, kernel_size=3, stride=1, bias=False).double() #Layer
l1wt = l1.weight.data #filter
inputs = np.random.rand(3, 3, 5, 5) #input
it = torch.from_numpy(inputs) #input tensor
it1 = it.clone()
output1 = l1(it) #output
output2 = torch.nn.functional.conv2d(it, l1wt, stride=1) #output
print(torch.equal(it, it1))
print(output1)
print(output2)
Upvotes: 1
Reputation: 1982
I think you forgot the bias.
inp = torch.rand(3,3,5,5)
a = nn.Conv2d(3,2,3,stride=2)
a(inp)
nn.functional.conv2d(inp, a.weight.data, bias=a.bias.data)
Looks the same to me
Upvotes: 1