Пример #1
0
    def create_emb(self, m, ln):
        emb_l = nn.ModuleList()
        for i in range(0, ln.size):
            n = ln[i]
            # construct embedding operator
            if self.qr_flag and n > self.qr_threshold:
                EE = QREmbeddingBag(n,
                                    m,
                                    self.qr_collisions,
                                    operation=self.qr_operation,
                                    mode="sum",
                                    sparse=True)
            elif self.md_flag and n > self.md_threshold:
                _m = m[i]
                base = max(m)
                EE = PrEmbeddingBag(n, _m, base)
                # use np initialization as below for consistency...
                W = np.random.uniform(low=-np.sqrt(1 / n),
                                      high=np.sqrt(1 / n),
                                      size=(n, _m)).astype(np.float32)
                EE.embs.weight.data = torch.tensor(W, requires_grad=True)

            else:
                #embeddingbagは入力データに前後関係がない場合に使用され、入力を一次元にすることでメモリと計算量を効率化している
                #ただし、一次元にすると各レコードの切れ目がわからないのでfoward計算をするときはoffsetをともに入力する、後のlS_oがそれである。
                #出力は普通のembeddingの出力を列方向に足し合わせたものである(でも結局入力の特徴量で1が立ってるのが1つだけなのであまり変わらない気もする)

                EE = nn.EmbeddingBag(n, m, mode="sum", sparse=True)

                # initialize embeddings
                # nn.init.uniform_(EE.weight, a=-np.sqrt(1 / n), b=np.sqrt(1 / n))
                W = np.random.uniform(low=-np.sqrt(1 / n),
                                      high=np.sqrt(1 / n),
                                      size=(n, m)).astype(np.float32)
                # approach 1
                EE.weight.data = torch.tensor(W, requires_grad=True)
                # approach 2
                # EE.weight.data.copy_(torch.tensor(W))
                # approach 3
                # EE.weight = Parameter(torch.tensor(W),requires_grad=True)

            emb_l.append(EE)

        return emb_l
Пример #2
0
    def create_emb(self, m, ln):
        emb_l = nn.ModuleList()
        for i in range(0, ln.size):
            n = ln[i]
            # construct embedding operator
            if self.qr_flag and n > self.qr_threshold:
                EE = QREmbeddingBag(n,
                                    m,
                                    self.qr_collisions,
                                    operation=self.qr_operation,
                                    mode="sum",
                                    sparse=True)
            elif self.md_flag and n > self.md_threshold:
                _m = m[i]
                base = max(m)
                EE = PrEmbeddingBag(n, _m, base)
                # use np initialization as below for consistency...
                W = np.random.uniform(low=-np.sqrt(1 / n),
                                      high=np.sqrt(1 / n),
                                      size=(n, _m)).astype(np.float32)
                EE.embs.weight.data = torch.tensor(W, requires_grad=True)

            else:
                EE = nn.EmbeddingBag(n, m, mode="sum", sparse=True)

                # initialize embeddings
                # nn.init.uniform_(EE.weight, a=-np.sqrt(1 / n), b=np.sqrt(1 / n))
                W = np.random.uniform(low=-np.sqrt(1 / n),
                                      high=np.sqrt(1 / n),
                                      size=(n, m)).astype(np.float32)
                # approach 1
                EE.weight.data = torch.tensor(W, requires_grad=True)
                # approach 2
                # EE.weight.data.copy_(torch.tensor(W))
                # approach 3
                # EE.weight = Parameter(torch.tensor(W),requires_grad=True)

            emb_l.append(EE)

        return emb_l