Ejemplo n.º 1
0
def get_net(net_path,
            input_dim,
            hidden_dims,
            use_cuda=torch.cuda.is_available()):
    net = extract_sdae_model(input_dim=input_dim, hidden_dims=hidden_dims)
    checkpoint = torch.load(net_path)
    net.load_state_dict(checkpoint['state_dict'])
    if use_cuda:
        net.cuda()
    return net
def load_pretrained_fd_autoencoder(net_filename,
                                   use_cuda=torch.cuda.is_available()):
    """
    load pretrained stack denoise autoencoder
    """
    checkpoint = torch.load(net_filename)
    fd_ae = extract_sdae_model(cfg.INPUT_DIM, cfg.HIDDEN_DIMS)
    # there some problems when loading cuda pretrained models
    fd_ae.load_state_dict(checkpoint['state_dict'])
    if use_cuda:
        fd_ae.cuda()
    return fd_ae
Ejemplo n.º 3
0
    def __init__(self, root_dir, batch_size=256, n_clusters=4, fd_hidden_dim=10, layer_norm=True, lr=0.001,
                 direct_update=False, maxiter=2e4, update_interval=140, tol=0.001, gamma=0.1,
                 fine_tune_infersent=False, use_vat=False, use_tensorboard=False, semi_supervised=False, split_sents=False, id=0, verbose=True):
        # model's settings
        self.root_dir = root_dir
        self.batch_size = batch_size
        self.fd_hidden_dim = fd_hidden_dim
        self.n_clusters = n_clusters
        self.layer_norm = layer_norm
        self.use_vat = use_vat
        self.semi_supervised = semi_supervised
        self.lr = lr
        self.direct_update = direct_update
        self.maxiter = maxiter
        self.update_interval = update_interval
        self.tol = tol
        self.gamma = gamma
        self.fine_tune_infersent = fine_tune_infersent
        self.verbose = verbose
        self.use_tensorboard = use_tensorboard
        self.id = id
        self.use_cuda = torch.cuda.is_available()
        self.split_sents = split_sents
        # data loader
        self.corpus_loader = Corpus_Loader(self.root_dir,
                                           layer_norm=self.layer_norm,
                                           verbose=self.verbose,
                                           use_cuda=self.use_cuda,
                                           semi_supervised=self.semi_supervised,
                                           split_sents=self.split_sents)
        # model's components
        self.kmeans = None
        # self.fd_ae = extract_sdae_text(dim=fd_hidden_dim)
        self.fd_ae = extract_sdae_model(input_dim=cfg.INPUT_DIM, hidden_dims=cfg.HIDDEN_DIMS)

        self.cluster_layer = None
        self.ae_criteron = nn.MSELoss()
        self.cluster_criteron = F.binary_cross_entropy
        self.optimizer = None
        # model's state
        self.current_p = None
        self.current_q = None
        self.current_pred_labels = None
        self.past_pred_labels = None
        self.current_cluster_acc = None
        self.current_cluster_nmi = None
        self.current_cluster_ari = None
        # model's logger
        self.logger_tensorboard = None
        # initialize model's parameters and update current state
        self.initialize_model()
        self.initialize_tensorboard()
Ejemplo n.º 4
0
    use_cuda = torch.cuda.is_available()
    random_seed = args.seed
    recons_lam = args.recons_lam
    cluster_lam = args.cluster_lam
    batch_size = args.batch_size
    tol = args.tol
    lr = args.lr

    initialize_environment(random_seed=random_seed, use_cuda=use_cuda)

    feat_path = os.path.join(data_dir, cfg.TRAIN_TEXT_FEAT_FILE_NAME)
    feat, labels, ids = load_feat(feat_path)
    outputdir = get_output_dir(data_dir)
    net_filename = os.path.join(outputdir, cfg.PRETRAINED_FAE_FILENAME)
    checkpoint = torch.load(net_filename)
    net = extract_sdae_model(input_dim=cfg.INPUT_DIM,
                             hidden_dims=cfg.HIDDEN_DIMS)
    net.load_state_dict(checkpoint['state_dict'])
    if use_cuda:
        net.cuda()

    dcn = DCN(n_clusters,
              net,
              cfg.HIDDEN_DIMS[-1],
              lr=lr,
              tol=tol,
              batch_size=batch_size,
              recons_lam=recons_lam,
              cluster_lam=cluster_lam,
              use_cuda=use_cuda,
              verbose=True)
    dcn.fit(feat, labels=labels)