<reponame>vandykai/ailib<filename>ailib/models/text_cls/cls_fasttext.py<gh_stars>0
from ailib.models.base_model import BaseModule
import torch, torch.nn.functional as F
from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, LongTensor, ShortTensor, Tensor
from torch import nn, optim, as_tensor
from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset
from torch.nn.utils import weight_norm, spectral_norm

class Config(object):

    """配置参数"""
    def __init__(self):
        self.model_name = 'FastText'
        self.embedding_pretrained = None                                # 预训练词向量
        self.n_classes = 2                                              # 类别数
        self.n_vocab = 0                                                # 词表大小，在运行时赋值
        self.padding_idx = 0                                            # embedding层padding_idx
        self.learning_rate = 1e-3                                       # 学习率
        self.embed_size = self.embedding_pretrained.size(1)\
            if self.embedding_pretrained is not None else 300           # 字向量维度, 若使用了预训练词向量，则维度统一
        self.hidden_size = 128                                          # lstm隐藏层

class Model(BaseModule):

    def __init__(self, config):
        super().__init__()
        if config.embedding_pretrained is not None:
            self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False)
        else:
            self.embedding = nn.Embedding(config.n_vocab, config.embed_size, padding_idx=config.padding_idx)

        self.fc = nn.Sequential(
            nn.Linear(config.embed_size, config.hidden_size),
            nn.BatchNorm1d(config.hidden_size),
            nn.ReLU(inplace=True),
            nn.Linear(config.hidden_size, config.n_classes)
        )

    def init_weights(self, pretrained_word_vectors=None, is_static=False):
        if pretrained_word_vectors:
            self.embedding.weight = nn.Parameter(torch.from_numpy(pretrained_word_vectors).float())
        else:
            nn.init.uniform_(self.embedding.weight, -1.0, 1.0)

        if is_static:
            self.embedding.weight.requires_grad = False

    def forward(self, inputs):
        out = self.embedding(inputs)
        out = self.fc(torch.mean(out, dim=1)) # (batch, n_classes)
        return out

from ailib.models.base_model import BaseModule
import torch, torch.nn.functional as F
from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, LongTensor, ShortTensor, Tensor
from torch import nn, optim, as_tensor
from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset
from torch.nn.utils import weight_norm, spectral_norm

class Config(object):

    """配置参数"""
    def __init__(self):
        self.model_name = 'FastText'
        self.embedding_pretrained = None                                # 预训练词向量
        self.n_classes = 2                                              # 类别数
        self.n_vocab = 0                                                # 词表大小，在运行时赋值
        self.padding_idx = 0                                            # embedding层padding_idx
        self.learning_rate = 1e-3                                       # 学习率
        self.embed_size = self.embedding_pretrained.size(1)\
            if self.embedding_pretrained is not None else 300           # 字向量维度, 若使用了预训练词向量，则维度统一
        self.hidden_size = 128                                          # lstm隐藏层

class Model(BaseModule):

    def __init__(self, config):
        super().__init__()
        if config.embedding_pretrained is not None:
            self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False)
        else:
            self.embedding = nn.Embedding(config.n_vocab, config.embed_size, padding_idx=config.padding_idx)

        self.fc = nn.Sequential(
            nn.Linear(config.embed_size, config.hidden_size),
            nn.BatchNorm1d(config.hidden_size),
            nn.ReLU(inplace=True),
            nn.Linear(config.hidden_size, config.n_classes)
        )

    def init_weights(self, pretrained_word_vectors=None, is_static=False):
        if pretrained_word_vectors:
            self.embedding.weight = nn.Parameter(torch.from_numpy(pretrained_word_vectors).float())
        else:
            nn.init.uniform_(self.embedding.weight, -1.0, 1.0)

        if is_static:
            self.embedding.weight.requires_grad = False

    def forward(self, inputs):
        out = self.embedding(inputs)
        out = self.fc(torch.mean(out, dim=1)) # (batch, n_classes)
        return out
配置参数 # 预训练词向量 # 类别数 # 词表大小，在运行时赋值 # embedding层padding_idx # 学习率 # 字向量维度, 若使用了预训练词向量，则维度统一 # lstm隐藏层 # (batch, n_classes)