Пример #1
0
 def torchify(self, vectors):
     """
     Convert a list of vectors to torch tensors
     """
     output = []
     for vector in vectors:
         if not torch.is_tensor(vector):
             vector = pu.torchvar(vector, ttype=self.get('ttype'))
         output.append(vector)
     return output
Пример #2
0
    def __init__(self, vec_dim, dim, options={}):
        """
        Instantiate the object and set options
        """
        print("options:", options)
        self.set_options(options, default)
        options['ttype'] = self.get('ttype')
        self.validation_graph = None
        # self.bilstm           = StackedBiLSTM(vec_dim, dim, options=options)         # A Bi-directional LSTM
        self.bilstm = SimpleBiLSTM(dim,
                                   options=options)  # A Bi-directional LSTM
        self.embedder = Embedder(
            vec_dim, dim, options=options
        )  # Embedder (mostly for upscaling incoming vectors)
        self.class_weights = pu.torchvar(self.get('class_weights'),
                                         ttype=self.get('ttype'))

        # Loss functions
        self.coef1 = pu.torchvar([0.1])
        self.coef2 = pu.torchvar([0.2])
        self.coef3 = pu.torchvar([0.3])
        self.coef4 = pu.torchvar([0.4])
        self.accuracyLoss = pu.AccuracyLoss()
        self.skewLoss = pu.SkewedL1Loss(self.class_weights)
        self.criterion_CE = nn.CrossEntropyLoss(weight=self.class_weights,
                                                size_average=False)
        self.criterion_MSE = nn.MSELoss()
        self.orig_loss = None

        if torch.cuda.is_available():
            self.bilstm = self.bilstm.cuda()
            self.coef1 = self.coef1.cuda()
            self.coef2 = self.coef2.cuda()
            self.coef3 = self.coef3.cuda()
            self.coef4 = self.coef4.cuda()
            self.accuracyLoss = self.accuracyLoss.cuda()
            self.skewLoss = self.skewLoss.cuda()
            self.criterion_CE = self.criterion_CE.cuda()
            self.criterion_MSE = self.criterion_MSE.cuda()

        self.eval_mode()  # eval mode by default
Пример #3
0
    def vectorize(self, char):
        """
        Return the desired vector
        """
        if self.get('embedding_matrix'):
            index = pu.torchvar(self.vocab[char], ttype=torch.LongTensor)
            vec = self.embed_matrix(index).double()
            vec = torch.squeeze(vec)
            # pu.print_info(vec)
            return vec

        else:
            vec = self.vectors.get(char)
            return vec
Пример #4
0
    def __init__(self, bilstm, embedder, batch, options={}):
        """
        Instantiate the object, create the tensors, tie them together
        """
        self.set_options(options, default)
        self.bilstm = bilstm
        self.embedder = embedder  # Layers to (if needed) bring the dimensionality of the vectors in line with what the network expects
        self.labels = None  # Will hold all labels from this batch
        self.vector_list = [
        ]  # List of list of vectors (one list of vectors for each sample)
        self.preds = None

        try:  # Collect Labels (Assuming they aren't all 'None')
            # First, Collate a stacked tensor containing all labels from all samples in this batch
            for item in batch:
                labels, vectors = item  # A tuple related to one sample-- (list of N-1 labels, list of N char-vectors)
                if labels is None:
                    raise ValueException("Unlabeled Data")

                labels = pu.torchvar(
                    labels, ttype=torch.LongTensor
                )  # Convert to tensor (on GPU if available).  'torch.long' because the criterion needs ints
                if self.labels is None:
                    self.labels = labels
                else:
                    self.labels = pu.tensor_cat(self.labels, labels,
                                                0)  # Build a stack of tensors
        except:
            pass  # In the case of real-world data, the labels will all be 'None'

        # Second, Collate a stacked tensor containing all vectors from all samples in this batch (these will be used to make predictions later)
        for item in batch:
            labels, vectors = item  # A tuple related to one sample-- (list of N-1 labels, list of N char-vectors)
            # (this time ignoring 'labels' -- Labels were done separately to make this function more easily read by a human)
            vectors = self.torchify(
                vectors)  # Convert to a list of tensors (on GPU if available)
            if not self.get('embedding_matrix'):
                vectors = self.embed_vectors(
                    vectors
                )  # If needed, convert using a layer to the right dimensionality
            self.vector_list.append(vectors)
 def random_vector():
     vec = []
     for i in range(14):
         vec.append(2 * random.random() - 1)
     vec = pu.torchvar(vec, ttype=torch.DoubleTensor)
     return vec