1 ------------ nn ------------
import tensorflow as tf
import torch.nn.functional as F
tf.nn.Conv2d --> tensor
F.conv2d --> tensor
这两个大哥是等价的,不还有不同,在保持结果时候。F只是个函数,看源码,但是还有问题没解决。
看下面
import torch.nn as nn
import torch.nn.functional as F
import torch
from torch.autograd import Variable
class LeNet_bck(nn.Module):
def __init__(self):
super(LeNet_bck, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
out = self.fc3(x)
return out
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.c_w1 = torch.randn(6, 3, 5, 5)
self.c_w2 = torch.randn(16, 6, 5, 5)
self.fc_w1 = torch.randn(120,400)
self.fc_b1 = torch.randn(120)
self.fc_w2 = torch.randn(84, 120)
self.fc_b2 = torch.randn(84)
self.fc_w3 = torch.randn(10, 84)
self.fc_b3 = torch.randn(10)
def forward(self, x):
x = F.relu(F.conv2d(x,self.c_w1,stride=1))
x = F.max_pool2d(x, 2)
x = F.relu(F.conv2d(x,self.c_w2,stride=1))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(F.linear(x,self.fc_w1,self.fc_b1))
x = F.relu(F.linear(x,self.fc_w2,self.fc_b2))
out = F.linear(x,self.fc_w3,self.fc_b3)
return out
if __name__ == '__main__':
net = LeNet_bck()
print(net)
x = Variable(torch.FloatTensor(8, 3, 32,32))
print(net(x))
net = LeNet()
print(net)
x = Variable(torch.FloatTensor(8, 3, 32, 32))
print(net(x))
nn和nn.f源码对比。
# --------------torch.nn.Conv2d
import torch.nn.functional as F
class Conv2d(_ConvNd):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
kernel_size = _pair(kernel_size)
stride = _pair(stride)
padding = _pair(padding)
dilation = _pair(dilation)
super(Conv2d, self).__init__(
in_channels, out_channels, kernel_size, stride, padding, dilation,
False, _pair(0), groups, bias)
def forward(self, input):
return F.conv2d(input, self.weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
# -------------- torch.nn.functional.conv2d
def conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1,
groups=1):
if input is not None and input.dim() != 4:
raise ValueError("Expected 4D tensor as input, got {}D tensor instead.".format(input.dim()))
f = _ConvNd(_pair(stride), _pair(padding), _pair(dilation), False,
_pair(0), groups, torch.backends.cudnn.benchmark,
torch.backends.cudnn.deterministic, torch.backends.cudnn.enabled)
return f(input, weight, bias)