Example #1
0
    def __init__(self, **kwargs):
        super(SimpleDeterministicModelDynamics, self).__init__()
        self.__acoustic_state_dim = kwargs['goal_dim']
        self.__action_dim = kwargs['action_dim']
        self.__state_dim = kwargs['state_dim']
        self.__linears_size = kwargs['linear_layers_size']

        input_size = self.__acoustic_state_dim + self.__state_dim + self.__action_dim
        self.__bn1 = torch.nn.BatchNorm1d(input_size)

        self.linears = ModuleList([Linear(input_size, self.__linears_size[0])])
        self.linears.extend([
            Linear(self.__linears_size[i - 1], self.__linears_size[i])
            for i in range(1, len(self.__linears_size))
        ])

        self.goal = Linear(self.__linears_size[-1], kwargs['goal_dim'])
        self.state = Linear(self.__linears_size[-1], kwargs['state_dim'])

        self.relu = ReLU()
        self.tanh = Tanh()

        self.apply(init_weights)  # xavier uniform init
Example #2
0
    def forward(self, input, label):
        network = torch.cat([input, label], dim=1)
        network = self.gc_full(network)
        network = network.view(-1, CONFIG["NOISE_DIM"] + 10, 1, 1)
        network = self.deconv1(network)
        network = self.bn1(network)
        network = ReLU()(network)

        network = self.deconv2(network)
        network = self.bn2(network)
        network = ReLU()(network)

        network = self.deconv3(network)
        network = self.bn3(network)
        network = ReLU()(network)

        network = self.deconv4(network)
        network = self.bn4(network)
        network = ReLU()(network)

        network = self.deconv5(network)
        network = Tanh()(network)
        return network
Example #3
0
    def __init__(self, **kwargs):
        super(StochasticLstmModelDynamics, self).__init__()
        self.__acoustic_state_dim = kwargs['goal_dim']
        self.__action_dim = kwargs['action_dim']
        self.__state_dim = kwargs['state_dim']
        self.__lstm_sizes = kwargs['lstm_layers_size']
        self.__linears_size = kwargs['linear_layers_size']

        input_size = self.__acoustic_state_dim + self.__state_dim + self.__action_dim
        self.__bn1 = torch.nn.BatchNorm1d(input_size)

        self.lstms = ModuleList(
            [LSTM(input_size, self.__lstm_sizes[0], batch_first=True)])
        self.lstms.extend([
            LSTM(self.__lstm_sizes[i - 1],
                 self.__lstm_sizes[i],
                 batch_first=True) for i in range(1, len(self.__lstm_sizes))
        ])

        self.linears = ModuleList(
            [Linear(self.__lstm_sizes[-1], self.__linears_size[0])])
        self.linears.extend([
            Linear(self.__linears_size[i - 1], self.__linears_size[i])
            for i in range(1, len(self.__linears_size))
        ])

        self.goal_mu = Linear(self.__linears_size[-1], kwargs['goal_dim'])
        self.goal_log_var = Linear(self.__linears_size[-1], kwargs['goal_dim'])

        self.state_mu = Linear(self.__linears_size[-1], kwargs['state_dim'])
        self.state_log_var = Linear(self.__linears_size[-1],
                                    kwargs['state_dim'])

        self.relu = ReLU()
        self.tanh = Tanh()

        self.apply(init_weights)  # xavier uniform init
Example #4
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.dec1 = DecoderBlock(256, 256)
        self.dec2 = DecoderBlock(256, 256)
        self.dec3 = DecoderBlock(256, 256)
        self.dec4 = DecoderBlock(256, 256)

        self.dec5 = Sequential(
            ConvBlock(256,
                      128,
                      kernel=3,
                      stride=2,
                      padding=1,
                      output_padding=1,
                      norm=lambda nb_channels: LayerNorm(
                          (nb_channels, 108, 108)),
                      conv_layer=ConvTranspose2d,
                      transposed=True),
            ConvBlock(128,
                      64,
                      kernel=3,
                      stride=2,
                      padding=1,
                      output_padding=1,
                      norm=lambda nb_channels: LayerNorm(
                          (nb_channels, 216, 216)),
                      conv_layer=ConvTranspose2d,
                      transposed=True),
            ConvTranspose2d(64,
                            kwargs.get("nb_channels_output", 3),
                            kernel_size=1,
                            stride=1,
                            padding=0), Tanh())

        self.mlp = Sequential(Linear(8 + kwargs.get("nb_domains", 3), 256),
                              ReLU(inplace=True), Linear(256, 256),
                              ReLU(inplace=True), Linear(256, 256 * 4))
Example #5
0
    def __init__(self, args, word_embeddings: TextFieldEmbedder,
                 vocab: Vocabulary) -> None:
        super().__init__(vocab)

        # parameters
        self.args = args
        self.word_embeddings = word_embeddings

        # gate
        self.x_linear = nn.Linear(self.args.embedding_size,
                                  self.args.hidden_size,
                                  bias=False)
        self.W_z = nn.Linear(self.args.hidden_size, 1, bias=False)
        self.U_z = nn.Linear(self.args.hidden_size, 1, bias=False)
        self.W_r = nn.Linear(self.args.hidden_size, 1, bias=False)
        self.U_r = nn.Linear(self.args.hidden_size, 1, bias=False)
        self.W = nn.Linear(self.args.hidden_size, 1, bias=False)
        self.U = nn.Linear(self.args.hidden_size, 1, bias=False)

        # layers
        self.event_embedding = EventEmbedding(args, self.word_embeddings)
        self.lstm = DynamicLSTM(self.args.embedding_size,
                                self.args.hidden_size,
                                num_layers=1,
                                batch_first=True)
        self.attention = Attention(self.args.hidden_size, score_function='mlp')
        self.sigmoid = Sigmoid()
        self.tanh = Tanh()
        self.score = Score(self.args.hidden_size,
                           self.args.embedding_size,
                           threshold=self.args.threshold)

        # metrics
        self.accuracy = BooleanAccuracy()
        self.f1_score = F1Measure(positive_label=1)
        self.loss_function = BCELoss()
Example #6
0
 def __init__(self, nef=64, out_channels=3, in_channels = 3, useNorm='BN'):
     super(Pix2pix64, self).__init__()
                
     # 64*64*3-->64*64*128
     self.pad1 = ReflectionPad2d(padding=1)
     self.conv1 = Conv2d(in_channels, nef, 3, 1, 0)
     # 64*64*128-->32*32*256
     self.rcb2 = RCBBlock(nef, nef*2, useNorm=useNorm)     
     # 32*32*256-->16*16*512
     self.rcb3 = RCBBlock(nef*2, nef*4, useNorm=useNorm)     
     # 16*16*512-->8*8*512
     self.rcb4 = RCBBlock(nef*4, nef*8, useNorm=useNorm)            
     # 8*8*512-->4*4*512
     self.rcb5 = RCBBlock(nef*8, nef*8, useNorm=useNorm)            
     # 4*4*512-->2*2*512
     self.rcb6 = RCBBlock(nef*8, nef*8, useNorm=useNorm)                 
     # 2*2*512-->1*1*512
     self.relu = LeakyReLU(0.2)
     self.pad2 = ReflectionPad2d(padding=1)
     self.conv7 = Conv2d(nef*8, nef*8, 4, 2, 0)
     # 1*1*512-->2*2*512 # refleaction padding size should be less than feature size
     self.rdcb7 = RDCBBlock(nef*8, nef*8, useNorm=useNorm, up=True)
     # 2*2*1024-->4*4*512
     self.rdcb6 = RDCBBlock(nef*16, nef*8, useNorm=useNorm, up=True)          
     # 4*4*1024-->8*8*512
     self.rdcb5 = RDCBBlock(nef*16, nef*8, useNorm=useNorm, up=True)          
     # 8*8*1024-->16*16*512
     self.rdcb4 = RDCBBlock(nef*16, nef*4, useNorm=useNorm, up=True)         
     # 16*16*512-->32*32*256
     self.rdcb3 = RDCBBlock(nef*8, nef*2, useNorm=useNorm, up=True)          
     # 32*32*256-->64*64*128
     self.rdcb2 = RDCBBlock(nef*4, nef, useNorm=useNorm, up=True)         
     # 64*64*128-->64*64*3
     self.pad3 = ReflectionPad2d(padding=1)
     self.dconv1 = Conv2d(nef*2, out_channels, 3, 1, 0)
     self.tanh = Tanh()
    def forward(self, z, c):
        network1 = self.input_conv1(z)
        network1 = ReLU()(network1)

        network2 = self.label_conv1(c)
        network2 = ReLU()(network2)

        network = torch.cat([network1, network2], dim=1)

        network = self.merge_conv1(network)
        network = self.merge_bn1(network)
        network = ReLU()(network)

        network = self.merge_conv2(network)
        network = self.merge_bn2(network)
        network = ReLU()(network)

        network = self.merge_conv3(network)
        network = self.merge_bn3(network)
        network = ReLU()(network)

        network = self.full_conv1(network)
        network = Tanh()(network)
        return network
    def __init__(self, A, dim_in, dim_out, activation="relu", **kwargs):

        super().__init__()

        I = np.eye(*A.shape)
        A_hat = A.copy() + I

        D = np.sum(A_hat, axis=0)
        D_inv = D**-.5
        D_diag = np.diag(D_inv)

        A_hat = D_diag * A_hat * D_diag
        A_hat = torch.from_numpy(np.matrix.astype(A_hat, dtype=np.float32))
        self.dim_in = dim_in
        self.dim_out = dim_out

        self.A_hat = A_hat
        self.W = Linear(dim_in, dim_out)
        if activation == 'relu':
            self.activation = ReLU()
        elif activation == 'tanh':
            self.activation = Tanh()
        else:
            self.activation = lambda X: X
Example #9
0
 def __init__(self, args):
     super(AttentionMILFeatures, self).__init__()
     width_fe = is_in_args(args, 'width_fe', 64)
     #self.feature_extractor = Sequential(
     #    Linear(args.feature_depth, width_fe),
     #    ReLU(),
     #    Dropout(p=args.dropout),
     #    Linear(width_fe, width_fe),
     #    ReLU(),
     #    Dropout(p=args.dropout)
     #)
     self.weight_extractor = Sequential(
         Linear(args.feature_depth,
                int(width_fe / 2)),  #width_fe, int(width_fe/2)),
         Tanh(),
         Linear(int(width_fe / 2), 1),
         Softmax(dim=-2)  # Softmax sur toutes les tuiles. somme à 1.
     )
     self.classifier = Sequential(
         Linear(args.feature_depth, width_fe),
         ReLU(),  # Added 25/09
         Dropout(p=args.dropout),  # Added 25/09
         Linear(width_fe, 1),  # Added 25/09
         Sigmoid())
Example #10
0
    def __init__(self, hidden, num_aggr, config, **kwargs):
        super(ExpandingAConv, self).__init__(aggr='add', **kwargs)
        self.hidden = hidden
        self.num_aggr = num_aggr

        if config.fea_activation == 'ELU':
            self.fea_activation = ELU()
        elif config.fea_activation == 'ReLU':
            self.fea_activation = ReLU()

        self.fea_mlp = Sequential(Linear(hidden * self.num_aggr, hidden),
                                  ReLU(), Linear(hidden,
                                                 hidden), self.fea_activation)

        self.aggr_mlp = Sequential(Linear(hidden * 2, self.num_aggr), Tanh())

        if config.BN == 'Y':
            self.BN = BN(hidden)
        else:
            self.BN = None

        self.bond_encoder = BondEncoder(emb_dim=hidden)

        self.reset_parameters()
Example #11
0
    def __init__(self, **kwargs):
        super(SimpleDeterministicModelDynamicsDeltaPredict, self).__init__()
        self.__acoustic_state_dim = kwargs['goal_dim']
        self.__action_dim = kwargs['action_dim']
        self.__state_dim = kwargs['state_dim']
        self.__acoustic_dim = 26
        self.__linears_size = kwargs['linear_layers_size']

        # input_size = self.__acoustic_state_dim + self.__state_dim + self.__action_dim
        input_size = self.__state_dim + self.__action_dim
        self.__bn1 = torch.nn.BatchNorm1d(input_size)

        self.drop = torch.nn.modules.Dropout(p=0.1)

        # self.artic_state_0 = Linear(self.__state_dim + self.__action_dim - self.__acoustic_dim, 64)
        # self.artic_state_1 = Linear(64, self.__state_dim - self.__acoustic_dim)
        self.artic_state_0 = Linear(
            self.__state_dim + self.__action_dim - self.__acoustic_dim,
            self.__state_dim - self.__acoustic_dim)
        # self.artic_state_1 = Linear(64, )

        self.linears = ModuleList([Linear(input_size, self.__linears_size[0])])
        self.linears.extend([
            Linear(self.__linears_size[i - 1], self.__linears_size[i])
            for i in range(1, len(self.__linears_size))
        ])

        self.goal = Linear(self.__linears_size[-1], kwargs['goal_dim'])
        self.acoustic_state = Linear(self.__linears_size[-1],
                                     self.__acoustic_dim)
        self.state = Linear(self.__state_dim, self.__state_dim)

        self.relu = ReLU()
        self.tanh = Tanh()

        self.apply(init_weights)  # xavier uniform init
Example #12
0
def build_layers(input_dim,
                 hidden_dims,
                 output_dim,
                 activation=Tanh,
                 output_activation=Identity):
    '''
    Returns a list of Linear and Tanh layers with the specified layer sizes
    Parameters
    ----------
    input_dim : int
        the input dimension of the first linear layer
    hidden_dims : list
        a list of type int specifying the sizes of the hidden layers
    output_dim : int
        the output dimension of the final layer in the list
    Returns
    -------
    layers : list
        a list of Linear layers, each one followed by a Tanh layer, excluding the
        final layer
    '''

    layer_sizes = [input_dim] + hidden_dims + [output_dim]
    layers = []

    for i in range(len(layer_sizes) - 1):
        act = activation if i < len(
            layer_sizes) - 2 else output_activation  # Tyna note
        layers.append(Linear(layer_sizes[i], layer_sizes[i + 1], bias=True))
        # layers.append(Linear(layer_sizes[i], layer_sizes[i + 1], bias=True), act())

        # layers += [Linear(layer_sizes[j], layer_sizes[j + 1]), act()]
        if i != len(layer_sizes) - 2:
            layers.append(Tanh())

    return layers
Example #13
0
def model_simple():
    model = Sequential(Linear(2, 2), Tanh(), Linear(2, 2))
    state_dict = model.state_dict()
    state_dict["0.weight"].copy_(
        torch.nn.Parameter(
            data=torch.Tensor([
                [1.0, 1.0],
                [0.0, 1.0],
            ]),
            requires_grad=True,
        ))
    state_dict["0.bias"].copy_(
        torch.nn.Parameter(data=torch.Tensor([0.0, 0.0]), requires_grad=True))
    state_dict["2.weight"].copy_(
        torch.nn.Parameter(
            data=torch.Tensor([
                [1.0, 0.0],
                [-0.2, 1.0],
            ]),
            requires_grad=True,
        ))
    state_dict["2.bias"].copy_(
        torch.nn.Parameter(data=torch.Tensor([0.0, 0.0]), requires_grad=True))
    return model
Example #14
0
 def __init__(self,
              image_size: int = 256,
              image_channels: int = 4,
              pose_size: int = 3,
              intermediate_channels: int = 64,
              bottleneck_image_size: int = 32,
              bottleneck_block_count: int = 6,
              initialization_method: str = 'he'):
     super().__init__()
     self.main_body = EncoderDecoderModule(
         image_size=image_size,
         image_channels=image_channels + pose_size,
         output_channels=intermediate_channels,
         bottleneck_image_size=bottleneck_image_size,
         bottleneck_block_count=bottleneck_block_count,
         initialization_method=initialization_method)
     self.pumarola_color_change = Sequential(
         Conv7(intermediate_channels, image_channels,
               initialization_method), Tanh())
     self.pumarola_alpha_mask = Sequential(
         Conv7(intermediate_channels, image_channels,
               initialization_method), Sigmoid())
     self.zhou_grid_change = Conv7(intermediate_channels, 2,
                                   initialization_method)
 def __init__(self, output_shape):
     super().__init__()
     #pipe deconvolves input vector into (2, 64, 64) image
     self.pipe = Sequential(
         ConvTranspose2d(in_channels=LATENT_VECTOR_SIZE, out_channels=GENER_FILTERS * 8,
                         kernel_size=4, stride=1, padding=0),
         BatchNorm2d(GENER_FILTERS * 8),
         ReLU(),
         ConvTranspose2d(in_channels=GENER_FILTERS * 8, out_channels=GENER_FILTERS * 4,
                         kernel_size=4, stride=2, padding=1),
         BatchNorm2d(GENER_FILTERS * 4),
         ReLU(),
         ConvTranspose2d(in_channels=GENER_FILTERS * 4, out_channels=GENER_FILTERS * 2,
                         kernel_size=4, stride=2, padding=1),
         BatchNorm2d(GENER_FILTERS * 2),
         ReLU(),
         ConvTranspose2d(in_channels=GENER_FILTERS * 2, out_channels=GENER_FILTERS,
                         kernel_size=4, stride=2, padding=1),
         BatchNorm2d(GENER_FILTERS * 1),
         ReLU(),
         ConvTranspose2d(in_channels=GENER_FILTERS, out_channels=output_shape[0],
                         kernel_size=4, stride=2, padding=1),
         Tanh()
     )
Example #16
0
    def create(self, architecture: Architecture, metadata: Metadata, arguments: Configuration) -> Any:
        # create the input layer
        input_layer = self.create_input_layer(architecture, metadata, arguments)

        # conditional
        if "conditional" in architecture.arguments:
            # wrap the input layer with a conditional layer
            input_layer = ConditionalLayer(input_layer, metadata, **architecture.arguments.conditional)

        # create the hidden layers factory
        hidden_layers_factory = self.create_other("HiddenLayers", architecture, metadata,
                                                  arguments.get("hidden_layers", {}))

        # create the output activation
        if "output_activation" in arguments:
            output_activation = self.create_other(arguments.output_activation.factory, architecture,
                                                  metadata,
                                                  arguments.output_activation.get("arguments", {}))
        else:
            output_activation = None

        # create the output layer factory
        output_layer_factory = SingleOutputLayerFactory(architecture.arguments.code_size, activation=output_activation)

        # create the encoder
        return FeedForward(input_layer, hidden_layers_factory, output_layer_factory, default_hidden_activation=Tanh())
Example #17
0
    def __init__(self,
                 embedding_dim,
                 designer_weight_decay,
                 designer_lr,
                 vae_weight_decay,
                 vae_lr,
                 device,
                 fitness_class_str=None,
                 generator_class_str=None,
                 checkpoint_dict=dict(),
                 load_fitness_net=True,
                 optimize_objectives=['success', 'stability', 'robustness'],
                 use_1_robustness=False):
        super().__init__()
        self.designer_lr = designer_lr
        self.designer_weight_decay = designer_weight_decay
        self.distributed = False

        self.vae_weight_decay = vae_weight_decay
        self.vae_lr = vae_lr
        self.embedding_dim = embedding_dim
        self.device = device
        self.gn_encoder = Encoder(self.embedding_dim).to(self.device)
        self.decoder = Decoder(self.embedding_dim).to(self.device)

        if fitness_class_str is None or fitness_class_str == "ResFitnessNet":
            fitness_class = ResFitnessNet
        elif fitness_class_str == "SpatialConcatResFitnessNet":
            fitness_class = SpatialConcatResFitnessNet
        elif fitness_class_str == "SpatialConcatResFitnessNetV2":
            fitness_class = SpatialConcatResFitnessNetV2
        elif fitness_class_str == "BasicFitnessNet":
            fitness_class = BasicFitnessNet
        else:
            raise NotImplementedError

        if generator_class_str is None \
                or generator_class_str == "BaseGenerator":
            generator_class = BaseGenerator
        elif generator_class_str == "DeepThinGenerator":
            generator_class = DeepThinGenerator
        elif generator_class_str == "DeeperNoBatchnormGenerator":
            generator_class = DeeperNoBatchnormGenerator
        elif generator_class_str == "DeeperGenerator":
            generator_class = DeeperGenerator
        else:
            raise NotImplementedError

        self.fitness_net = None
        self.fn_optimizer = None
        self.optimize_objectives = optimize_objectives
        self.use_1_robustness = use_1_robustness
        if load_fitness_net:
            self.fitness_net = fitness_class(optimize_objectives,
                                             use_1_robustness).to(self.device)
            self.fn_optimizer = Adam(self.fitness_net.parameters(),
                                     lr=self.designer_lr,
                                     weight_decay=self.designer_weight_decay)

        self.generator_net = Sequential(
            Linear(self.embedding_dim, 4096),
            BatchNorm1d(4096),
            LeakyReLU(),
            #
            Linear(4096, 4096),
            BatchNorm1d(4096),
            LeakyReLU(),
            #
            Linear(4096, self.embedding_dim * 2),
            BatchNorm1d(self.embedding_dim * 2),
            Tanh()).to(self.device)

        self.vae_optimizer = Adam(list(self.decoder.parameters()) +
                                  list(self.gn_encoder.parameters()),
                                  lr=self.vae_lr,
                                  weight_decay=self.vae_weight_decay)
        self.gn_optimizer = Adam(list(self.generator_net.parameters()) +
                                 list(self.gn_encoder.parameters()),
                                 lr=self.designer_lr,
                                 weight_decay=self.designer_weight_decay)
        self.stats = {
            'epochs': 0,
            'update_steps': 0,
            'cycles': 0,
            'fn_update_steps': 0,
            'gn_update_steps': 0,
        }

        if load_fitness_net and checkpoint_dict[
                "fn_checkpoint_path"] is not None:
            self.load_fn_checkpoint(checkpoint_dict["fn_checkpoint_path"])
        if checkpoint_dict["gn_checkpoint_path"] is not None:
            self.load_gn_checkpoint(checkpoint_dict["gn_checkpoint_path"])
        elif checkpoint_dict["ae_checkpoint_path"] is not None:
            self.load_ae_checkpoint(checkpoint_dict["ae_checkpoint_path"])
Example #18
0
    def __init__(self, input_shape, n_convfilter, \
                 n_fc_filters, h_shape, conv3d_filter_shape):
        print("initializing \"encoder\"")
        #input_shape = (self.batch_size, 3, img_w, img_h)
        super(encoder, self).__init__()
        #conv1
        self.conv1a = Conv2d(input_shape[1], n_convfilter[0], 7, padding=3)
        self.bn1a = BatchNorm2d(n_convfilter[0])
        self.conv1b = Conv2d(n_convfilter[0], n_convfilter[0], 3, padding=1)
        self.bn1b = BatchNorm2d(n_convfilter[0])

        #conv2
        self.conv2a = Conv2d(n_convfilter[0], n_convfilter[1], 3, padding=1)
        self.bn2a = BatchNorm2d(n_convfilter[1])
        self.conv2b = Conv2d(n_convfilter[1], n_convfilter[1], 3, padding=1)
        self.bn2b = BatchNorm2d(n_convfilter[1])
        self.conv2c = Conv2d(n_convfilter[0], n_convfilter[1], 1)
        self.bn2c = BatchNorm2d(n_convfilter[1])

        #conv3
        self.conv3a = Conv2d(n_convfilter[1], n_convfilter[2], 3, padding=1)
        self.bn3a = BatchNorm2d(n_convfilter[2])
        self.conv3b = Conv2d(n_convfilter[2], n_convfilter[2], 3, padding=1)
        self.bn3b = BatchNorm2d(n_convfilter[2])
        self.conv3c = Conv2d(n_convfilter[1], n_convfilter[2], 1)
        self.bn3c = BatchNorm2d(n_convfilter[2])

        #conv4
        self.conv4a = Conv2d(n_convfilter[2], n_convfilter[3], 3, padding=1)
        self.bn4a = BatchNorm2d(n_convfilter[3])
        self.conv4b = Conv2d(n_convfilter[3], n_convfilter[3], 3, padding=1)
        self.bn4b = BatchNorm2d(n_convfilter[3])

        #conv5
        self.conv5a = Conv2d(n_convfilter[3], n_convfilter[4], 3, padding=1)
        self.bn5a = BatchNorm2d(n_convfilter[4])
        self.conv5b = Conv2d(n_convfilter[4], n_convfilter[4], 3, padding=1)
        self.bn5b = BatchNorm2d(n_convfilter[4])

        #conv6
        self.conv6a = Conv2d(n_convfilter[4], n_convfilter[5], 3, padding=1)
        self.bn6a = BatchNorm2d(n_convfilter[5])
        self.conv6b = Conv2d(n_convfilter[5], n_convfilter[5], 3, padding=1)
        self.bn6b = BatchNorm2d(n_convfilter[5])

        #pooling layer
        self.pool = MaxPool2d(kernel_size=2, padding=1)

        #nonlinearities of the network
        self.leaky_relu = LeakyReLU(negative_slope=0.01)
        self.sigmoid = Sigmoid()
        self.tanh = Tanh()

        #find the input feature map size of the fully connected layer
        fc7_feat_w, fc7_feat_h = self.fc_in_featmap_size(input_shape,
                                                         num_pooling=6)
        #define the fully connected layer
        self.fc7 = Linear(int(n_convfilter[5] * fc7_feat_w * fc7_feat_h),
                          n_fc_filters[0])

        #define the FCConv3DLayers in 3d convolutional gru unit
        #conv3d_filter_shape = (self.n_deconvfilter[0], self.n_deconvfilter[0], 3, 3, 3)
        self.t_x_s_update = BN_FCConv3DLayer_torch(n_fc_filters[0],
                                                   conv3d_filter_shape,
                                                   h_shape)
        self.t_x_s_reset = BN_FCConv3DLayer_torch(n_fc_filters[0],
                                                  conv3d_filter_shape, h_shape)
        self.t_x_rs = BN_FCConv3DLayer_torch(n_fc_filters[0],
                                             conv3d_filter_shape, h_shape)
Example #19
0
X = df[df.columns[:-1]]
Y = df[df.columns[-1]]

#Coloca os dados em escala 0 a 1
minmax_scale = preprocessing.MinMaxScaler().fit(X.astype(float))
X = minmax_scale.transform(X)

#Define a Rede
#----------------------------------------------------------------
D_in = len(X[0])
D_hidden = 4
D_out = 1

#model=Linear(D_in,D_out)
#model = Sequential( Linear(D_in,D_hidden),Linear(D_hidden,D_out) )
model = Sequential(Linear(D_in, D_hidden), Tanh(), Linear(D_hidden, D_out),
                   Tanh())

minibatch_size = 2000

D_train = 20000
D_test = 10000
D_step = 1000

n_step = 0

#pode ser otimizado se utilizar "torch.from_numpy"
X_train = torch.Tensor(X[n_step * D_step:n_step * D_step + D_train])
Y_train = torch.Tensor(Y[n_step * D_step:n_step * D_step + D_train].values)
X_test = torch.Tensor(X[n_step * D_step + D_train:n_step * D_step + D_train +
                        D_test])
Example #20
0
 def __init__(self, input_shape, output_shape):
     super(energy_net, self).__init__()
     self.fc1 = Linear(input_shape, 128)
     self.fc2 = Linear(128, output_shape)
     self.act_fn = Tanh()
Example #21
0
 def _build_O_net(input_shape, output_shape):
     net = torch.nn.Sequential(Linear(input_shape, 128), Tanh(),
                               Linear(128, output_shape))
     return net.cuda()
Example #22
0
    def __init__(self, input_shape, n_convfilter, \
                 n_fc_filters, h_shape, conv3d_filter_shape):
        print("\ninitializing \"encoder\"")
        #input_shape = (self.batch_size, 3, img_w, img_h)
        super(encoder, self).__init__()
        #conv1
        conv1_kernal_size = 7
        self.conv1 = Conv2d(in_channels= input_shape[1], \
                            out_channels= n_convfilter[0], \
                            kernel_size= conv1_kernal_size, \
                            padding = int((conv1_kernal_size - 1) / 2))

        #conv2
        conv2_kernal_size = 3
        self.conv2 = Conv2d(in_channels= n_convfilter[0], \
                            out_channels= n_convfilter[1], \
                            kernel_size= conv2_kernal_size,\
                            padding = int((conv2_kernal_size - 1) / 2))

        #conv3
        conv3_kernal_size = 3
        self.conv3 = Conv2d(in_channels= n_convfilter[1], \
                            out_channels= n_convfilter[2], \
                            kernel_size= conv2_kernal_size,\
                            padding = int((conv3_kernal_size - 1) / 2))

        #conv4
        conv4_kernal_size = 3
        self.conv4 = Conv2d(in_channels= n_convfilter[2], \
                            out_channels= n_convfilter[3], \
                            kernel_size= conv2_kernal_size,\
                            padding = int((conv4_kernal_size - 1) / 2))

        #conv5
        conv5_kernal_size = 3
        self.conv5 = Conv2d(in_channels= n_convfilter[3], \
                            out_channels= n_convfilter[4], \
                            kernel_size= conv2_kernal_size,\
                            padding = int((conv5_kernal_size - 1) / 2))

        #conv6
        conv6_kernal_size = 3
        self.conv6 = Conv2d(in_channels= n_convfilter[4], \
                            out_channels= n_convfilter[5], \
                            kernel_size= conv2_kernal_size,\
                            padding = int((conv6_kernal_size - 1) / 2))

        #pooling layer
        self.pool = MaxPool2d(kernel_size=2, padding=1)

        #nonlinearities of the network
        self.leaky_relu = LeakyReLU(negative_slope=0.01)
        self.sigmoid = Sigmoid()
        self.tanh = Tanh()

        #find the input feature map size of the fully connected layer
        fc7_feat_w, fc7_feat_h = self.fc_in_featmap_size(input_shape,
                                                         num_pooling=6)
        #define the fully connected layer
        self.fc7 = Linear(int(n_convfilter[5] * fc7_feat_w * fc7_feat_h),
                          n_fc_filters[0])

        #define the FCConv3DLayers in 3d convolutional gru unit
        self.t_x_s_update = FCConv3DLayer_torch(n_fc_filters[0],
                                                conv3d_filter_shape, h_shape)
        self.t_x_s_reset = FCConv3DLayer_torch(n_fc_filters[0],
                                               conv3d_filter_shape, h_shape)
        self.t_x_rs = FCConv3DLayer_torch(n_fc_filters[0], conv3d_filter_shape,
                                          h_shape)
Example #23
0
def test_calcs():
    """Gaussian/Neural non-periodic standard.

    Checks that the answer matches that expected from previous Mathematica
    calculations.
    """

    #: Making the list of non-periodic images
    images = [
        Atoms(
            symbols="PdOPd2",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 0.0, 0.0], [0.0, 2.0, 0.0],
                                [0.0, 0.0, 3.0], [1.0, 0.0, 0.0]]),
        ),
        Atoms(
            symbols="PdOPd2",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 1.0, 0.0], [1.0, 2.0, 1.0],
                                [-1.0, 1.0, 2.0], [1.0, 3.0, 2.0]]),
        ),
        Atoms(
            symbols="PdO",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[2.0, 1.0, -1.0], [1.0, 2.0, 1.0]]),
        ),
        Atoms(
            symbols="Pd2O",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[-2.0, -1.0, -1.0], [1.0, 2.0, 1.0],
                                [3.0, 4.0, 4.0]]),
        ),
        Atoms(
            symbols="Cu",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 0.0, 0.0]]),
        ),
    ]

    # Parameters
    hiddenlayers = {"O": (2, ), "Pd": (2, ), "Cu": (2, )}

    Gs = {}
    Gs["G2_etas"] = [0.2]
    Gs["G2_rs_s"] = [0]
    Gs["G4_etas"] = [0.4]
    Gs["G4_zetas"] = [1]
    Gs["G4_gammas"] = [1]
    Gs["cutoff"] = 6.5

    elements = ["O", "Pd", "Cu"]

    G = make_symmetry_functions(elements=elements,
                                type="G2",
                                etas=Gs["G2_etas"])
    G += make_symmetry_functions(
        elements=elements,
        type="G4",
        etas=Gs["G4_etas"],
        zetas=Gs["G4_zetas"],
        gammas=Gs["G4_gammas"],
    )
    amp_images = amp_hash(images)
    descriptor = Gaussian(Gs=G, cutoff=Gs["cutoff"])
    descriptor.calculate_fingerprints(amp_images, calculate_derivatives=True)
    fingerprints_range = calculate_fingerprints_range(descriptor, amp_images)
    np.random.seed(1)
    O_weights_1 = np.random.rand(10, 2)
    O_weights_2 = np.random.rand(1, 3).reshape(-1, 1)
    np.random.seed(2)
    Pd_weights_1 = np.random.rand(10, 2)
    Pd_weights_2 = np.random.rand(1, 3).reshape(-1, 1)
    np.random.seed(3)
    Cu_weights_1 = np.random.rand(10, 2)
    Cu_weights_2 = np.random.rand(1, 3).reshape(-1, 1)

    weights = OrderedDict([
        ("O", OrderedDict([(1, O_weights_1), (2, O_weights_2)])),
        ("Pd", OrderedDict([(1, Pd_weights_1), (2, Pd_weights_2)])),
        ("Cu", OrderedDict([(1, Cu_weights_1), (2, Cu_weights_2)])),
    ])

    scalings = OrderedDict([
        ("O", OrderedDict([("intercept", 0), ("slope", 1)])),
        ("Pd", OrderedDict([("intercept", 0), ("slope", 1)])),
        ("Cu", OrderedDict([("intercept", 0), ("slope", 1)])),
    ])

    calc = Amp(
        descriptor,
        model=NeuralNetwork(
            hiddenlayers=hiddenlayers,
            weights=weights,
            scalings=scalings,
            activation="tanh",
            fprange=fingerprints_range,
            mode="atom-centered",
            fortran=False,
        ),
        logging=False,
    )

    amp_energies = [calc.get_potential_energy(image) for image in images]
    amp_forces = [calc.get_forces(image) for image in images]
    amp_forces = np.concatenate(amp_forces)

    torch_O_weights_1 = torch.FloatTensor(O_weights_1[:-1, :]).t()
    torch_O_bias_1 = torch.FloatTensor(O_weights_1[-1, :])
    torch_O_weights_2 = torch.FloatTensor(O_weights_2[:-1, :]).t()
    torch_O_bias_2 = torch.FloatTensor(O_weights_2[-1, :])
    torch_Pd_weights_1 = torch.FloatTensor(Pd_weights_1[:-1, :]).t()
    torch_Pd_bias_1 = torch.FloatTensor(Pd_weights_1[-1, :])
    torch_Pd_weights_2 = torch.FloatTensor(Pd_weights_2[:-1, :]).t()
    torch_Pd_bias_2 = torch.FloatTensor(Pd_weights_2[-1, :])
    torch_Cu_weights_1 = torch.FloatTensor(Cu_weights_1[:-1, :]).t()
    torch_Cu_bias_1 = torch.FloatTensor(Cu_weights_1[-1, :])
    torch_Cu_weights_2 = torch.FloatTensor(Cu_weights_2[:-1, :]).t()
    torch_Cu_bias_2 = torch.FloatTensor(Cu_weights_2[-1, :])

    device = "cpu"
    dataset = AtomsDataset(
        images,
        descriptor=Gaussian,
        cores=1,
        label="consistency",
        Gs=Gs,
        forcetraining=True,
    )

    fp_length = dataset.fp_length
    batch_size = len(dataset)
    dataloader = DataLoader(dataset,
                            batch_size,
                            collate_fn=collate_amp,
                            shuffle=False)
    model = FullNN(elements, [fp_length, 2, 2], device, forcetraining=True)
    model.state_dict()["elementwise_models.O.model_net.0.weight"].copy_(
        torch_O_weights_1)
    model.state_dict()["elementwise_models.O.model_net.0.bias"].copy_(
        torch_O_bias_1)
    model.state_dict()["elementwise_models.O.model_net.2.weight"].copy_(
        torch_O_weights_2)
    model.state_dict()["elementwise_models.O.model_net.2.bias"].copy_(
        torch_O_bias_2)
    model.state_dict()["elementwise_models.Pd.model_net.0.weight"].copy_(
        torch_Pd_weights_1)
    model.state_dict()["elementwise_models.Pd.model_net.0.bias"].copy_(
        torch_Pd_bias_1)
    model.state_dict()["elementwise_models.Pd.model_net.2.weight"].copy_(
        torch_Pd_weights_2)
    model.state_dict()["elementwise_models.Pd.model_net.2.bias"].copy_(
        torch_Pd_bias_2)
    model.state_dict()["elementwise_models.Cu.model_net.0.weight"].copy_(
        torch_Cu_weights_1)
    model.state_dict()["elementwise_models.Cu.model_net.0.bias"].copy_(
        torch_Cu_bias_1)
    model.state_dict()["elementwise_models.Cu.model_net.2.weight"].copy_(
        torch_Cu_weights_2)
    model.state_dict()["elementwise_models.Cu.model_net.2.bias"].copy_(
        torch_Cu_bias_2)
    import torch.nn as nn
    for name, layer in model.named_modules():
        if isinstance(layer, MLP):
            layer.model_net = nn.Sequential(layer.model_net, Tanh())

    for batch in dataloader:
        x = to_tensor(batch[0], device)
        y = to_tensor(batch[1], device)
        energy_pred, force_pred = model(x)
    for idx, i in enumerate(amp_energies):
        assert round(i, 4) == round(
            energy_pred.tolist()[idx][0],
            4), "The predicted energy of image %i is wrong!" % (idx + 1)
    print("Energy predictions are correct!")
    for idx, sample in enumerate(amp_forces):
        for idx_d, value in enumerate(sample):
            predict = force_pred.tolist()[idx][idx_d]
            assert abs(value - predict) < 0.0001, (
                "The predicted force of image % i, direction % i is wrong! Values: %s vs %s"
                % (idx + 1, idx_d, value, force_pred.tolist()[idx][idx_d]))
    print("Force predictions are correct!")
Example #24
0
 def weight_fn(x):
     return 1 + Tanh(x)
Example #25
0
 def setUp(self):
     self.tanh = Tanh()
     self.atanh = Atanh()
     self.pwln = PWLNormalizor(2, 16, mean=1.4, std=2.5)
Example #26
0
def torch_fn():
    """Create a tanh layer in torch."""
    return Tanh()
from torch.nn import ReLU, Tanh, Sigmoid, ELU, LeakyReLU

ACTIVATIONS = {
    'relu': ReLU(),
    'tanh': Tanh(),
    'sigmoid': Sigmoid(),
    'elu': ELU(),
    'leaky_relu': LeakyReLU()
}

GNN_MSG = 'gnn_msg'
GNN_NODE_FEAT_IN = 'feat'
GNN_NODE_FEAT_OUT = 'gnn_node_feat_out'
GNN_AGG_MSG = 'gnn_agg_msg'
GNN_EDGE_WEIGHT = 'gnn_edge_weight'
Example #28
0
    def __init__(self, input_nc=1, output_nc=1, depth=32, momentum=0.8,dropout = 0):
        super(UnetGenerator, self).__init__()
        '''
        # 256 x 256
        self.e1 = nn.Sequential(nn.Conv2d(input_nc,depth,kernel_size=4,stride=2,padding=1),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 128 x 128
        self.e2 = nn.Sequential(nn.Conv2d(depth,depth*2,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*2),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 64 x 64
        self.e3 = nn.Sequential(nn.Conv2d(depth*2,depth*4,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*4),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 32 x 32
        self.e4 = nn.Sequential(nn.Conv2d(depth*4,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 16 x 16
        self.e5 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 8 x 8
        self.e6 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 4 x 4
        self.e7 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 2 x 2
        self.e8 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 1 x 1
        self.d1 = nn.Sequential(nn.ConvTranspose2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 2 x 2
        self.d2 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 4 x 4
        self.d3 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 8 x 8
        self.d4 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8))
        # 16 x 16
        self.d5 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*4,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*4))
        # 32 x 32
        self.d6 = nn.Sequential(nn.ConvTranspose2d(depth*4*2,depth*2,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*2))
        # 64 x 64
        self.d7 = nn.Sequential(nn.ConvTranspose2d(depth*2*2,depth,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth))
        # 128 x 128
        self.d8 = nn.ConvTranspose2d(depth*2,output_nc,kernel_size=4,stride=2,padding=1)
        # 256 x 256
        self.relu = nn.ReLU(inplace=True)
        self.tanh = nn.Tanh()
    def forward(self,x):
        # encoder
        out_e1 = self.e1(x)              # 128 x 128
        out_e2 = self.e2(out_e1)             # 64 x 64
        out_e3 = self.e3(out_e2)             # 32 x 32
        out_e4 = self.e4(out_e3)             # 16 x 16
        out_e5 = self.e5(out_e4)             # 8 x 8
        out_e6 = self.e6(out_e5)             # 4 x 4
        out_e7 = self.e7(out_e6)             # 2 x 2
        out_e8 = self.e8(out_e7)             # 1 x 1
        # decoder
        out_d1 = self.d1(self.relu(out_e8))  # 2 x 2
        out_d1_ = torch.cat((out_d1, out_e7),1)
        out_d2 = self.d2(self.relu(out_d1_)) # 4 x 4
        out_d2_ = torch.cat((out_d2, out_e6),1)
        out_d3 = self.d3(self.relu(out_d2_)) # 8 x 8
        out_d3_ = torch.cat((out_d3, out_e5),1)
        out_d4 = self.d4(self.relu(out_d3_)) # 16 x 16
        out_d4_ = torch.cat((out_d4, out_e4),1)
        out_d5 = self.d5(self.relu(out_d4_)) # 32 x 32
        out_d5_ = torch.cat((out_d5, out_e3),1)
        out_d6 = self.d6(self.relu(out_d5_)) # 64 x 64
        out_d6_ = torch.cat((out_d6, out_e2),1)
        out_d7 = self.d7(self.relu(out_d6_)) # 128 x 128
        out_d7_ = torch.cat((out_d7, out_e1),1)
        out_d8 = self.d8(self.relu(out_d7_)) # 256 x 256
        out = self.tanh(out_d8)
        return out
        '''
        # input_img = Input(shape=self.img_shape)  # 256, 256, 1
        # # Downsampling
        # d0 = conv2d(input_img, depth * 1, bn=False, stride=1, name="d0")  # 256, 256, 16
        # d0 = conv2d(d0, depth * 1, bn=False, f_size=3, stride=1, name="d0_1")  # 256, 256, 16
        # d1 = conv2d(d0, depth * 2, bn=False, name="d1")  # 128, 128, 32
        # d2 = conv2d(d1, depth * 2, name="d2")  # 64, 64, 32
        # d2 = conv2d(d2, depth * 2, f_size=3, stride=1, name="d2_1")  # 64, 64, 32
        # d3 = conv2d(d2, depth * 4, name="d3")  # 32, 32, 64
        # d4 = conv2d(d3, depth * 4, name="d4")  # 16, 16, 64
        # d4 = conv2d(d4, depth * 4, f_size=3, stride=1, name="d4_1")  # 16, 16, 64
        # d5 = conv2d(d4, depth * 8, name="d5")  # 8, 8, 128
        # d6 = conv2d(d5, depth * 8, name="d6")  # 4, 4, 128
        # d6 = conv2d(d6, depth * 8, f_size=3, stride=1, name="d6_1")  # 4, 4, 128
        # d7 = conv2d(d6, depth * 16, name="d7")  # 2, 2, 256
        # d8 = conv2d(d7, depth * 16, f_size=2, stride=1, padding="valid", name="d8")
        self.depth = depth
        self.d0 = Sequential(Conv2d(input_nc, depth, 7, 1, 3), LeakyReLU(0.2),
                             Conv2d(depth, depth, 5, 1, 2), LeakyReLU(0.2))  # 256, 256, 16
        self.d1 = Sequential(Conv2d(depth, depth * 2, 4, 2, 1), LeakyReLU(0.2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2))  # 128, 128, 32
        self.d2 = Sequential(Conv2d(depth * 2, depth * 2, 4, 2, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2))  # 64, 64, 32
        self.d3 = Sequential(Conv2d(depth * 2, depth * 4, 4, 2, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2))  # 32, 32, 64
        self.d4 = Sequential(Conv2d(depth * 4, depth * 4, 4, 2, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2))  # 16, 16, 64
        self.d5 = Sequential(Conv2d(depth * 4, depth * 8, 4, 2, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2))  # 8, 8, 128
        self.d6 = Sequential(Conv2d(depth * 8, depth * 8, 4, 2, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2))  # 4, 4, 128
        self.d7 = Sequential(Conv2d(depth * 8, depth * 16, 4, 2, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2))  # 2, 2, 256
        self.d8 = Sequential(Conv2d(depth * 16, depth * 16, 4, 2, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             )  # 2, 2, 256

        # # Upsampling
        # u0 = deconv2d(d8, d7, depth * 16)  # 2, 2, depth * 16  + depth * 16,depth * 8
        # u1 = deconv2d(u0, d6, depth * 8)  # 4, 4, depth * 8  + depth * 8,depth * 8
        # u2 = deconv2d(u1, d5, depth * 8)  # 8 ,8, depth * 8 + depth * 8,depth * 4
        # u3 = deconv2d(u2, d4, depth * 4)  # 16,16,depth * 4 + depth * 4,depth * 4
        # u4 = deconv2d(u3, d3, depth * 4)  # 32,32,depth * 4 + depth * 4,depth * 2
        # u5 = deconv2d(u4, d2, depth * 2)  # 64,64,depth * 2 + depth * 2,depth * 2
        # u6 = deconv2d(u5, d1, depth * 2)  # 128,128,depth * 2 + depth * 2,depth * 2
        # u7 = deconv2d(u6, d0, depth * 1)  # 256,256,depth * 2 + depth * 2, depth * 1
        # # u8 = UpSampling2D(size=2)(u7)  #256,256,depth * 2 + depth * 2,depth * 2
        # u8 = Conv2D(self.channels, kernel_size=5, strides=1, padding='same', activation=None)(u7)  # 256,256,1
        # output_img = Activation(sigmoid10)(u8)
        self.u0 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1), BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u1 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1), BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u2 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1) ,BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u3 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1), BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u4 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u5 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u6 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 1, depth * 1, 3, 1, 1), BatchNorm2d(depth * 1, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        # self.u7 = Sequential(Upsample(scale_factor=2),
        #                      Conv2d(depth * 1, depth * 1, 3, 1, 1), LeakyReLU(0.2),
        #                      Dropout(0.1), BatchNorm2d(depth * 1, momentum=momentum))
        self.u7 = Sequential(Conv2d(depth * 1, output_nc, 5, 1, 2), Tanh())
        # self.conv1 = Conv2d(input_nc, depth, 4, 2, 1)
        # self.conv2 = Conv2d(depth, depth * 2, 4, 2, 1)
        # self.conv3 = Conv2d(depth * 2, depth * 4, 4, 2, 1)
        # self.conv4 = Conv2d(depth * 4, depth * 8, 4, 2, 1)
        # self.conv5 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv6 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv7 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv8 = Conv2d(depth * 8, depth * 8, 4, 2, 1)

        # self.dconv1 = ConvTranspose2d(depth * 8, depth * 8, 4, 2, 1)
        # self.dconv2 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv3 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv4 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv5 = ConvTranspose2d(depth * 8 * 2, depth * 4, 4, 2, 1)
        # self.dconv6 = ConvTranspose2d(depth * 4 * 2, depth * 2, 4, 2, 1)
        # self.dconv7 = ConvTranspose2d(depth * 2 * 2, depth, 4, 2, 1)
        # self.dconv8 = ConvTranspose2d(depth * 2, output_nc, 4, 2, 1)

        # self.batch_norm = BatchNorm2d(depth)
        # self.batch_norm2 = BatchNorm2d(depth * 2)
        # self.batch_norm4 = BatchNorm2d(depth * 4)
        # self.batch_norm8 = BatchNorm2d(depth * 8)
        #
        self.leaky_relu = LeakyReLU(0.2)
        # self.relu = ReLU(True)

        # self.dropout = Dropout(0.2)

        # self.sigmoid = Sigmoid()
        self.conv_32_8 = Conv2d(depth * 16 * 2, depth * 8, 3, 1, 1)
        self.conv_16_8 = Conv2d(depth * 8 * 2, depth * 8, 3, 1, 1)
        self.conv_16_4 = Conv2d(depth * 8 * 2, depth * 4, 3, 1, 1)
        self.conv_8_4 = Conv2d(depth * 4 * 2, depth * 4, 3, 1, 1)
        self.conv_8_2 = Conv2d(depth * 4 * 2, depth * 2, 3, 1, 1)
        self.conv_4_2 = Conv2d(depth * 2 * 2, depth * 2, 3, 1, 1)
        self.conv_4_1 = Conv2d(depth * 2 * 2, depth * 1, 3, 1, 1)
        self.conv_2_1 = Conv2d(depth * 1 * 2, depth * 1, 3, 1, 1)
import torch as t
from torch.nn import Conv1d as conv, MaxPool1d as maxpool, Linear as fc, Softmax, ReLU, Dropout, Tanh, BatchNorm1d as BN
from HW1.utils import variable
import torchtext
from torchtext.vocab import Vectors, GloVe


softmax = Softmax()
dropout = Dropout()
relu = ReLU()
tanh = Tanh()


def vectorize(text, TEXT, vdim=300):
    length, batch_size = text.data.numpy().shape
    return t.cat([TEXT.vocab.vectors[text.long().data.transpose(0,1)[i]].view(1,length,vdim) for i in range(batch_size)]).permute(0,2,1)


class Convnet(t.nn.Module):
    def __init__(self):
        super(Convnet, self).__init__()
        self.conv1 = conv(300, 50, 3, padding=1)
        # self.conv2 = conv(50, 50, 3, padding=1)
        self.fc2 = fc(50, 2)

    def forward(self, x):
        dropout = Dropout(.25)
        xx = relu(self.conv1(x))
        xx = self.conv2(xx)
        xx = relu(t.max(xx, -1)[0])
        xx = dropout(xx)
Example #30
0
    def __init__(self, feature_extractor_params, memory_shape=(128, 40),
                 controller_size=200, nb_reads=1):
        """
        In the constructor we instantiate an lstm module
        """
        super(MANNNetwork, self).__init__()
        self.feature_extractor = FeaturesExtractorFactory()(**feature_extractor_params)
        self.memory_shape = memory_shape
        self.controller_size = controller_size
        self.nb_reads = nb_reads

        self.controller = LSTM(self.feature_extractor.output_dim + 1, self.controller_size)
        self.controller_to_key = Sequential(Linear(self.controller_size, memory_shape[1]), Tanh())
        self.controller_to_sigma = Sequential(Linear(self.controller_size, 1), Sigmoid())
        self.gamma = Parameter(torch.FloatTensor([0.95]), requires_grad=True)
        self.output_layer = Linear(memory_shape[1] + controller_size, 1)