def init_weights(self): init.orthogonal_(self.rnn.weight_ih_l0) init.uniform_(self.rnn.weight_hh_l0, a=-0.01, b=0.01) embeddings = preprocessing.load_embeddings(self.pretrained_vectors) embedding_weights = torch.FloatTensor(self.vocab_size, self.input_size) init.uniform_(embedding_weights, a=-0.25, b=0.25) for k, v in embeddings.items(): embedding_weights[k] = torch.FloatTensor(v) embedding_weights[0] = torch.FloatTensor([0] * self.input_size) del self.embedding.weight self.embedding.weight = nn.Parameter(embedding_weights)
print('Loading networks', end='...') decoder = DecoderWithAttention(ATTENTION_DIM, EMBBEDING_DIM, DECODER_DIM, vocab_size, ENCODER_DIM, DROPOUT) encoder = Encoder(output_size=12) print('done') if START_EPOCH != 0: print('Loading last model', end='...') decoder.load_state_dict( torch.load('../models/image_captioning_{}.model'.format(START_EPOCH))) print('done') # Embedding if EMBBEDING_DIM == 200: print('Loading embeddings', end='...') embedding, _ = load_embeddings(embedding_file, DATA_FOLDER) decoder.load_pretrained_embeddings(embedding, fine_tune=True) print('done') # Loss function criterion = nn.CrossEntropyLoss().to(DEVICE) # Data loader train_loader = torch.utils.data.DataLoader(CaptionDataset( DATA_FOLDER, 'TRAIN'), batch_size=BATCH_SIZE, shuffle=True, num_workers=1, pin_memory=True) valid_loader = torch.utils.data.DataLoader(CaptionDataset(DATA_FOLDER, 'VAL'),
# Batch parameters batch_size = 64 context_length = 2 user_filter = "Exiatus" # Model parameters decoder_learning_ratio = 5.0 epochs = 5000 grad_clip = 50 hidden_size = 200 learning_rate = 0.0001 # Load vocab/embeddings w2i, i2w = preprocessing.load_vocab(vocab_file) embeddings = preprocessing.load_embeddings(embedding_file, w2i) vocab_size = len(w2i) input_size = len(embeddings[1]) print("Loaded %d words" % vocab_size) # Load/numberize messages messages = preprocessing.load_messages(messages_file) numberized_messages = preprocessing.numberize_messages(messages, w2i) print("Loaded %d messages" % len(messages)) # Create encoder encoder = models.Encoder( input_size=input_size, hidden_size=hidden_size,