else:
    print('Full softmax')
print()

embedding = Embedding(sourceEmbedDim, targetEmbedDim, corpus.sourceVoc.size(),
                      corpus.targetVoc.size())
encdec = EncDec(sourceEmbedDim,
                targetEmbedDim,
                hiddenDim,
                corpus.targetVoc.size(),
                useSmallSoftmax=useSmallSoftmax,
                dropoutRate=dropoutRate,
                numLayers=numLayers)

if useSmallSoftmax:
    vocGen = VocGenerator(vocGenHiddenDim, corpus.targetVoc.size(),
                          corpus.sourceVoc.size())
    vocGen.load_state_dict(torch.load(vocGenFile))
    vocGen.cuda()
    vocGen.eval()

encdec.softmaxLayer.weight.weight = embedding.targetEmbedding.weight

embedding.cuda()
encdec.cuda()

batchListTrain = utils.buildBatchList(len(corpus.trainData), batchSize)
batchListDev = utils.buildBatchList(len(corpus.devData), batchSize)

withoutWeightDecay = []
withWeightDecay = []
for name, param in list(embedding.named_parameters()) + list(
Ejemplo n.º 2
0
torch.cuda.manual_seed(seed)

corpus = Corpus(sourceTrainFile, sourceOrigTrainFile, targetTrainFile,
                sourceDevFile, sourceOrigDevFile, targetDevFile,
                minFreqSource, minFreqTarget, maxLen)

print('Source vocabulary size: '+str(corpus.sourceVoc.size()))
print('Target vocabulary size: '+str(corpus.targetVoc.size()))
print()
print('# of training sentences: '+str(len(corpus.trainData)))
print('# of develop sentnces:  '+str(len(corpus.devData)))
print('Random seed: ', str(seed))
print('K = ', K)
print()

vocGen = VocGenerator(vocGenHiddenDim, corpus.targetVoc.size(), corpus.sourceVoc.size(), dropoutRate)
vocGen.to(device)

batchListTrain = utils.buildBatchList(len(corpus.trainData), batchSize)
batchListDev = utils.buildBatchList(len(corpus.devData), batchSize)

withoutWeightDecay = []
withWeightDecay = []

for name, param in vocGen.named_parameters():
    if 'bias' in name or 'softmax' in name or 'Embedding' in name:
        withoutWeightDecay += [param]
    else:
        withWeightDecay += [param]
optParams = [{'params': withWeightDecay, 'weight_decay': weightDecay},
             {'params': withoutWeightDecay, 'weight_decay': 0.0}]
out_features = 12772
voc_features = 226658

seed = 1
gpuId = 0

torch.set_num_threads(1)
torch.manual_seed(seed)
random.seed(seed)
torch.cuda.set_device(gpuId)
torch.cuda.manual_seed(seed)

device = torch.device('cuda:' + str(gpuId))
cpu = torch.device('cpu')

vocGen = VocGenerator(vocGenHiddenDim, out_features, voc_features)
vocGen.load_state_dict(torch.load(vocGenFile))
vocGen.cuda()
vocGen.eval()

sourceTrainFile = '../../WikiCatSum/film_tok_min5_L7.5k/train.bow.src'
sourceOrigTrainFile = sourceTrainFile
targetTrainFile = '../../WikiCatSum/film_tok_min5_L7.5k/train.bow.tgt'
sourceTestFile = '../../WikiCatSum/film_tok_min5_L7.5k/test.bow.src'
sourceOrigTestFile = sourceTestFile
targetTestFile = '../../WikiCatSum/film_tok_min5_L7.5k/test.bow.tgt'
minFreqSource = 2
minFreqTarget = 2
maxLen = 10000
trainPickle = 'trainData.pt'
devPickle = 'devData.pt'
torch.manual_seed(seed)
random.seed(seed)
torch.cuda.set_device(gpuId)
torch.cuda.manual_seed(seed)

device = torch.device('cuda:'+str(gpuId))
cpu = torch.device('cpu')

corpus = Corpus(sourceTrainFile, sourceOrigTrainFile, targetTrainFile,
                sourceTestFile, sourceOrigTestFile, targetTestFile,
                minFreqSource, minFreqTarget, maxLen, trainPickle, devPickle)

batchSize = len(corpus.devData)

vocGen = VocGenerator(vocGenHiddenDim, 12772, 226658)
vocGen.load_state_dict(torch.load(vocGenFile))
vocGen.cuda()
vocGen.eval()

batchListDev = utils.buildBatchList(len(corpus.devData), batchSize)

for batch in batchListDev:
    batchSize = batch[1]-batch[0]+1
    batchInputSource, lengthsSource, batchInputTarget, batchTarget, lengthsTarget, tokenCount, batchData, maxTargetLen = corpus.processBatchInfoNMT(batch, train = False, volatile = True, device = device)

    targetVocGen, inputVocGen = corpus.processBatchInfoVocGen(batchData, smoothing = False, volatile = True, device = device)
    outputVocGen = vocGen(inputVocGen)

<<<<<<< HEAD
    #tmp = F.sigmoid(outputVocGen.data).data + targetVocGen.data