我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.Dropout2d()。
def __init__(self): super(Model2, self).__init__() # fine tuning the ResNet helped significantly with the accuracy base_model = MyResNet(BasicBlock, [2, 2, 2, 2]) base_model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) # code needed to deactivate fine tuning of resnet #for param in base_model.parameters(): # param.requires_grad = False self.base_model = base_model self.drop0 = nn.Dropout2d(0.05) self.conv1 = nn.Conv2d(512, 256, 3, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(256) self.drop1 = nn.Dropout2d(0.05) self.conv2 = nn.Conv2d(256, 128, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(128) self.drop2 = nn.Dropout2d(0.05) self.conv3 = nn.Conv2d(128, 1+9, 3, padding=1, bias=False)
def __init__(self): """Init LeNet encoder.""" super(Generator, self).__init__() self.restored = False self.encoder = nn.Sequential( # 1st conv block # input [1 x 28 x 28] # output [64 x 12 x 12] nn.Conv2d(1, 64, 5, 1, 0, bias=False), nn.MaxPool2d(2), nn.ReLU(), # 2nd conv block # input [64 x 12 x 12] # output [50 x 4 x 4] nn.Conv2d(64, 50, 5, 1, 0, bias=False), nn.Dropout2d(), nn.MaxPool2d(2), nn.ReLU() ) self.fc1 = nn.Linear(50 * 4 * 4, 500)
def __init__(self): """Init LeNet encoder.""" super(LeNetEncoder, self).__init__() self.restored = False self.encoder = nn.Sequential( # 1st conv layer # input [1 x 28 x 28] # output [20 x 12 x 12] nn.Conv2d(1, 20, kernel_size=5), nn.MaxPool2d(kernel_size=2), nn.ReLU(), # 2nd conv layer # input [20 x 12 x 12] # output [50 x 4 x 4] nn.Conv2d(20, 50, kernel_size=5), nn.Dropout2d(), nn.MaxPool2d(kernel_size=2), nn.ReLU() ) self.fc1 = nn.Linear(50 * 4 * 4, 500)
def __init__(self, word_dim, num_words, char_dim, num_chars, num_filters, kernel_size, rnn_mode, hidden_size, num_layers, num_labels, tag_space=0, embedd_word=None, embedd_char=None, p_in=0.2, p_rnn=0.5): super(BiVarRecurrentConv, self).__init__(word_dim, num_words, char_dim, num_chars, num_filters, kernel_size, rnn_mode, hidden_size, num_layers, num_labels, tag_space=tag_space, embedd_word=embedd_word, embedd_char=embedd_char, p_in=p_in, p_rnn=p_rnn) self.dropout_in = None self.dropout_rnn = nn.Dropout2d(p_rnn) if rnn_mode == 'RNN': RNN = VarMaskedRNN elif rnn_mode == 'LSTM': RNN = VarMaskedLSTM elif rnn_mode == 'GRU': RNN = VarMaskedGRU else: raise ValueError('Unknown RNN mode: %s' % rnn_mode) self.rnn = RNN(word_dim + num_filters, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True, dropout=(p_in, p_rnn))
def __init__(self, internal_scale=None, use_relu=None, asymmetric=None, dilated=None, input_channels=None, output_channels=None, downsample=None, dropout_prob=None): super().__init__() internal_channels = output_channels // internal_scale input_stride = downsample and 2 or 1 self.__dict__.update(locals()) del self.self self.input_conv = nn.Conv2d(input_channels, internal_channels, input_stride, stride=input_stride, padding=0, bias=False) self.input_batch_norm = nn.BatchNorm2d(internal_channels, eps=1e-03) if asymmetric == True: self.middle_conv = nn.Conv2d( internal_channels, internal_channels, (5,1), stride=1, padding=(2,0), bias=True, dilation=1) self.middle_batch_norm = nn.BatchNorm2d(internal_channels, eps=1e-03) self.middle_conv = nn.Conv2d( internal_channels, internal_channels, (1,5), stride=1, padding=(0,2), bias=True, dilation=1) self.middle_batch_norm = nn.BatchNorm2d(internal_channels, eps=1e-03) else: self.middle_conv = nn.Conv2d( internal_channels, internal_channels, 3, stride=1, padding=dilated, bias=True, dilation=dilated) self.middle_batch_norm = nn.BatchNorm2d(internal_channels, eps=1e-03) self.output_conv = nn.Conv2d(internal_channels, output_channels, 1, stride=1, padding=0, bias=False) self.output_batch_norm = nn.BatchNorm2d(output_channels, eps=1e-03) self.dropout = nn.Dropout2d(dropout_prob)
def get_parameters(model, bias=False): import torch.nn as nn modules_skipped = ( nn.ReLU, nn.MaxPool2d, nn.Dropout2d, nn.Sequential, torchfcn.models.FCN32s, torchfcn.models.FCN16s, torchfcn.models.FCN8s, ) for m in model.modules(): if isinstance(m, nn.Conv2d): if bias: yield m.bias else: yield m.weight elif isinstance(m, nn.ConvTranspose2d): # weight is frozen because it is just a bilinear upsampling if bias: assert m.bias is None elif isinstance(m, modules_skipped): continue else: raise ValueError('Unexpected module: %s' % str(m))
def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10)
def __init__(self, block, layers, num_classes=2): super(cnnAlpha, self).__init__() self.in_channels = 8 self.conv = conv3x3(1, 8) self.bn = nn.BatchNorm2d(8) self.relu = nn.ReLU(inplace=True) self.layer1 = self.make_layer(block, 16, layers[0]) self.layer2 = self.make_layer(block, 32, layers[1]) self.layer3 = self.make_layer(block, 16, layers[2]) self.conv2 = conv3x3(16, 8) self.avg_pool = nn.AvgPool2d(8) self.fc = nn.Linear(288, num_classes) self.softmax = nn.Softmax() self.dropout = nn.Dropout2d(p=0.2)
def test_Dropout2d(self): b = random.randint(1, 5) w = random.randint(1, 5) h = random.randint(1, 5) num_features = 1000 input = torch.Tensor(num_features, b, w, h) self._test_dropout(nn.Dropout2d, input)
def __init__(self, in_channels, channels, kernel_size=3, padding=1, dropout=None): super(PreActivationBlock, self).__init__() self.bn = nn.BatchNorm2d(in_channels) self.conv = nn.Conv2d(in_channels, channels, kernel_size=kernel_size, stride=1, padding=padding, bias=False) self.dropout = dropout if dropout is not None: self.drop = nn.Dropout2d(p=dropout)
def __init__(self): super(Mnist, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) self.bn = nn.BatchNorm2d(20)
def __init__(self, n_channels=3, n_classes=1, dropout=0.0, bn=1, activation='relu'): super().__init__() self.n_channels = n_channels self.n_classes = n_classes self.bn = bn self.activation = activation self.dropout = dropout if dropout: self.dropout2d = nn.Dropout2d(p=dropout) else: self.dropout2d = lambda x: x
def test_invalid_dropout_p(self): v = Variable(torch.ones(1)) self.assertRaises(ValueError, lambda: nn.Dropout(-0.1)) self.assertRaises(ValueError, lambda: nn.Dropout(1.1)) self.assertRaises(ValueError, lambda: nn.Dropout2d(-0.1)) self.assertRaises(ValueError, lambda: nn.Dropout2d(1.1)) self.assertRaises(ValueError, lambda: nn.Dropout3d(-0.1)) self.assertRaises(ValueError, lambda: nn.Dropout3d(1.1)) self.assertRaises(ValueError, lambda: F.dropout(v, -0.1)) self.assertRaises(ValueError, lambda: F.dropout(v, 1.1))
def dropout(p=0.5, inplace=False, dim=2): #TODO: in the future some preprocessing goes here in_dim = dim if in_dim == 1: return nn.Dropout(p=p, inplace=inplace) elif in_dim == 2: return nn.Dropout2d(p=p, inplace=inplace) elif in_dim == 3: return nn.Dropout3d(p=p, inplace=inplace) # convolutional # Regular convolution
def __init__(self, hps: HyperParams): super().__init__() self.hps = hps if hps.dropout: self.dropout2d = nn.Dropout2d(p=hps.dropout) else: self.dropout2d = lambda x: x self.register_buffer('global_step', torch.IntTensor(1).zero_())
def __init__(self, in_, out, *, dropout, bn): super().__init__() self.bn = nn.BatchNorm2d(in_) if bn else None self.activation = nn.ReLU(inplace=True) self.conv = conv3x3(in_, out) self.dropout = nn.Dropout2d(p=dropout) if dropout else None
def __init__(self, in_, out, scale, *, dropout, bn): super().__init__() self.in_ = in_ self.bn = nn.BatchNorm2d(in_) if bn else None self.activation = nn.ReLU(inplace=True) self.conv = nn.Conv2d(in_, out, 1) self.dropout = nn.Dropout2d(p=dropout) if dropout else None self.pool = nn.MaxPool2d(scale, scale)
def __init__(self): super(resnet_modified_large, self).__init__() self.resnet = tv.models.resnet101(pretrained=True) #probably want linear, relu, dropout self.linear = nn.Linear(7*7*2048, 1024) self.dropout2d = nn.Dropout2d(.5) self.dropout = nn.Dropout(.5) self.relu = nn.LeakyReLU() initLinear(self.linear)
def __init__(self): super(resnet_modified_medium, self).__init__() self.resnet = tv.models.resnet50(pretrained=True) #probably want linear, relu, dropout self.linear = nn.Linear(7*7*2048, 1024) self.dropout2d = nn.Dropout2d(.5) self.dropout = nn.Dropout(.5) self.relu = nn.LeakyReLU() initLinear(self.linear)
def __init__(self): super(resnet_modified_small, self).__init__() self.resnet = tv.models.resnet34(pretrained=True) #probably want linear, relu, dropout self.linear = nn.Linear(7*7*512, 1024) self.dropout2d = nn.Dropout2d(.5) self.dropout = nn.Dropout(.5) self.relu = nn.LeakyReLU() initLinear(self.linear)
def __init__(self): super(Embedder, self).__init__() def identity(v): return lambda x: x bn2d = nn.InstanceNorm2d bn1d = identity self.nb_previous_images = 2 self.emb_sup_c1 = nn.Conv2d(512, 1024, kernel_size=3, padding=0, stride=1) self.emb_sup_c1_bn = bn2d(1024) self.emb_sup_c1_sd = nn.Dropout2d(0.0) self.emb_add_fc1 = nn.Linear( (self.nb_previous_images+1) # speeds + (self.nb_previous_images+1) # is_reverse + (self.nb_previous_images+1) # steering wheel + (self.nb_previous_images+1) # steering wheel raw + self.nb_previous_images*9, 128 ) self.emb_add_fc1_bn = bn1d(128) self.emb_fc1 = nn.Linear(1024*3 + 128, 512) self.emb_fc1_bn = bn1d(512) init_weights(self)
def __init__(self, word_dim, num_words, char_dim, num_chars, pos_dim, num_pos, num_filters, kernel_size, rnn_mode, hidden_size, num_layers, num_labels, arc_space, type_space, embedd_word=None, embedd_char=None, embedd_pos=None, p_in=0.2, p_out=0.5, p_rnn=(0.5, 0.5), biaffine=True): super(BiRecurrentConvBiAffine, self).__init__() self.word_embedd = Embedding(num_words, word_dim, init_embedding=embedd_word) self.char_embedd = Embedding(num_chars, char_dim, init_embedding=embedd_char) self.pos_embedd = Embedding(num_pos, pos_dim, init_embedding=embedd_pos) self.conv1d = nn.Conv1d(char_dim, num_filters, kernel_size, padding=kernel_size - 1) self.dropout_in = nn.Dropout2d(p=p_in) self.dropout_out = nn.Dropout2d(p=p_out) self.num_labels = num_labels if rnn_mode == 'RNN': RNN = VarMaskedRNN elif rnn_mode == 'LSTM': RNN = VarMaskedLSTM elif rnn_mode == 'FastLSTM': RNN = VarMaskedFastLSTM elif rnn_mode == 'GRU': RNN = VarMaskedGRU else: raise ValueError('Unknown RNN mode: %s' % rnn_mode) self.rnn = RNN(word_dim + num_filters + pos_dim, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True, dropout=p_rnn) out_dim = hidden_size * 2 self.arc_h = nn.Linear(out_dim, arc_space) self.arc_c = nn.Linear(out_dim, arc_space) self.attention = BiAAttention(arc_space, arc_space, 1, biaffine=biaffine) self.type_h = nn.Linear(out_dim, type_space) self.type_c = nn.Linear(out_dim, type_space) self.bilinear = BiLinear(type_space, type_space, self.num_labels) self.logsoftmax = nn.LogSoftmax()
def blockUNet(in_c, out_c, name, transposed=False, bn=True, relu=True, dropout=False): block = nn.Sequential() if relu: block.add_module('%s.relu' % name, nn.ReLU(inplace=True)) else: block.add_module('%s.leakyrelu' % name, nn.LeakyReLU(0.2, inplace=True)) if not transposed: block.add_module('%s.conv' % name, nn.Conv2d(in_c, out_c, 4, 2, 1, bias=False)) else: block.add_module('%s.tconv' % name, nn.ConvTranspose2d(in_c, out_c, 4, 2, 1, bias=False)) if bn: block.add_module('%s.bn' % name, nn.BatchNorm2d(out_c)) if dropout: block.add_module('%s.dropout' % name, nn.Dropout2d(0.5, inplace=True)) return block
def __init__(self, dim, n_out, kernel_size=3, stride=1, padding=None, batchnorm=True, activation=True, pool=None, dropout=0.0, verbose=False): super(ConvBlock, self).__init__() if padding is None: padding = kernel_size // 2 n_in = int(dim[0]) self.conv2d = nn.Conv2d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=padding) self.batchnorm = nn.BatchNorm2d(n_out) if batchnorm else None self.activation = nn.ReLU(inplace=True) if activation else None self.dropout = nn.Dropout2d(dropout) if dropout > 0.0 else None # Output dimensions just from convolution dim[0] = n_out dim[1:] = 1 + np.floor((dim[1:] + padding * 2 - kernel_size) / stride) # Add a pooling block, which might further change dim self.pool = pool if pool is None else PoolBlock(dim, pool, 2, verbose=verbose) # Number of params self.n_params = n_out * (n_in * kernel_size * kernel_size + (3 if batchnorm else 1)) if verbose: print("Conv2d in %3i out %3i h %3i w %3i k %i s %i params %9i" % (n_in, *dim, kernel_size, stride, self.n_params))
def __init__(self, in_channels, out_channels, **kwargs): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) self.bn = nn.BatchNorm2d(out_channels, eps=0.001) self.dropout = nn.Dropout2d()
def __init__(self, inplanes, planes, stride=1, downsample=None): super(MyBasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.dropout = nn.Dropout2d() self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride
def __init__(self, inplanes, planes, stride=1, downsample=None): super(MyBottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) self.dropout = nn.Dropout2d() self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) # Spatial transformer localization-network self.localization = nn.Sequential( nn.Conv2d(1, 8, kernel_size=7), nn.MaxPool2d(2, stride=2), nn.ReLU(True), nn.Conv2d(8, 10, kernel_size=5), nn.MaxPool2d(2, stride=2), nn.ReLU(True) ) # Regressor for the 3 * 2 affine matrix self.fc_loc = nn.Sequential( nn.Linear(10 * 3 * 3, 32), nn.ReLU(True), nn.Linear(32, 3 * 2) ) # Initialize the weights/bias with identity transformation self.fc_loc[2].weight.data.fill_(0) self.fc_loc[2].bias.data = torch.FloatTensor([1, 0, 0, 0, 1, 0]) # Spatial transformer network forward function
def __init__(self, n_classes=21, learned_billinear=False): super(fcn32s, self).__init__() self.learned_billinear = learned_billinear self.n_classes = n_classes self.conv_block1 = nn.Sequential( nn.Conv2d(3, 64, 3, padding=100), nn.ReLU(inplace=True), nn.Conv2d(64, 64, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2, stride=2, ceil_mode=True),) self.conv_block2 = nn.Sequential( nn.Conv2d(64, 128, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(128, 128, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2, stride=2, ceil_mode=True),) self.conv_block3 = nn.Sequential( nn.Conv2d(128, 256, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2, stride=2, ceil_mode=True),) self.conv_block4 = nn.Sequential( nn.Conv2d(256, 512, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2, stride=2, ceil_mode=True),) self.conv_block5 = nn.Sequential( nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2, stride=2, ceil_mode=True),) self.classifier = nn.Sequential( nn.Conv2d(512, 4096, 7), nn.ReLU(inplace=True), nn.Dropout2d(), nn.Conv2d(4096, 4096, 1), nn.ReLU(inplace=True), nn.Dropout2d(), nn.Conv2d(4096, self.n_classes, 1),) # TODO: Add support for learned upsampling if self.learned_billinear: raise NotImplementedError # upscore = nn.ConvTranspose2d(self.n_classes, self.n_classes, 64, stride=32, bias=False) # upscore.scale_factor = None