简体   繁体   中英

torch.nn.conv2d does not give the same result as torch.nn.functional.conv2d

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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM