我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.Softmax()。
def __init__(self, model_source="model", cuda=False): self.torch = torch.cuda if cuda else torch self.cuda = cuda if self.cuda: model_source = torch.load(model_source) else: model_source = torch.load(model_source, map_location=lambda storage, loc: storage) self.src_dict = model_source["src_dict"] self.trains_score = model_source["trains_score"] self.args = args = model_source["settings"] model = BiLSTM_Cut(args) model.load_state_dict(model_source['model']) if self.cuda: model = model.cuda() model.prob_projection = nn.Softmax().cuda() else: model = model.cpu() model.prob_projection = nn.Softmax().cpu() self.model = model.eval()
def classifyOneImage(model,img_pil,preprocess): model.eval() img_tensor = preprocess(img_pil) img_tensor.unsqueeze_(0) if use_gpu: img_tensor = img_tensor.cuda() img_variable = Variable(img_tensor) out = model(img_variable) m = nn.Softmax() if use_gpu: return m(out).cpu() return(out) #method == util.GradType.NAIVE or util.GradType.GUIDED
def Occlusion_exp(image,occluding_size,occluding_stride,model,preprocess,classes,groundTruth): img = np.copy(image) height, width,_= img.shape output_height = int(math.ceil((height-occluding_size)/occluding_stride+1)) output_width = int(math.ceil((width-occluding_size)/occluding_stride+1)) ocludedImages=[] for h in range(output_height): for w in range(output_width): #occluder region h_start = h*occluding_stride w_start = w*occluding_stride h_end = min(height, h_start + occluding_size) w_end = min(width, w_start + occluding_size) input_image = copy.copy(img) input_image[h_start:h_end,w_start:w_end,:] = 0 ocludedImages.append(preprocess(Image.fromarray(input_image))) L = np.empty(output_height*output_width) L.fill(groundTruth) L = torch.from_numpy(L) tensor_images = torch.stack([img for img in ocludedImages]) dataset = torch.utils.data.TensorDataset(tensor_images,L) dataloader = torch.utils.data.DataLoader(dataset,batch_size=5,shuffle=False, num_workers=8) heatmap=np.empty(0) model.eval() for data in dataloader: images, labels = data if use_gpu: images, labels = (images.cuda()), (labels.cuda(async=True)) outputs = model(Variable(images)) m = nn.Softmax() outputs=m(outputs) if use_gpu: outs=outputs.cpu() heatmap = np.concatenate((heatmap,outs[0:outs.size()[0],groundTruth].data.numpy())) return heatmap.reshape((output_height, output_width))
def __init__(self, phase, base, extras, head, num_classes): super(SSD, self).__init__() self.phase = phase self.num_classes = num_classes # TODO: implement __call__ in PriorBox self.priorbox = PriorBox(v2) self.priors = Variable(self.priorbox.forward(), volatile=True) self.size = 300 # SSD network self.vgg = nn.ModuleList(base) # Layer learns to scale the l2 normalized features from conv4_3 self.L2Norm = L2Norm(512, 20) self.extras = nn.ModuleList(extras) self.loc = nn.ModuleList(head[0]) self.conf = nn.ModuleList(head[1]) if phase == 'test': self.softmax = nn.Softmax() self.detect = Detect(num_classes, 0, 200, 0.01, 0.45)
def __init__(self, model_source, cuda=False): self.torch = torch.cuda if cuda else torch self.cuda = cuda if self.cuda: model_source = torch.load(model_source) else: model_source = torch.load(model_source, map_location=lambda storage, loc: storage) self.src_dict = model_source["src_dict"] self.trains_score = model_source["trains_score"] self.args = args = model_source["settings"] model = BiLSTM_CRF_Size(args) model.load_state_dict(model_source['model']) if self.cuda: model = model.cuda() model.prob_projection = nn.Softmax().cuda() else: model = model.cpu() model.prob_projection = nn.Softmax().cpu() self.model = model.eval()
def call_nn_op(op, epsilon): """ a helper function that adds appropriate parameters when calling an nn module representing an operation like Softmax :param op: the nn.Module operation to instantiate :param epsilon: a scaling parameter for certain custom modules :return: instantiation of the op module with appropriate parameters """ if op in [ClippedSoftmax]: try: return op(epsilon, dim=1) except TypeError: # Support older pytorch 0.2 release. return op(epsilon) elif op in [ClippedSigmoid]: return op(epsilon) elif op in [nn.Softmax, nn.LogSoftmax]: return op(dim=1) else: return op()
def _make_test_model(): import torch.nn as nn from inferno.extensions.layers.reshape import AsMatrix toy_net = nn.Sequential(nn.Conv2d(3, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, 1, 1), nn.ELU(), nn.AdaptiveAvgPool2d((1, 1)), AsMatrix(), nn.Linear(256, 10), nn.Softmax()) return toy_net
def build_graph_model(self): model = Graph() model\ .add_input_node('input')\ .add_node('conv1', Conv2D(3, 32, 3), 'input')\ .add_node('conv2', BNReLUConv2D(32, 32, 3), 'conv1')\ .add_node('pool1', nn.MaxPool2d(kernel_size=2, stride=2), 'conv2')\ .add_node('conv3', BNReLUConv2D(32, 32, 3), 'pool1')\ .add_node('pool2', nn.MaxPool2d(kernel_size=2, stride=2), 'conv3')\ .add_node('conv4', BNReLUConv2D(32, 32, 3), 'pool2')\ .add_node('pool3', nn.AdaptiveAvgPool2d(output_size=(1, 1)), 'conv4')\ .add_node('matrix', AsMatrix(), 'pool3')\ .add_node('linear', nn.Linear(32, self.NUM_CLASSES), 'matrix')\ .add_node('softmax', nn.Softmax(), 'linear')\ .add_output_node('output', 'softmax') return model
def __init__(self, base, extras, head, num_classes): super(SSD, self).__init__() self.num_classes = num_classes # TODO: implement __call__ in PriorBox self.priorbox = PriorBox(v2) self.priors = Variable(self.priorbox.forward(), volatile=True) self.num_priors = self.priors.size(0) self.size = 300 # SSD network self.vgg = nn.ModuleList(base) # Layer learns to scale the l2 normalized features from conv4_3 self.L2Norm = L2Norm(512, 20) self.extras = nn.ModuleList(extras) self.loc = nn.ModuleList(head[0]) self.conf = nn.ModuleList(head[1]) self.softmax = nn.Softmax().cuda() # self.detect = Detect(num_classes, 0, 200, 0.001, 0.45)
def __init__(self, opt): self.name = "TextLstm" super(TextLSTM, self).__init__() self.opt = opt self.embedding = nn.Embedding(opt.vocab_size, opt.embed_dim) self.lstm = nn.LSTM(input_size=opt.embed_dim, hidden_size=opt.hidden_size, num_layers=1, batch_first=True, bidirectional=False) self.linears = nn.Sequential( nn.Linear(opt.hidden_size, opt.linear_hidden_size), nn.ReLU(), nn.Dropout(0.25), nn.Linear(opt.linear_hidden_size, opt.num_classes), # nn.Softmax() ) if opt.embedding_path: self.embedding.weight.data.copy_(torch.from_numpy(np.load(opt.embedding_path))) # # self.embedding.weight.requires_grad = False
def __init__(self, embeddings_in, embeddings_out, **kwargs): super(Seq2SeqAttnModel, self).__init__(embeddings_in, embeddings_out) self.hsz = kwargs['hsz'] nlayers = kwargs['layers'] rnntype = kwargs['rnntype'] pdrop = kwargs.get('dropout', 0.5) dsz = embeddings_in.dsz self.gpu = kwargs.get('gpu', True) self.encoder_rnn = pytorch_rnn(dsz, self.hsz, rnntype, nlayers, pdrop) self.dropout = nn.Dropout(pdrop) self.decoder_rnn = pytorch_rnn_cell(self.hsz + dsz, self.hsz, rnntype, nlayers, pdrop) self.preds = nn.Linear(self.hsz, self.nc) self.probs = nn.LogSoftmax() self.output_to_attn = nn.Linear(self.hsz, self.hsz, bias=False) self.attn_softmax = nn.Softmax() self.attn_out = nn.Linear(2 * self.hsz, self.hsz, bias=False) self.attn_tanh = pytorch_activation("tanh") self.nlayers = nlayers
def __init__(self, args): super(ACERCnnDisModel, self).__init__(args) # build model # 0. feature layers self.conv1 = nn.Conv2d(self.input_dims[0], 32, kernel_size=3, stride=2) # NOTE: for pkg="atari" self.rl1 = nn.ReLU() self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl2 = nn.ReLU() self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl3 = nn.ReLU() self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl4 = nn.ReLU() if self.enable_lstm: self.lstm = nn.LSTMCell(3*3*32, self.hidden_dim) # 1. actor: /pi_{/theta}(a_t | x_t) self.actor_5 = nn.Linear(self.hidden_dim, self.output_dims) self.actor_6 = nn.Softmax() # 2. critic: Q_{/theta_v}(x_t, a_t) self.critic_5 = nn.Linear(self.hidden_dim, self.output_dims) self._reset()
def __init__(self, args): super(ACERMlpDisModel, self).__init__(args) # build model # 0. feature layers self.fc1 = nn.Linear(self.input_dims[0] * self.input_dims[1], self.hidden_dim) self.rl1 = nn.ReLU() # lstm if self.enable_lstm: self.lstm = nn.LSTMCell(self.hidden_dim, self.hidden_dim) # 1. actor: /pi_{/theta}(a_t | x_t) self.actor_2 = nn.Linear(self.hidden_dim, self.output_dims) self.actor_3 = nn.Softmax() # 2. critic: Q_{/theta_v}(x_t, a_t) self.critic_2 = nn.Linear(self.hidden_dim, self.output_dims) self._reset()
def __init__(self, args): super(A3CCnnDisModel, self).__init__(args) # build model # 0. feature layers self.conv1 = nn.Conv2d(self.input_dims[0], 32, kernel_size=3, stride=2) # NOTE: for pkg="atari" self.rl1 = nn.ReLU() self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl2 = nn.ReLU() self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl3 = nn.ReLU() self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl4 = nn.ReLU() if self.enable_lstm: self.lstm = nn.LSTMCell(3*3*32, self.hidden_dim) # 1. policy output self.policy_5 = nn.Linear(self.hidden_dim, self.output_dims) self.policy_6 = nn.Softmax() # 2. value output self.value_5 = nn.Linear(self.hidden_dim, 1) self._reset()
def __init__(self, embedding_dim, hidden_dim, n_process_block_iters, tanh_exploration, use_tanh, use_cuda): super(CriticNetwork, self).__init__() self.hidden_dim = hidden_dim self.n_process_block_iters = n_process_block_iters self.encoder = Encoder( embedding_dim, hidden_dim, use_cuda) self.process_block = Attention(hidden_dim, use_tanh=use_tanh, C=tanh_exploration, use_cuda=use_cuda) self.sm = nn.Softmax() self.decoder = nn.Sequential( nn.Linear(hidden_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 1) )
def __init__(self, observation_space, action_space, hidden_size, sigma_init, no_noise): super(ActorCritic, self).__init__() self.no_noise = no_noise self.state_size = observation_space.shape[0] self.action_size = action_space.n self.relu = nn.ReLU(inplace=True) self.softmax = nn.Softmax(dim=1) self.fc1 = nn.Linear(self.state_size, hidden_size) self.lstm = nn.LSTMCell(hidden_size, hidden_size) if no_noise: self.fc_actor = nn.Linear(hidden_size, self.action_size) self.fc_critic = nn.Linear(hidden_size, 1) else: self.fc_actor = NoisyLinear(hidden_size, self.action_size, sigma_init=sigma_init) self.fc_critic = NoisyLinear(hidden_size, 1, sigma_init=sigma_init)
def r_duvenaud(self, h): # layers aux = [] for l in range(len(h)): param_sz = self.learn_args[l].size() parameter_mat = torch.t(self.learn_args[l])[None, ...].expand(h[l].size(0), param_sz[1], param_sz[0]) aux.append(torch.transpose(torch.bmm(parameter_mat, torch.transpose(h[l], 1, 2)), 1, 2)) for j in range(0, aux[l].size(1)): # Mask whole 0 vectors aux[l][:, j, :] = nn.Softmax()(aux[l][:, j, :].clone())*(torch.sum(aux[l][:, j, :] != 0, 1) > 0).expand_as(aux[l][:, j, :]).type_as(aux[l]) aux = torch.sum(torch.sum(torch.stack(aux, 3), 3), 1) return self.learn_modules[0](torch.squeeze(aux))
def _test_model(self, config, model): logger.info('Testing model') tr = imagenet_like()['test'] folder = ImageTestFolder(self.input_dir, transform=tr) logger.info('Number of files %d', len(folder)) results = [] crop_num = len(tr.transforms[0]) for index in range(crop_num): # iterate over tranformations logger.info('Testing transformation %d/%d', index + 1, crop_num) folder.transform.transforms[0].index = index loader = torch.utils.data.DataLoader(folder, batch_size=config.batch_size, num_workers=config.workers, pin_memory=True) names, crop_results = test_model(loader, model, activation=nn.Softmax()) results.append(crop_results) #break final_results = [sum(map(lambda x: x[i].numpy(), results)) / float(crop_num) for i in range(len(folder.imgs))] return names, final_results
def __init__(self, config): super(VIN, self).__init__() self.config = config self.h = nn.Conv2d(in_channels=config.l_i, out_channels=config.l_h, kernel_size=(3, 3), stride=1, padding=1, bias=True) self.r = nn.Conv2d(in_channels=config.l_h, out_channels=1, kernel_size=(1, 1), stride=1, padding=0, bias=False) self.q = nn.Conv2d(in_channels=1, out_channels=config.l_q, kernel_size=(3, 3), stride=1, padding=1, bias=False) self.fc = nn.Linear(in_features=config.l_q, out_features=8, bias=False) self.w = Parameter(torch.zeros(config.l_q,1,3,3), requires_grad=True) self.sm = nn.Softmax()
def __init__(self): super(Q, self).__init__() self.cnn = nn.Sequential( nn.Conv2d(3, 16, 3, stride=1, padding=1), nn.LeakyReLU(), nn.MaxPool2d(2, 2), nn.Conv2d(16, 32, 3, stride=1, padding=1), nn.LeakyReLU(), nn.MaxPool2d(2, 2), nn.Conv2d(32, 64, 3, stride=1, padding=1), nn.LeakyReLU(), nn.MaxPool2d(2, 2) ) self.fc = nn.Sequential( nn.Linear(4 * 4 * 64, 128), nn.LeakyReLU(), nn.Linear(4*4*64, 10), nn.Softmax() )
def __init__(self, input_shape, base_filters, num_hidden, num_actions): super(CNN, self).__init__() num_input = int(np.prod(input_shape)) self.convs = nn.Sequential( nn.Conv2d(input_shape[0], base_filters, 8, 4, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 nn.Conv2d(base_filters, base_filters * 2, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 16 x 16 nn.Conv2d(base_filters * 2, base_filters * 2, 3, 1, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 7 x 7 ) self.classifier = nn.Sequential( nn.Linear(base_filters * 2 * 10 * 10, num_hidden), nn.ReLU(), nn.Linear(num_hidden, num_actions), nn.Softmax() )
def __init__(self, input_shape, base_filters, num_hidden, num_actions): super(CNN, self).__init__() num_input = int(np.prod(input_shape)) self.num_hidden = num_hidden self.convs = nn.Sequential( nn.Conv2d(input_shape[0], base_filters, 5, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 nn.Conv2d(base_filters, base_filters * 2, 5, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 16 x 16 nn.Conv2d(base_filters * 2, base_filters * 2, 5, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 9 x 9 ) # for p in self.convs.parameters(): # p.requires_grad = False # use random conv features #self.convs.apply(weights_init) self.conv_out_size = base_filters * 2 * 11 * 11 self.rnn = nn.RNN(self.conv_out_size, self.num_hidden, batch_first=True) self.classifier = nn.Sequential( nn.Linear(num_hidden, num_actions), nn.Softmax() )
def __init__(self, params): super(ChatBot, self).__init__(); # absorb all parameters to self for attr in params: setattr(self, attr, params[attr]); # standard initializations self.hState = torch.Tensor(); self.cState = torch.Tensor(); self.actions = []; self.evalFlag = False; # modules (common) self.inNet = nn.Embedding(self.inVocabSize, self.embedSize); self.outNet = nn.Linear(self.hiddenSize, self.outVocabSize); self.softmax = nn.Softmax(); # initialize weights initializeWeights([self.inNet, self.outNet], 'xavier'); # initialize hidden states
def __init__(self, config): super(InnerAttentionNAACLEncoder, self).__init__() self.bsize = config['bsize'] self.word_emb_dim = config['word_emb_dim'] self.enc_lstm_dim = config['enc_lstm_dim'] self.pool_type = config['pool_type'] self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True) self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda() self.proj_key = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False) self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False) self.query_embedding = nn.Embedding(1, 2*self.enc_lstm_dim) self.softmax = nn.Softmax()
def __init__(self, config): super(InnerAttentionMILAEncoder, self).__init__() self.bsize = config['bsize'] self.word_emb_dim = config['word_emb_dim'] self.enc_lstm_dim = config['enc_lstm_dim'] self.pool_type = config['pool_type'] self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True) self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda() self.proj_key = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False) self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=False) self.query_embedding = nn.Embedding(2, 2*self.enc_lstm_dim) self.softmax = nn.Softmax()
def __init__(self, config): super(InnerAttentionYANGEncoder, self).__init__() self.bsize = config['bsize'] self.word_emb_dim = config['word_emb_dim'] self.enc_lstm_dim = config['enc_lstm_dim'] self.pool_type = config['pool_type'] self.enc_lstm = nn.LSTM(self.word_emb_dim, self.enc_lstm_dim, 1, bidirectional=True) self.init_lstm = Variable(torch.FloatTensor(2, self.bsize, self.enc_lstm_dim).zero_()).cuda() self.proj_lstm = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True) self.proj_query = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True) self.proj_enc = nn.Linear(2*self.enc_lstm_dim, 2*self.enc_lstm_dim, bias=True) self.query_embedding = nn.Embedding(1, 2*self.enc_lstm_dim) self.softmax = nn.Softmax()
def __init__(self, dim, coverage=False, attn_type="dot"): super(GlobalAttention, self).__init__() self.dim = dim self.attn_type = attn_type assert (self.attn_type in ["dot", "general", "mlp"]), ( "Please select a valid attention type.") if self.attn_type == "general": self.linear_in = nn.Linear(dim, dim, bias=False) elif self.attn_type == "mlp": self.linear_context = BottleLinear(dim, dim, bias=False) self.linear_query = nn.Linear(dim, dim, bias=True) self.v = BottleLinear(dim, 1, bias=False) # mlp wants it with bias out_bias = self.attn_type == "mlp" self.linear_out = nn.Linear(dim*2, dim, bias=out_bias) self.sm = nn.Softmax() self.tanh = nn.Tanh() if coverage: self.linear_cover = nn.Linear(1, dim, bias=False)
def __init__(self, config): super(SelfAttentiveEncoder, self).__init__() self.bilstm = BiLSTM(config) self.drop = nn.Dropout(config['dropout']) self.ws1 = nn.Linear(config['nhid'] * 2, config['attention-unit'], bias=False) self.ws2 = nn.Linear(config['attention-unit'], config['attention-hops'], bias=False) self.tanh = nn.Tanh() self.softmax = nn.Softmax() self.dictionary = config['dictionary'] # self.init_weights() self.attention_hops = config['attention-hops']
def __init__(self, batch_size, num_tokens, embed_size, word_gru_hidden, bidirectional= True, init_range=0.1, use_lstm=False): super(AttentionWordRNN, self).__init__() self.batch_size = batch_size self.num_tokens = num_tokens self.embed_size = embed_size self.word_gru_hidden = word_gru_hidden self.bidirectional = bidirectional self.use_lstm = use_lstm self.lookup = nn.Embedding(num_tokens, embed_size) if bidirectional == True: if use_lstm: print("inside using LSTM") self.word_gru = nn.LSTM(embed_size, word_gru_hidden, bidirectional= True) else: self.word_gru = nn.GRU(embed_size, word_gru_hidden, bidirectional= True) self.weight_W_word = nn.Parameter(torch.Tensor(2* word_gru_hidden, 2*word_gru_hidden)) self.bias_word = nn.Parameter(torch.Tensor(2* word_gru_hidden,1)) self.weight_proj_word = nn.Parameter(torch.Tensor(2*word_gru_hidden, 1)) else: if use_lstm: self.word_gru = nn.LSTM(embed_size, word_gru_hidden, bidirectional= False) else: self.word_gru = nn.GRU(embed_size, word_gru_hidden, bidirectional= False) self.weight_W_word = nn.Parameter(torch.Tensor(word_gru_hidden, word_gru_hidden)) self.bias_word = nn.Parameter(torch.Tensor(word_gru_hidden,1)) self.weight_proj_word = nn.Parameter(torch.Tensor(word_gru_hidden, 1)) self.softmax_word = nn.Softmax() self.weight_W_word.data.uniform_(-init_range, init_range) self.weight_proj_word.data.uniform_(-init_range, init_range)
def classifyOneImage(model,img_pil,preprocess): model.eval() img_tensor = preprocess(img_pil) img_tensor.unsqueeze_(0) if use_gpu: img_tensor = img_tensor.cuda() img_variable = Variable(img_tensor) out = model(img_variable) m = nn.Softmax() if use_gpu: return m(out).cpu() return(out)
def __init__(self, block, layers, num_classes=3): super(cnnT2, self).__init__() self.in_channels = 16 self.conv = conv3x3(1, 16) self.bn = nn.BatchNorm2d(16) self.relu = nn.ReLU(inplace=True) self.layer1 = self.make_layer(block, 16, layers[0]) self.layer2 = self.make_layer(block, 32, layers[0]) self.layer3 = self.make_layer(block, 64, layers[1]) self.avg_pool = nn.AvgPool2d(8) self.fc = nn.Linear(2304, num_classes) self.softmax = nn.Softmax()
def __init__(self): super(cnnT1, self).__init__() self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, padding=1) self.fc1 = nn.Linear(16 * 25 * 7, 120) self.fc2 = nn.Linear(120, 3) self.softmax = nn.Softmax()
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 __init__(self, batch_size, rnn_len=5, hidden_state=64, feature_num=29, var_hidden=None, dropout=False): super(RNNModel, self).__init__() self.n_layer = rnn_len self.nhid = hidden_state self.l0 = nn.Linear(feature_num, feature_num) # self.d1 = nn.Dropout(p=0.2) if var_hidden is None: self.rnn = nn.LSTM(input_size=feature_num, hidden_size=hidden_state, num_layers=rnn_len, batch_first=True) rnn_output_size = hidden_state else: self.hidden_arr = var_hidden for i, state_num in enumerate(var_hidden): assert (rnn_len == len(var_hidden)) last_size = var_hidden[i - 1] if i > 0 else feature_num setattr(self, 'rnn_{}'.format(i), nn.LSTM(input_size=last_size, hidden_size=state_num, num_layers=1, batch_first=True)) rnn_output_size = var_hidden[-1] # (N * 500 * 128) # (N * 128) # self.l1 = nn.Linear(hidden_state, hidden_state) # self.a1 = nn.Sigmoid() # (N * 128) # (N * 128) self._dropout = dropout if dropout: self.do = nn.Dropout(p=0.2) self.l2 = nn.Linear(rnn_output_size, 2) # (N * 2) self.softmax = nn.Softmax() # (100, 128) self.init_weights()
def __init__(self, block, layers, num_classes=2): super(cnnT3, self).__init__() self.in_channels = 16 self.conv = conv3x3(1, 16) self.bn = nn.BatchNorm2d(16) 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, 64, layers[2]) self.avg_pool = nn.AvgPool2d(8) self.fc = nn.Linear(2304, num_classes) self.softmax = nn.Softmax()
def __init__(self, observation_space, non_rgb_rgb_state_size, action_space, hidden_size): super(ActorCritic, self).__init__() self.rgb_state_size = (6, 128, 128) self.action_size = 5 self.relu = nn.ReLU(inplace=True) self.softmax = nn.Softmax() # the archtecture is adapted from Sim2Real (Rusu et. al., 2016) self.conv1 = nn.Conv2d( self.rgb_state_size[0], 16, 8, stride=4, padding=1) self.conv2 = nn.Conv2d(16, 32, 5, stride=2) self.fc1 = nn.Linear(1152 + non_rgb_rgb_state_size, hidden_size) self.lstm = nn.LSTMCell(hidden_size, hidden_size) self.fc_actor1 = nn.Linear(hidden_size, self.action_size) self.fc_actor2 = nn.Linear(hidden_size, self.action_size) self.fc_actor3 = nn.Linear(hidden_size, self.action_size) self.fc_actor4 = nn.Linear(hidden_size, self.action_size) self.fc_actor5 = nn.Linear(hidden_size, self.action_size) self.fc_actor6 = nn.Linear(hidden_size, self.action_size) self.fc_critic = nn.Linear(hidden_size, 1) # Orthogonal weight initialisation for name, p in self.named_parameters(): if 'weight' in name: init.orthogonal(p) elif 'bias' in name: init.constant(p, 0)
def __init__(self, train, valid, test, devscores, config): # fix seed np.random.seed(config['seed']) torch.manual_seed(config['seed']) assert torch.cuda.is_available(), 'torch.cuda required for Relatedness' torch.cuda.manual_seed(config['seed']) self.train = train self.valid = valid self.test = test self.devscores = devscores self.inputdim = train['X'].shape[1] self.nclasses = config['nclasses'] self.seed = config['seed'] self.l2reg = 0. self.batch_size = 64 self.maxepoch = 1000 self.early_stop = True self.model = nn.Sequential( nn.Linear(self.inputdim, self.nclasses), nn.Softmax(), ) self.loss_fn = nn.MSELoss() if torch.cuda.is_available(): self.model = self.model.cuda() self.loss_fn = self.loss_fn.cuda() self.loss_fn.size_average = False self.optimizer = optim.Adam(self.model.parameters(), weight_decay=self.l2reg)
def __init__(self, d_k, dropout): super().__init__() self.temper = np.power(d_k, 0.5) self.dropout = nn.Dropout(dropout) self.softmax = nn.Softmax()
def __init__(self,out_size): super(VC_resnet50,self).__init__() self.softmax=nn.Softmax() self.resnet50=torchvision.models.resnet50(pretrained=True) self.resnet50.fc=nn.Linear(2048,out_size).cuda()
def __init__(self,out_size): super(VC_vgg19,self).__init__() self.softmax=nn.Softmax() self.vgg19=torchvision.models.vgg19(pretrained=True).cuda() mod = list(self.vgg19.classifier.children()) mod.pop() mod.append(torch.nn.Linear(4096, out_size).cuda()) new_classifier = torch.nn.Sequential(*mod) self.vgg19.classifier = new_classifier
def __init__(self, state_size, out_size): super().__init__() self.net = nn.Linear(state_size, out_size) self.softmax = nn.Softmax() xavier_init(self)
def __init__(self, embed_size, state_size, out_size): super().__init__() self.net_lstm = nn.LSTMCell(embed_size, state_size) self.net_mlp = nn.Linear(state_size, out_size) self.softmax = nn.Softmax() xavier_init(self)
def _make_test_model(): toy_net = nn.Sequential(nn.Conv2d(3, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, 1, 1), nn.ELU(), nn.AdaptiveMaxPool2d((1, 1)), AsMatrix(), nn.Linear(256, 10), nn.Softmax()) return toy_net
def __init__(self): super(C3D, self).__init__() self.conv1 = nn.Conv3d(3, 64, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.pool1 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)) self.conv2 = nn.Conv3d(64, 128, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.pool2 = nn.MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2)) self.conv3a = nn.Conv3d(128, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.conv3b = nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.pool3 = nn.MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2)) self.conv4a = nn.Conv3d(256, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.conv4b = nn.Conv3d(512, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.pool4 = nn.MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2)) self.conv5a = nn.Conv3d(512, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.conv5b = nn.Conv3d(512, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.pool5 = nn.MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2), padding=(0, 1, 1)) self.fc6 = nn.Linear(8192, 4096) self.fc7 = nn.Linear(4096, 4096) self.fc8 = nn.Linear(4096, 487) self.dropout = nn.Dropout(p=0.5) self.relu = nn.ReLU() self.softmax = nn.Softmax()
def __init__(self, num_actions): super().__init__() self.conv_layers = nn.Sequential( nn.Conv2d(INPUT_CHANNELS, 32, 8, 4), nn.ReLU(), nn.Conv2d(32, 64, 4, 2), nn.ReLU(), nn.Conv2d(64, 64, 3, 1), nn.ReLU() ) self.fc = nn.Linear(3136, 512) self.policy_output = nn.Sequential( nn.Linear(512, num_actions), nn.Softmax(1) ) self.value_output = nn.Linear(512, 1) # init weights and biases import torch.nn.init as init for m in self.modules(): if isinstance(m, nn.Conv2d): init.kaiming_normal(m.weight) m.bias.data.zero_() elif isinstance(m, nn.Linear): init.kaiming_normal(m.weight) m.bias.data.zero_()
def __init__(self, dim): super(GlobalAttention, self).__init__() self.linear_in = nn.Linear(dim, dim, bias=False) self.sm = nn.Softmax() self.linear_out = nn.Linear(dim*2, dim, bias=False) self.tanh = nn.Tanh() self.mask = None