示例#1
0
    def forward(self, state, action):
        """
        Perform forward pass through the network.

        Parameters
        ----------
        state : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            State of the environment.
        action : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            Action of the agent.

        Returns
        -------
        xc : torch.Tensor, torch.cuda.FloatTensor
            Network output.

        """

        # convert to torch
        s = convert_to_tensor(state, self.device)
        a = convert_to_tensor(action, self.device)

        xs = F.relu(self.sfc1(s))
        xc = torch.cat((xs, a), dim=1)
        xc = F.relu(self.cfc1(xc))
        xc = self.cfc2(xc)

        return xc
示例#2
0
    def forward(self, state, action1, action2):
        """
        Perform forward pass through the network.

        Parameters
        ----------
        state : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            State of the environment.
        action1 : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            Action of first agent.
        action2 : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            Action of second agent.

        Returns
        -------
        xc : torch.Tensor, torch.cuda.FloatTensor
            Network output.

        """

        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

        # convert to torch
        s = convert_to_tensor(state, device)
        a1 = convert_to_tensor(action1, device)
        a2 = convert_to_tensor(action2, device)

        isbatch = True if len(s.size()) > 1 else False

        # state path
        xs = F.relu(self.sfc1(s))
        xs = F.relu(self.sfc2(xs))

        # action1 path
        xa1 = F.relu(self.a1fc1(a1))
        xa1 = F.relu(self.a1fc2(xa1))

        # action2 path
        xa2 = F.relu(self.a2fc1(a2))
        xa2 = F.relu(self.a2fc2(xa2))

        # common path
        xc = torch.cat((xs, xa1, xa2), dim=1)
        # apply batch norm for batch inputs
        if isbatch:
            xc = self.cbn1(xc)
        xc = F.relu(self.cfc1(xc))
        xc = F.relu(self.cfc2(xc))
        xc = self.dro1(xc)
        xc = self.cfc3(xc)

        return xc
示例#3
0
文件: nmt.py 项目: mihirkale815/nmt
    def decode(self, src_encodings: Tensor, decoder_init_state, tgt_sents: List[List[str]]):
        """
        Given source encodings, compute the log-likelihood of predicting the gold-standard target
        sentence tokens

        Args:
            src_encodings: hidden states of tokens in source sentences
            decoder_init_state: decoder GRU/LSTM's initial state
            tgt_sents: list of gold-standard target sentences, wrapped by `<s>` and `</s>`

        Returns:
            scores: could be a variable of shape (batch_size, ) representing the 
                log-likelihood of generating the gold-standard target sentence for 
                each example in the input batch
        """
        target, lens = utils.convert_to_tensor(tgt_sents,self.vocab.tgt)
        batch_size, max_len = target.size()
        predictions = torch.zeros((batch_size,max_len,len(self.vocab.tgt)))
        hidden = decoder_init_state
        inputs = torch.LongTensor([self.vocab.tgt.word2id['<s>'] for _ in range(batch_size)])
        for idx in range(0, max_len):
            outputs, hidden, attn_scores = self.decoder(inputs, hidden, src_encodings)
            inputs = target[:, idx]
            predictions[:, idx] = outputs
        return target,predictions
示例#4
0
    def forward(self, state):
        """
        Perform forward pass through the network.

        Parameters
        ----------
        state : numpy.ndarray, torch.Tensor, torch.cuda.FloatTensor
            State of the environment.

        Returns
        -------
        x : torch.Tensor, torch.cuda.FloatTensor
            Network output.

        """

        # convert to torch
        x = convert_to_tensor(state, self.device)

        # forward pass
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.tanh(self.fc3(x))  # restrict action from -1 to 1 using tanh

        return x
示例#5
0
文件: nmt.py 项目: mihirkale815/nmt
    def encode(self, src_sents: List[List[str]]) -> Tuple[Tensor, Any]:
        """
        Use a GRU/LSTM to encode source sentences into hidden states

        Args:
            src_sents: list of source sentence tokens

        Returns:
            src_encodings: hidden states of tokens in source sentences, this could be a variable 
                with shape (batch_size, source_sentence_length, encoding_dim), or in orther formats
            decoder_init_state: decoder GRU/LSTM's initial state, computed from source encodings
        """
        source,lens = utils.convert_to_tensor(src_sents,self.vocab.src)
        src_encodings,decoder_init_state = self.encoder(source,lens)
        return src_encodings, decoder_init_state
示例#6
0
                                  to_file="{}/{}/{}/net_g.png".format(args.output_dir, model_name, date_str))
        tf.keras.utils.plot_model(m.d, show_shapes=True, expand_nested=True, dpi=150,
                                  to_file="{}/{}/{}/net_d.png".format(args.output_dir, model_name, date_str))
    except Exception as e:
        print(e)
    return logger


if __name__ == "__main__":
    utils.set_soft_gpu(args.soft_gpu)
    cifar = CIFAR(n_class=args.label_dim)
    (x_train, y_train), (x_test, y_test) = cifar.load()
    print("x_shape:", x_train.shape, " x_type:", x_train.dtype,
          " y_shape:", y_train.shape, " y_type:", y_train.dtype)
    model_name = args.model
    summary_writer = tf.summary.create_file_writer('{}/{}/{}'.format(args.output_dir, model_name, date_str))
    if model_name == "acgan":
        d = utils.get_ds(args.batch_size // 2, x_train, y_train)
        m = ACGAN(args.latent_dim, args.label_dim, x_train.shape[1:], a=-1, b=1, c=1,
                  summary_writer=summary_writer, lr=args.lr, beta1=args.beta1, beta2=args.beta2, net=args.net)
        logger = init_logger(model_name, date_str, m)
        train(m, d)
    elif model_name == "acgangp":
        x_train, y_train = utils.convert_to_tensor(x_train, y_train)
        m = ACGANgp(args.latent_dim, args.label_dim, x_train.shape[1:], args.lambda_,
                    summary_writer=summary_writer, lr=args.lr, beta1=args.beta1, beta2=args.beta2, net=args.net)
        logger = init_logger(model_name, date_str, m)
        traingp(m, x_train, y_train)
    else:
        raise ValueError("model name error")