Python torch.nn.Dropout() Examples
The following are 30
code examples of torch.nn.Dropout().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn
, or try the search function
.
Example #1
Source File: encoder.py From hgraph2graph with MIT License | 9 votes |
def __init__(self, rnn_type, input_size, node_fdim, hidden_size, depth, dropout): super(MPNEncoder, self).__init__() self.hidden_size = hidden_size self.input_size = input_size self.depth = depth self.W_o = nn.Sequential( nn.Linear(node_fdim + hidden_size, hidden_size), nn.ReLU(), nn.Dropout(dropout) ) if rnn_type == 'GRU': self.rnn = GRU(input_size, hidden_size, depth) elif rnn_type == 'LSTM': self.rnn = LSTM(input_size, hidden_size, depth) else: raise ValueError('unsupported rnn cell type ' + rnn_type)
Example #2
Source File: model_architecture.py From models with MIT License | 8 votes |
def get_model(load_weights = True): deepsea_cpu = nn.Sequential( # Sequential, nn.Conv2d(4,320,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.MaxPool2d((1, 4),(1, 4)), nn.Dropout(0.2), nn.Conv2d(320,480,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.MaxPool2d((1, 4),(1, 4)), nn.Dropout(0.2), nn.Conv2d(480,960,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.Dropout(0.5), Lambda(lambda x: x.view(x.size(0),-1)), # Reshape, nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(50880,925)), # Linear, nn.Threshold(0, 1e-06), nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(925,919)), # Linear, nn.Sigmoid(), ) if load_weights: deepsea_cpu.load_state_dict(torch.load('model_files/deepsea_cpu.pth')) return nn.Sequential(ReCodeAlphabet(), deepsea_cpu)
Example #3
Source File: 22_vgg.py From deep-learning-note with MIT License | 7 votes |
def vgg(conv_arch, fc_features, fc_hidden_units=4096): net = nn.Sequential() # 卷积层部分 for i, (num_convs, in_channels, out_channels) in enumerate(conv_arch): # 每经过一个 vgg_block 宽高减半 net.add_module('vgg_block_' + str(i+1), vgg_block(num_convs, in_channels, out_channels)) # 全连接部分 net.add_module('fc', nn.Sequential( utils.FlattenLayer(), nn.Linear(fc_features, fc_hidden_units), nn.ReLU(), nn.Dropout(0.5), nn.Linear(fc_hidden_units, fc_hidden_units), nn.ReLU(), nn.Dropout(0.5), nn.Linear(fc_hidden_units, 10) )) return net
Example #4
Source File: finetune.py From transferlearning with MIT License | 7 votes |
def __init__(self): super(DANNet, self).__init__() model = models.vgg16(pretrained=True) #False self.features = model.features for param in self.features.parameters(): #NOTE: prune:True // finetune:False param.requires_grad = True self.classifier = nn.Sequential( nn.Dropout(), nn.Linear(25088, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplace=True), ) self.cls_fc = nn.Linear(4096, 31)
Example #5
Source File: BiLSTM.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 7 votes |
def __init__(self, **kwargs): super(BiLSTM, self).__init__() for k in kwargs: self.__setattr__(k, kwargs[k]) V = self.embed_num D = self.embed_dim C = self.label_num paddingId = self.paddingId self.embed = nn.Embedding(V, D, padding_idx=paddingId) if self.pretrained_embed: self.embed.weight.data.copy_(self.pretrained_weight) else: init_embedding(self.embed.weight) self.dropout_embed = nn.Dropout(self.dropout_emb) self.dropout = nn.Dropout(self.dropout) self.bilstm = nn.LSTM(input_size=D, hidden_size=self.lstm_hiddens, num_layers=self.lstm_layers, bidirectional=True, batch_first=True, bias=True) self.linear = nn.Linear(in_features=self.lstm_hiddens * 2, out_features=C, bias=True) init_linear(self.linear)
Example #6
Source File: model.py From models with MIT License | 6 votes |
def __init__( self ): super(CNN, self).__init__() self.elmo_feature_extractor = nn.Sequential( nn.Conv2d( 1024, 32, kernel_size=(7,1), padding=(3,0) ), nn.ReLU(), nn.Dropout( 0.25 ), ) n_final_in = 32 self.dssp3_classifier = nn.Sequential( nn.Conv2d( n_final_in, 3, kernel_size=(7,1), padding=(3,0)) ) self.dssp8_classifier = nn.Sequential( nn.Conv2d( n_final_in, 8, kernel_size=(7,1), padding=(3,0)) ) self.diso_classifier = nn.Sequential( nn.Conv2d( n_final_in, 2, kernel_size=(7,1), padding=(3,0)) )
Example #7
Source File: model.py From VSE-C with MIT License | 6 votes |
def __init__(self, vocab_size, word_dim, embed_size, use_abs=False, glove_path='data/glove.pkl'): super(EncoderTextDeepCNN, self).__init__() self.use_abs = use_abs self.embed_size = embed_size # word embedding self.embed = nn.Embedding(vocab_size, word_dim-300, padding_idx=0) _, embed_weight = pickle.load(open(glove_path, 'rb')) self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False) channel_num = embed_size self.conv1 = nn.Conv1d(word_dim, embed_size, 2, stride=2) # [batch_size, dim, 30] self.conv2 = nn.Conv1d(embed_size, embed_size, 4, stride=2) # [batch_size, dim, 14] self.conv3 = nn.Conv1d(embed_size, embed_size, 5, stride=2) # [batch_size, dim, 5] self.conv4 = nn.Conv1d(embed_size, channel_num, 5) self.drop = nn.Dropout(p=0.5) self.relu = nn.ReLU() # self.mlp = nn.Linear(embed_size, embed_size) self.init_weights()
Example #8
Source File: model.py From VSE-C with MIT License | 6 votes |
def __init__(self, vocab_size, word_dim, embed_size, use_abs=False, glove_path='data/glove.pkl'): super(EncoderTextCNN, self).__init__() self.use_abs = use_abs self.embed_size = embed_size # word embedding self.embed = nn.Embedding(vocab_size, word_dim-300, padding_idx=0) # 0 for <pad> _, embed_weight = pickle.load(open(glove_path, 'rb')) self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False) channel_num = embed_size // 4 self.conv2 = nn.Conv1d(word_dim, channel_num, 2) self.conv3 = nn.Conv1d(word_dim, channel_num, 3) self.conv4 = nn.Conv1d(word_dim, channel_num, 4) self.conv5 = nn.Conv1d(word_dim, channel_num, 5) self.drop = nn.Dropout(p=0.5) self.relu = nn.ReLU() # self.mlp = nn.Linear(embed_size, embed_size) self.init_weights()
Example #9
Source File: softmax_nn.py From OpenNRE with MIT License | 6 votes |
def __init__(self, sentence_encoder, num_class, rel2id): """ Args: sentence_encoder: encoder for sentences num_class: number of classes id2rel: dictionary of id -> relation name mapping """ super().__init__() self.sentence_encoder = sentence_encoder self.num_class = num_class self.fc = nn.Linear(self.sentence_encoder.hidden_size, num_class) self.softmax = nn.Softmax(-1) self.rel2id = rel2id self.id2rel = {} self.drop = nn.Dropout() for rel, id in rel2id.items(): self.id2rel[id] = rel
Example #10
Source File: bag_attention.py From OpenNRE with MIT License | 6 votes |
def __init__(self, sentence_encoder, num_class, rel2id): """ Args: sentence_encoder: encoder for sentences num_class: number of classes id2rel: dictionary of id -> relation name mapping """ super().__init__() self.sentence_encoder = sentence_encoder self.num_class = num_class self.fc = nn.Linear(self.sentence_encoder.hidden_size, num_class) self.softmax = nn.Softmax(-1) self.rel2id = rel2id self.id2rel = {} self.drop = nn.Dropout() for rel, id in rel2id.items(): self.id2rel[id] = rel
Example #11
Source File: learnedgroupconv.py From Pytorch-Project-Template with MIT License | 6 votes |
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, condense_factor=None, dropout_rate=0.): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.condense_factor = condense_factor self.groups = groups self.dropout_rate = dropout_rate # Check if given configs are valid assert self.in_channels % self.groups == 0, "group value is not divisible by input channels" assert self.in_channels % self.condense_factor == 0, "condensation factor is not divisible by input channels" assert self.out_channels % self.groups == 0, "group value is not divisible by output channels" self.batch_norm = nn.BatchNorm2d(in_channels) self.relu = nn.ReLU(inplace=True) if self.dropout_rate > 0: self.dropout = nn.Dropout(self.dropout_rate, inplace=False) self.conv = nn.Conv2d(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=1, bias=False) # register conv buffers self.register_buffer('_count', torch.zeros(1)) self.register_buffer('_stage', torch.zeros(1)) self.register_buffer('_mask', torch.ones(self.conv.weight.size()))
Example #12
Source File: resnet_one_tower_baseline.py From dogTorch with MIT License | 6 votes |
def __init__(self, args): super(ResNet18Image2IMUOneTower, self).__init__() assert args.sequence_length == 1, "ResNet18Image2IMU supports seq-len=1" self.class_weights = args.dataset.CLASS_WEIGHTS[torch.LongTensor( args.imus)] resnet_model = torchvision_resnet18(pretrained=args.pretrain) # Remove the last fully connected layer. del resnet_model.fc self.resnet = resnet_model self.resnet.conv1 = nn.Conv2d(6, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) num_features = 512 num_frames = 1 num_classes = args.num_classes # Make num_imu fc layers self.imus = args.imus for i in self.imus: setattr(self, 'imu{}'.format(i), nn.Linear(num_frames * num_features, num_classes)) self.dropout = nn.Dropout()
Example #13
Source File: encoder.py From hgraph2graph with MIT License | 6 votes |
def __init__(self, rnn_type, input_size, node_fdim, hidden_size, depth, dropout): super(MPNEncoder, self).__init__() self.hidden_size = hidden_size self.input_size = input_size self.depth = depth self.W_o = nn.Sequential( nn.Linear(node_fdim + hidden_size, hidden_size), nn.ReLU(), nn.Dropout(dropout) ) if rnn_type == 'GRU': self.rnn = GRU(input_size, hidden_size, depth) elif rnn_type == 'LSTM': self.rnn = LSTM(input_size, hidden_size, depth) else: raise ValueError('unsupported rnn cell type ' + rnn_type)
Example #14
Source File: model_architecture.py From models with MIT License | 6 votes |
def get_seqpred_model(load_weights = True): deepsea_cpu = nn.Sequential( # Sequential, nn.Conv2d(4,320,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.MaxPool2d((1, 4),(1, 4)), nn.Dropout(0.2), nn.Conv2d(320,480,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.MaxPool2d((1, 4),(1, 4)), nn.Dropout(0.2), nn.Conv2d(480,960,(1, 8),(1, 1)), nn.Threshold(0, 1e-06), nn.Dropout(0.5), Lambda(lambda x: x.view(x.size(0),-1)), # Reshape, nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(50880,925)), # Linear, nn.Threshold(0, 1e-06), nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(925,919)), # Linear, nn.Sigmoid(), ) if load_weights: deepsea_cpu.load_state_dict(torch.load('model_files/deepsea_cpu.pth')) return nn.Sequential(ReCodeAlphabet(), ConcatenateRC(), deepsea_cpu, AverageRC())
Example #15
Source File: asr.py From End-to-end-ASR-Pytorch with MIT License | 6 votes |
def __init__(self, input_dim, vocab_size, module, dim, layer, dropout): super(Decoder, self).__init__() self.in_dim = input_dim self.layer = layer self.dim = dim self.dropout = dropout # Init assert module in ['LSTM', 'GRU'], NotImplementedError self.hidden_state = None self.enable_cell = module == 'LSTM' # Modules self.layers = getattr(nn, module)( input_dim, dim, num_layers=layer, dropout=dropout, batch_first=True) self.char_trans = nn.Linear(dim, vocab_size) self.final_dropout = nn.Dropout(dropout)
Example #16
Source File: model.py From subword-qac with MIT License | 6 votes |
def __init__(self, config): super(LanguageModel, self).__init__() self.config = config self.ntoken = ntoken = config.ntoken self.ninp = ninp = config.ninp self.nhid = nhid = config.nhid self.nlayers = nlayers = config.nlayers self.encoder = nn.Embedding(ntoken, ninp) self.dropouti = nn.Dropout(config.dropouti) if config.dropouti > 0 else None self.lstm = LSTM([ninp] + [nhid] * nlayers, bias=False, layernorm=True, dropoutr=config.dropoutr, dropouth=config.dropouth, dropouto=config.dropouto) self.projection = nn.Linear(nhid, ninp) self.decoder = nn.Linear(ninp, ntoken) self.decoder.weight = self.encoder.weight self.init_weights()
Example #17
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self): super(CW2_Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 3) self.bnm1 = nn.BatchNorm2d(32, momentum=0.1) self.conv2 = nn.Conv2d(32, 64, 3) self.bnm2 = nn.BatchNorm2d(64, momentum=0.1) self.conv3 = nn.Conv2d(64, 128, 3) self.bnm3 = nn.BatchNorm2d(128, momentum=0.1) self.conv4 = nn.Conv2d(128, 128, 3) self.bnm4 = nn.BatchNorm2d(128, momentum=0.1) self.fc1 = nn.Linear(3200, 256) #self.dropout1 = nn.Dropout(p=0.35, inplace=False) self.bnm5 = nn.BatchNorm1d(256, momentum=0.1) self.fc2 = nn.Linear(256, 256) self.bnm6 = nn.BatchNorm1d(256, momentum=0.1) self.fc3 = nn.Linear(256, 10) #self.dropout2 = nn.Dropout(p=0.35, inplace=False) #self.dropout3 = nn.Dropout(p=0.35, inplace=False)
Example #18
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self): super(CW2_Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 3) self.bnm1 = nn.BatchNorm2d(32, momentum=0.1) self.conv2 = nn.Conv2d(32, 64, 3) self.bnm2 = nn.BatchNorm2d(64, momentum=0.1) self.conv3 = nn.Conv2d(64, 128, 3) self.bnm3 = nn.BatchNorm2d(128, momentum=0.1) self.conv4 = nn.Conv2d(128, 128, 3) self.bnm4 = nn.BatchNorm2d(128, momentum=0.1) self.fc1 = nn.Linear(3200, 256) #self.dropout1 = nn.Dropout(p=0.35, inplace=False) self.bnm5 = nn.BatchNorm1d(256, momentum=0.1) self.fc2 = nn.Linear(256, 256) self.bnm6 = nn.BatchNorm1d(256, momentum=0.1) self.fc3 = nn.Linear(256, 10) #self.dropout2 = nn.Dropout(p=0.35, inplace=False) #self.dropout3 = nn.Dropout(p=0.35, inplace=False)
Example #19
Source File: models.py From transferlearning with MIT License | 6 votes |
def __init__(self, num_class, base_net='resnet50', transfer_loss='mmd', use_bottleneck=True, bottleneck_width=256, width=1024): super(Transfer_Net, self).__init__() self.base_network = backbone.network_dict[base_net]() self.use_bottleneck = use_bottleneck self.transfer_loss = transfer_loss bottleneck_list = [nn.Linear(self.base_network.output_num( ), bottleneck_width), nn.BatchNorm1d(bottleneck_width), nn.ReLU(), nn.Dropout(0.5)] self.bottleneck_layer = nn.Sequential(*bottleneck_list) classifier_layer_list = [nn.Linear(self.base_network.output_num(), width), nn.ReLU(), nn.Dropout(0.5), nn.Linear(width, num_class)] self.classifier_layer = nn.Sequential(*classifier_layer_list) self.bottleneck_layer[0].weight.data.normal_(0, 0.005) self.bottleneck_layer[0].bias.data.fill_(0.1) for i in range(2): self.classifier_layer[i * 3].weight.data.normal_(0, 0.01) self.classifier_layer[i * 3].bias.data.fill_(0.0)
Example #20
Source File: ConvKB_1D.py From ConvKB with Apache License 2.0 | 5 votes |
def __init__(self, config): super(ConvKB, self).__init__(config) self.ent_embeddings = nn.Embedding(self.config.entTotal, self.config.hidden_size) self.rel_embeddings = nn.Embedding(self.config.relTotal, self.config.hidden_size) self.conv1_bn = nn.BatchNorm1d(3) self.conv_layer = nn.Conv1d(3, self.config.out_channels, self.config.kernel_size) # kernel size x 3 self.conv2_bn = nn.BatchNorm1d(self.config.out_channels) self.dropout = nn.Dropout(self.config.convkb_drop_prob) self.non_linearity = nn.ReLU() self.fc_layer = nn.Linear((self.config.hidden_size - self.config.kernel_size + 1) * self.config.out_channels, 1, bias=False) self.criterion = nn.Softplus() self.init_parameters()
Example #21
Source File: model.py From models with MIT License | 5 votes |
def __init__(self): super(Beluga, self).__init__() self.model = nn.Sequential( nn.Sequential( nn.Conv2d(4, 320, (1, 8)), nn.ReLU(), nn.Conv2d(320, 320, (1, 8)), nn.ReLU(), nn.Dropout(0.2), nn.MaxPool2d((1, 4), (1, 4)), nn.Conv2d(320, 480, (1, 8)), nn.ReLU(), nn.Conv2d(480, 480, (1, 8)), nn.ReLU(), nn.Dropout(0.2), nn.MaxPool2d((1, 4), (1, 4)), nn.Conv2d(480, 640, (1, 8)), nn.ReLU(), nn.Conv2d(640, 640, (1, 8)), nn.ReLU(), ), nn.Sequential( nn.Dropout(0.5), Lambda(lambda x: x.view(x.size(0), -1)), nn.Sequential(Lambda(lambda x: x.view(1, -1) if 1 == len(x.size()) else x), nn.Linear(67840, 2003)), nn.ReLU(), nn.Sequential(Lambda(lambda x: x.view(1, -1) if 1 == len(x.size()) else x), nn.Linear(2003, 2002)), ), nn.Sigmoid(), )
Example #22
Source File: asr.py From End-to-end-ASR-Pytorch with MIT License | 5 votes |
def __init__(self, input_size, vocab_size, init_adadelta, ctc_weight, encoder, attention, decoder, emb_drop=0.0): super(ASR, self).__init__() # Setup assert 0 <= ctc_weight <= 1 self.vocab_size = vocab_size self.ctc_weight = ctc_weight self.enable_ctc = ctc_weight > 0 self.enable_att = ctc_weight != 1 self.lm = None # Modules self.encoder = Encoder(input_size, **encoder) if self.enable_ctc: self.ctc_layer = nn.Linear(self.encoder.out_dim, vocab_size) if self.enable_att: self.dec_dim = decoder['dim'] self.pre_embed = nn.Embedding(vocab_size, self.dec_dim) self.embed_drop = nn.Dropout(emb_drop) self.decoder = Decoder( self.encoder.out_dim+self.dec_dim, vocab_size, **decoder) query_dim = self.dec_dim*self.decoder.layer self.attention = Attention( self.encoder.out_dim, query_dim, **attention) # Init if init_adadelta: self.apply(init_weights) for l in range(self.decoder.layer): bias = getattr(self.decoder.layers, 'bias_ih_l{}'.format(l)) bias = init_gate(bias)
Example #23
Source File: module.py From End-to-end-ASR-Pytorch with MIT License | 5 votes |
def __init__(self, input_dim, module, dim, bidirection, dropout, layer_norm, sample_rate, sample_style, proj): super(RNNLayer, self).__init__() # Setup rnn_out_dim = 2*dim if bidirection else dim self.out_dim = sample_rate * \ rnn_out_dim if sample_rate > 1 and sample_style == 'concat' else rnn_out_dim self.dropout = dropout self.layer_norm = layer_norm self.sample_rate = sample_rate self.sample_style = sample_style self.proj = proj if self.sample_style not in ['drop', 'concat']: raise ValueError('Unsupported Sample Style: '+self.sample_style) # Recurrent layer self.layer = getattr(nn, module.upper())( input_dim, dim, bidirectional=bidirection, num_layers=1, batch_first=True) # Regularizations if self.layer_norm: self.ln = nn.LayerNorm(rnn_out_dim) if self.dropout > 0: self.dp = nn.Dropout(p=dropout) # Additional projection layer if self.proj: self.pj = nn.Linear(rnn_out_dim, rnn_out_dim)
Example #24
Source File: lm.py From End-to-end-ASR-Pytorch with MIT License | 5 votes |
def __init__(self, vocab_size, emb_tying, emb_dim, module, dim, n_layers, dropout): super().__init__() self.dim = dim self.n_layers = n_layers self.emb_tying = emb_tying if emb_tying: assert emb_dim == dim, "Output dim of RNN should be identical to embedding if using weight tying." self.vocab_size = vocab_size self.emb = nn.Embedding(vocab_size, emb_dim) self.dp1 = nn.Dropout(dropout) self.dp2 = nn.Dropout(dropout) self.rnn = getattr(nn, module.upper())( emb_dim, dim, num_layers=n_layers, dropout=dropout, batch_first=True) if not self.emb_tying: self.trans = nn.Linear(emb_dim, vocab_size)
Example #25
Source File: cnn_encoder.py From OpenNRE with MIT License | 5 votes |
def __init__(self, token2id, max_length=128, hidden_size=230, word_size=50, position_size=5, blank_padding=True, word2vec=None, kernel_size=3, padding_size=1, dropout=0, activation_function=F.relu, mask_entity=False): """ Args: token2id: dictionary of token->idx mapping max_length: max length of sentence, used for postion embedding hidden_size: hidden size word_size: size of word embedding position_size: size of position embedding blank_padding: padding for CNN word2vec: pretrained word2vec numpy kernel_size: kernel_size size for CNN padding_size: padding_size for CNN """ # Hyperparameters super(CNNEncoder, self).__init__(token2id, max_length, hidden_size, word_size, position_size, blank_padding, word2vec, mask_entity=mask_entity) self.drop = nn.Dropout(dropout) self.kernel_size = kernel_size self.padding_size = padding_size self.act = activation_function self.conv = nn.Conv1d(self.input_size, self.hidden_size, self.kernel_size, padding=self.padding_size) self.pool = nn.MaxPool1d(self.max_length)
Example #26
Source File: 21_alexnet.py From deep-learning-note with MIT License | 5 votes |
def __init__(self): super(AlexNet, self).__init__() self.conv = nn.Sequential( nn.Conv2d(1, 96, 11, 4), # in_channels, out_channels, kerner_size, stride nn.ReLU(), nn.MaxPool2d(3, 2), # kernel_size, stride # 减小卷积窗口,padding 为 2 保证输入输出尺寸一致,增大输出通道 nn.Conv2d(96, 256, 5, 1, 2), # in_channels, out_channels, kerner_size, stride, padding nn.ReLU(), nn.MaxPool2d(3, 2), # 连续 3 个卷积层,更小的窗口,继续增加通道数 # 前两个卷积层后不用池化层,减小输入的高和宽 nn.Conv2d(256, 384, 3, 1, 1), nn.ReLU(), nn.Conv2d(384, 384, 3, 1, 1), nn.ReLU(), nn.Conv2d(384, 256, 3, 1, 1), nn.ReLU(), nn.MaxPool2d(3, 2) ) # 使用 Dropout 缓解过拟合 self.fc = nn.Sequential( nn.Linear(256*5*5, 4096), nn.ReLU(), nn.Dropout(0.5), nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5), # 我们用 Fashion-MNIST,所以最后为 10 nn.Linear(4096, 10) )
Example #27
Source File: 52_cnn_sentiment.py From deep-learning-note with MIT License | 5 votes |
def __init__(self, vocab, embed_size, kernel_sizes, num_channels): super(TextCNN, self).__init__() self.embedding = nn.Embedding(len(vocab), embed_size) # 不参与训练的嵌入层 self.constant_embedding = nn.Embedding(len(vocab), embed_size) self.dropout = nn.Dropout(0.5) self.decoder = nn.Linear(sum(num_channels), 2) # 时序最大池化层没有权重,所以可以共用一个实例 self.pool = GlobalMaxPool1d() self.convs = nn.ModuleList() # 创建多个一维卷积层 for c, k in zip(num_channels, kernel_sizes): self.convs.append(nn.Conv1d(in_channels = 2*embed_size, out_channels = c, kernel_size = k))
Example #28
Source File: object_alignment_train.py From VSE-C with MIT License | 5 votes |
def __init__(self, w2v, im_dim): super(ObjectAlignmentNet, self).__init__() self.embed = nn.Embedding(w2v.shape[0], w2v.shape[1]) self.embed.weight.data = torch.FloatTensor(w2v) self.im_dim = im_dim self.word_dim = w2v.shape[1] self.alignment = nn.Linear(self.im_dim * self.word_dim, 2) self.dropout = nn.Dropout(p=0.5)
Example #29
Source File: pretrained_model_reloaded_th.py From models with MIT License | 5 votes |
def get_model(load_weights = True): # alphabet seems to be fine: """ https://github.com/davek44/Basset/tree/master/src/dna_io.py#L145-L148 seq = seq.replace('A','0') seq = seq.replace('C','1') seq = seq.replace('G','2') seq = seq.replace('T','3') """ pretrained_model_reloaded_th = nn.Sequential( # Sequential, nn.Conv2d(4,300,(19, 1)), nn.BatchNorm2d(300), nn.ReLU(), nn.MaxPool2d((3, 1),(3, 1)), nn.Conv2d(300,200,(11, 1)), nn.BatchNorm2d(200), nn.ReLU(), nn.MaxPool2d((4, 1),(4, 1)), nn.Conv2d(200,200,(7, 1)), nn.BatchNorm2d(200), nn.ReLU(), nn.MaxPool2d((4, 1),(4, 1)), Lambda(lambda x: x.view(x.size(0),-1)), # Reshape, nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(2000,1000)), # Linear, nn.BatchNorm1d(1000,1e-05,0.1,True),#BatchNorm1d, nn.ReLU(), nn.Dropout(0.3), nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(1000,1000)), # Linear, nn.BatchNorm1d(1000,1e-05,0.1,True),#BatchNorm1d, nn.ReLU(), nn.Dropout(0.3), nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(1000,164)), # Linear, nn.Sigmoid(), ) if load_weights: sd = torch.load('model_files/pretrained_model_reloaded_th.pth') pretrained_model_reloaded_th.load_state_dict(sd) return pretrained_model_reloaded_th
Example #30
Source File: BiLSTM_CNN.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 5 votes |
def __init__(self, **kwargs): super(BiLSTM_CNN, self).__init__() for k in kwargs: self.__setattr__(k, kwargs[k]) V = self.embed_num D = self.embed_dim C = self.label_num paddingId = self.paddingId char_paddingId = self.char_paddingId # word embedding layer self.embed = nn.Embedding(V, D, padding_idx=paddingId) if self.pretrained_embed: self.embed.weight.data.copy_(self.pretrained_weight) # char embedding layer self.char_embedding = nn.Embedding(self.char_embed_num, self.char_dim, padding_idx=char_paddingId) # init_embedding(self.char_embedding.weight) init_embed(self.char_embedding.weight) # dropout self.dropout_embed = nn.Dropout(self.dropout_emb) self.dropout = nn.Dropout(self.dropout) # cnn # self.char_encoders = nn.ModuleList() self.char_encoders = [] for i, filter_size in enumerate(self.conv_filter_sizes): f = nn.Conv3d(in_channels=1, out_channels=self.conv_filter_nums[i], kernel_size=(1, filter_size, self.char_dim)) self.char_encoders.append(f) for conv in self.char_encoders: if self.device != cpu_device: conv.cuda() lstm_input_dim = D + sum(self.conv_filter_nums) self.bilstm = nn.LSTM(input_size=lstm_input_dim, hidden_size=self.lstm_hiddens, num_layers=self.lstm_layers, bidirectional=True, batch_first=True, bias=True) self.linear = nn.Linear(in_features=self.lstm_hiddens * 2, out_features=C, bias=True) init_linear_weight_bias(self.linear)