<jupyter_start><jupyter_text>1D卷积  ---等效于CNN做文本分类 ![](http://op4a94iq8.bkt.clouddn.com/18-7-13/49813055.jpg)<jupyter_code># --------------------------- 1D 卷积 实例 ---------------------------------
import torch 
import torch.nn as nn
import torch.nn.functional as F

m = nn.Conv1d(2, 3, 4, stride=2,padding=0,groups=1,dilation=1)

input = torch.randn(5, 2, 10)
print("input size:",input.shape)

output = m(input)
print("output size:",output.shape)

print("filter size:",m.weight.shape)
print(m.weight.type)

# pool of non-square window
input = torch.randn(1, 512, 14, 14) # batch_size=1
print("input size:",input.shape)
print(input.type)

m = nn.AvgPool2d(14)

output = m(input)
print("output size:",output.shape)

avg=output.view(output.shape[0],1,output.shape[1])
print("avg size:",avg.shape)

one_conv_filter = nn.Conv1d(1, 1024, 3, stride=1,padding=1,groups=1,dilation=1)

out_1 = one_conv_filter(avg)
print("out_1 size:",out_1.shape)
print("filter size:",one_conv_filter.weight.shape)


# --------------------- 加权融合 -----------------------


# 不考虑batchsize时实现了1x1卷积
y=torch.Tensor(
    [
       [[[0.1]],[[0.7]],[[3]],],
       [[[0.2]],[[0.4]],[[10]],]
    ])

print("Y size:",y.shape)

x=torch.Tensor(
    [
        [[1,1,],[1,1,],],
        
        [[2,2,],[2,2,],],
        
        [[3,3,],[3,3,],],
    ])

print("X size:",x.shape)

out=x*y
print(out.shape)
print(out[1]) 

# ----------------- 聚合 ----------
res=out.sum(dim=1)
print(res.shape)
print(res)<jupyter_output>Y size: torch.Size([2, 3, 1, 1])
X size: torch.Size([3, 2, 2])
torch.Size([2, 3, 2, 2])
tensor([[[  0.2000,   0.2000],
         [  0.2000,   0.2000]],

        [[  0.8000,   0.8000],
         [  0.8000,   0.8000]],

        [[ 30.0000,  30.0000],
         [ 30.0000,  30.0000]]])
torch.Size([2, 2, 2])
tensor([[[ 10.5000,  10.5000],
         [ 10.5000,  10.5000]],

        [[ 31.0000,  31.0000],
         [ 31.0000,  31.0000]]])<jupyter_text>改造版本<jupyter_code># 32  缩放5次到 1x1@1024 
# From https://github.com/kuangliu/pytorch-cifar 
import torch
import torch.nn as nn
import torch.nn.functional as F

def Attention(X):
    # [bs,in_Channel,in_H,in_W]
    m = nn.AvgPool2d(X.shape[-1])  #最好在初始化层定义好
    # [bs,in_Channel,1,1]
    out=m(X)
    out=out.view(out.shape[0],1,output.shape[1])
    
    one_conv_filter = nn.Conv1d(1, out_channel, 3, stride=1,padding=1,groups=1,dilation=1)

    out = one_conv_filter(out)
    
    

class Block(nn.Module):
    '''Depthwise conv + Pointwise conv'''
    def __init__(self, in_planes, out_planes, stride=1):
        super(Block, self).__init__()
        
        # 分组卷积数=输入通道数
        self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False)
        
        self.bn1 = nn.BatchNorm2d(in_planes)
        
        
        self.conv2 = 
        
        self.bn2 = nn.BatchNorm2d(out_planes)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        
        
        out = F.relu(self.bn2(self.conv2(out)))
        
        
        return out


class MobileNet(nn.Module):
    # (128,2) means conv planes=128, conv stride=2, by default conv stride=1
    cfg = [64, (128,2), 128, (256,2), 256, (512,2), 512, 512, 512, 512, 512, (1024,2), 1024]

    def __init__(self, num_classes=10):
        super(MobileNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
        
        self.bn1 = nn.BatchNorm2d(32)
        self.layers = self._make_layers(in_planes=32) # 自动化构建层
        self.linear = nn.Linear(1024, num_classes)

    def _make_layers(self, in_planes):
        layers = []
        for x in self.cfg:
            out_planes = x if isinstance(x, int) else x[0]
            stride = 1 if isinstance(x, int) else x[1]
            layers.append(Block(in_planes, out_planes, stride))
            in_planes = out_planes
        return nn.Sequential(*layers)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.layers(out)
        out = F.avg_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = self.linear(out)
        return out<jupyter_output><empty_output><jupyter_text>原来版本<jupyter_code># 32  缩放5次到 1x1@1024 
# From https://github.com/kuangliu/pytorch-cifar 
import torch
import torch.nn as nn
import torch.nn.functional as F


class Block(nn.Module):
    '''Depthwise conv + Pointwise conv'''
    def __init__(self, in_planes, out_planes, stride=1):
        super(Block, self).__init__()
        
        # 分组卷积数=输入通道数
        self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False)
        
        self.bn1 = nn.BatchNorm2d(in_planes)
        
        
        self.conv2 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
        
        self.bn2 = nn.BatchNorm2d(out_planes)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        return out


class MobileNet(nn.Module):
    # (128,2) means conv planes=128, conv stride=2, by default conv stride=1
    cfg = [64, (128,2), 128, (256,2), 256, (512,2), 512, 512, 512, 512, 512, (1024,2), 1024]

    def __init__(self, num_classes=10):
        super(MobileNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
        
        self.bn1 = nn.BatchNorm2d(32)
        self.layers = self._make_layers(in_planes=32) # 自动化构建层
        self.linear = nn.Linear(1024, num_classes)

    def _make_layers(self, in_planes):
        layers = []
        for x in self.cfg:
            out_planes = x if isinstance(x, int) else x[0]
            stride = 1 if isinstance(x, int) else x[1]
            layers.append(Block(in_planes, out_planes, stride))
            in_planes = out_planes
        return nn.Sequential(*layers)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.layers(out)
        out = F.avg_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = self.linear(out)
        return out<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17887896019496344, 'fraction_numerical': 0.07164906580016248, 'mean_word_length': 2.517714285714286, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '312100', 'license': 'Apache-2.0', 'n_tokens_mistral': 2660, 'n_tokens_neox': 2408, 'n_words': 434, 'path': 'Main_02.ipynb', 'repo_name': 'JYJatBUPT/More_Compact_MobileNet', 'size': '6155'}