Exemple #1
0
    def __init__(self,
                 input_nc,
                 input_width,
                 input_height,
                 ngf=10,
                 kernel_size=3,
                 soft_decision=True,
                 stochastic=False,
                 reduction_rate=2,
                 dropout_prob=0.0,
                 **kwargs):
        # super(RouterGAPwithConv_TwoFClayers, self).__init__()
        self.ngf = ngf
        self.soft_decision = soft_decision
        self.stochastic = stochastic
        self.dropout_prob = dropout_prob

        if max(input_width, input_height) < kernel_size:
            warnings.warn('Router kernel too large, shrink it')
            kernel_size = max(input_width, input_height)

        self.conv1 = tf.keras.layers.Conv2D(input_nc,
                                            ngf,
                                            kernel_size=kernel_size)
        self.fc1 = nn.Linear(ngf, ngf / reduction_rate + 1)
        self.fc2 = nn.Linear(ngf / reduction_rate + 1, 1)
        self.sigmoid = nn.Sigmoid()
Exemple #2
0
 def __init__(self,
              input_nc,
              input_width,
              input_height,
              dropout_prob=0.0,
              **kwargs):
     # super(MLP_LeNetMNIST, self).__init__()
     self.dropout_prob = dropout_prob
     ngf = input_nc * input_width * input_height
     self.fc1 = nn.Linear(ngf, int(round(ngf / 1.6)))
     self.fc2 = nn.Linear(int(round(ngf / 1.6)), 10)
Exemple #3
0
 def __init__(self,
              input_nc,
              input_width,
              input_height,
              no_classes=10,
              **kwargs):
     # super(MLP_LeNet, self).__init__()
     assert input_nc * input_width * input_height > 120
     self.fc1 = nn.Linear(input_nc * input_width * input_height, 120)
     self.fc2 = nn.Linear(120, 84)
     self.fc3 = nn.Linear(84, no_classes)
Exemple #4
0
    def __init__(self,
                 hidden_size=256,
                 num_layers=2,
                 latent_size=50,
                 bidirectional=False):
        super(RNNEncoder, self).__init__()

        self.embs = nn.Embedding(get_vocab_size(), hidden_size)
        self.rnn = nn.GRU(input_size=hidden_size,
                          hidden_size=hidden_size,
                          num_layers=num_layers,
                          bidirectional=bidirectional)

        self.final_mlp = nn.Sequential(nn.Linear(hidden_size, hidden_size),
                                       nn.LeakyReLU(),
                                       nn.Linear(hidden_size, 2 * latent_size))
Exemple #5
0
    def __init__(self,
                 input_nc,
                 input_width,
                 input_height,
                 ngf=32,
                 kernel_size=3,
                 soft_decision=True,
                 stochastic=False,
                 **kwargs):

        # super(RouterGAPwithDoubleConv, self).__init__()
        self.ngf = ngf
        self.soft_decision = soft_decision
        self.stochastic = stochastic

        if max(input_width, input_height) < kernel_size:
            warnings.warn('Router kernel too large, shrink it')
            kernel_size = max(input_width, input_height)
            if max(input_width, input_height) % 2 == 0:
                kernel_size += 1

        padding = int((kernel_size - 1) / 2)
        self.conv1 = tf.keras.layers.Conv2D(input_nc,
                                            ngf,
                                            kernel_size=kernel_size,
                                            padding=padding)
        self.conv2 = tf.keras.layers.Conv2D(ngf,
                                            ngf,
                                            kernel_size=kernel_size,
                                            padding=padding)
        self.relu = nn.ReLU(inplace=True)
        self.linear1 = nn.Linear(ngf, 1)
        self.sigmoid = nn.Sigmoid()
Exemple #6
0
 def __init__(self,
              input_nc,
              input_width,
              input_height,
              no_classes=10,
              **kwargs):
     super(LR, self).__init__()
     self.fc = nn.Linear(input_nc * input_width * input_height, no_classes)
Exemple #7
0
    def __init__(self,
                 input_nc,
                 input_width,
                 input_height,
                 kernel_size=28,
                 soft_decision=True,
                 stochastic=False,
                 reduction_rate=2,
                 dropout_prob=0.0,
                 **kwargs):
        # super(RouterGAP_TwoFClayers, self).__init__()
        self.soft_decision = soft_decision
        self.stochastic = stochastic
        self.dropout_prob = dropout_prob

        self.fc1 = nn.Linear(input_nc, input_nc / reduction_rate + 1)
        self.fc2 = nn.Linear(input_nc / reduction_rate + 1, 1)
        self.sigmoid = nn.Sigmoid()
Exemple #8
0
 def __init__(self, input_dim, hidden_dim):
     super(StackedAttention, self).__init__()
     self.Wv = tf.keras.layers.Conv2d(input_dim,
                                      hidden_dim,
                                      kernel_size=(1, 1),
                                      padding=0)
     self.Wu = nn.Linear(input_dim, hidden_dim)
     self.Wp = nn.Conv2d(hidden_dim, 1, kernel_size=(1, 1), padding=0)
     self.hidden_dim = hidden_dim
     self.attention_maps = None
Exemple #9
0
    def __init__(self,
                 input_nc,
                 input_width,
                 input_height,
                 kernel_size=28,
                 soft_decision=True,
                 stochastic=False,
                 reduction_rate=2,
                 **kwargs):
        # super(Router_MLP_h1, self).__init__()
        self.soft_decision = soft_decision
        self.stochastic = stochastic

        width = input_nc * input_width * input_height
        self.fc1 = nn.Linear(width, width / reduction_rate + 1)
        self.sigmoid = nn.Sigmoid()
Exemple #10
0
    def __init__(self,
                 input_nc,
                 input_width,
                 input_height,
                 ngf=5,
                 kernel_size=7,
                 soft_decision=True,
                 stochastic=False,
                 **kwargs):

        # super(RouterGAP, self).__init__()
        self.ngf = ngf
        self.soft_decision = soft_decision
        self.stochastic = stochastic

        if max(input_width, input_height) < kernel_size:
            warnings.warn('Router kernel too large, shrink it')
            kernel_size = max(input_width, input_height)

        self.conv1 = tf.keras.layers.Conv2D(input_nc,
                                            ngf,
                                            kernel_size=kernel_size)
        self.linear1 = nn.Linear(ngf, 1)
        self.sigmoid = nn.Sigmoid()
Exemple #11
0
    def build_graph(self):
        """
        Builds computational graph for policy
        """
        with tf.variable_scope(self.name):

            instr_embedding = self._get_instr_embedding(obs.instr)
            x = torch.transpose(torch.transpose(obs.image, 1, 3), 2, 3)
            x = self.image_conv(x)
            for controler in self.controllers:
                x = controler(x, instr_embedding)
            x = F.relu(self.film_pool(x))
            x = x.reshape(x.shape[0], -1)

            hidden = (memory[:, :self.semi_memory_size],
                      memory[:, self.semi_memory_size:])
            hidden = self.memory_rnn(x, hidden)
            embedding = hidden[0]
            memory = torch.cat(hidden, dim=1)

            embedding = torch.cat((embedding, instr_embedding), dim=1)
            x = self.actor(embedding)
            dist = Categorical(logits=F.log_softmax(x, dim=1))

            # memory_rnn = tf.nn.rnn_cell.LSTMCell(self.memory_dim)  # TODO: set these

            rnn_outs = create_rnn(
                name='probs_network',
                cell_type='lstm',
                output_dim=self.action_dim,
                hidden_sizes=self.hidden_sizes,
                hidden_nonlinearity=self.hidden_nonlinearity,
                output_nonlinearity=tf.nn.softmax,
                input_dim=(
                    None,
                    None,
                    self.obs_dim,
                ),
            )

            # obs_var, hidden_var, probs_var, next_hidden_var, cell = create_rnn(name='probs_network2',
            #                       cell_type='lstm',
            #                       output_dim=self.action_dim,
            #                       hidden_sizes=self.hidden_sizes,
            #                       hidden_nonlinearity=self.hidden_nonlinearity,
            #                       output_nonlinearity=tf.nn.softmax,
            #                       input_dim=(None, None, self.obs_dim,),
            #                       )

            from tensorflow.keras import datasets, layers, models
            import matplotlib.pyplot as plt

            # x = "INPUT"
            # input_conv = nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
            #
            # model = models.Sequential()
            # model.add(layers.Conv2D(128, (2,2), padding='SAME', input_shape=(8, 8, 3)))
            # model.add(layers.BatchNormalization())
            # model.add(layers.ReLU())
            # model.add(layers.MaxPooling2D((2, 2), strides=2))
            # model.add(layers.Conv2D(128, (3, 3), padding='SAME'))
            # model.add(layers.BatchNormalization())
            # model.add(layers.ReLU())
            # model.add(layers.MaxPooling2D((2, 2), strides=2))
            # model.compile('rmsprop', 'mse')

            film_pool = layers.MaxPooling2D((2, 2), strides=2)
            word_embedding = layers.Embedding(
                obs_space["instr"], self.instr_dim)  # TODO: get this!
            gru_dim = self.instr_dim  # TODO: set this
            gru_dim //= 2
            instr_rnn = tf.keras.layers.GRUCell(self.instr_dim,
                                                gru_dim,
                                                batch_first=True,
                                                bidirectional=True)
            self.final_instr_dim = self.instr_dim

            memory_rnn = tf.keras.layers.LSTMCell(
                self.image_dim, self.memory_dim)  # TODO: set these

            # Resize image embedding
            embedding_size = self.semi_memory_size  # TODO: set this
            # if self.use_instr and not "filmcnn" in arch:
            #     self.embedding_size += self.final_instr_dim  # TODO: consider keepint this!

            num_module = 2
            self.controllers = []
            for ni in range(num_module):
                if ni < num_module - 1:
                    mod = ExpertControllerFiLM(
                        in_features=self.final_instr_dim,
                        out_features=128,
                        in_channels=128,
                        imm_channels=128)
                else:
                    mod = ExpertControllerFiLM(
                        in_features=self.final_instr_dim,
                        out_features=self.image_dim,
                        in_channels=128,
                        imm_channels=128)
                self.controllers.append(mod)
                self.add_module('FiLM_Controler_' + str(ni), mod)
            #
            # Define actor's model
            self.actor = nn.Sequential(nn.Linear(self.embedding_size, 64),
                                       nn.Tanh(),
                                       nn.Linear(64, action_space.n))

            # rnn_outs = create_rnn(name='probs_network',
            #                       cell_type=self._cell_type,
            #                       output_dim=self.action_dim,
            #                       hidden_sizes=self.hidden_sizes,
            #                       hidden_nonlinearity=self.hidden_nonlinearity,
            #                       output_nonlinearity=tf.nn.softmax,
            #                       input_dim=(None, None, self.obs_dim,),
            #                       )

            self.obs_var, self.hidden_var, self.probs_var, self.next_hidden_var, self.cell = rnn_outs
            self.probs_var = (self.probs_var + probs_var) / 2

            # symbolically define sampled action and distribution
            self._dist = Discrete(self.action_dim)

            # save the policy's trainable variables in dicts
            current_scope = tf.get_default_graph().get_name_scope()
            trainable_policy_vars = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, scope=current_scope)
            self.policy_params = OrderedDict([
                (remove_scope_from_name(var.name, current_scope), var)
                for var in trainable_policy_vars
            ])