def trainModel(fileVocabulary, wordVocabulary, contextProvider, model, superBatchSize, miniBatchSize, parametersPath, embeddingsPath, learningRate, l1Coefficient, l2Coefficient, epochs, metricsPath): if os.path.exists(metricsPath): os.remove(metricsPath) superBatchesCount = contextProvider.contextsCount / superBatchSize + 1 startTime = time.time() previousTotal = 0 for epoch in xrange(0, epochs): for superBatchIndex in xrange(0, superBatchesCount): contextSuperBatch = contextProvider[superBatchIndex * superBatchSize: (superBatchIndex + 1) * superBatchSize] fileIndices, wordIndices, targetWordIndices = contextSuperBatch[:, 1], contextSuperBatch[:, 1: -1], contextSuperBatch[:, -1] model.train(wordIndices, targetWordIndices, miniBatchSize, learningRate, l1Coefficient, l2Coefficient) metrics = validation.validate(wordVocabulary, model) customMetrics = { 'simGemJewel': similarity('gem', 'jewel', wordVocabulary, model) } validation.dump(metricsPath, epoch, superBatchIndex, *metrics, **customMetrics) validation.dump(metricsPath, epoch, superBatchIndex, *metrics) if previousTotal < sum(metrics): model.dump(parametersPath, embeddingsPath) currentTime = time.time() elapsed = currentTime - startTime secondsPerEpoch = elapsed / (epoch + 1) rg, sim353, simLex999, syntRel, sat = metrics log.progress( 'Training model: {0:.3f}%. Elapsed: {1}. Epoch: {2}. ({3:.3f} sec/epoch), RG: {4}. Sim353: {5}. SimLex999: {6}. SyntRel: {7}. SAT: {8}. Gem/Jewel: {9:.3f}.', epoch + 1, epochs, log.delta(elapsed), epoch, secondsPerEpoch, rg, sim353, simLex999, syntRel, sat, customMetrics['simGemJewel']) log.lineBreak() return model
def train(model, fileIndexMap, wordIndexMap, wordEmbeddings, contexts, epochs, batchSize, learningRate, metricsPath=None, pathTo=None): model.trainingContexts.set_value(contexts) contextsCount, contextSize = contexts.shape initialiLearningRate = learningRate startTime = time.time() metrics = { 'meanError': np.nan, 'medianError': np.nan, 'maxError': np.nan, 'minError': np.nan, 'learningRate': learningRate } maxError = None for epoch in xrange(0, epochs): errors = [] for contextIndex in xrange(0, contextsCount): error = model.trainModel(contextIndex, learningRate) errors.append(error) log.progress('Training model: {0:.3f}%. Epoch: {1}. Elapsed: {2}. Error(mean,median,min,max): {3:.3f}, {4:.3f}, {5:.3f}, {6:.3f}. Learning rate: {7}.', epoch * contextsCount + contextIndex + 1, epochs * contextsCount, epoch + 1, log.delta(time.time() - startTime), metrics['meanError'], metrics['medianError'], metrics['minError'], metrics['maxError'], learningRate) learningRate = learningRate * (1 - (float(epoch) + 1) / epochs) learningRate = max(initialiLearningRate * 0.0001, learningRate) metrics = { 'meanError': np.mean(errors), 'medianError': np.median(errors), 'maxError': np.max(errors), 'minError': np.min(errors), 'learningRate': learningRate } if pathTo is not None and (maxError is None or maxError > metrics['maxError']): model.dump(pathTo.fileEmbeddings, pathTo.weights) maxError = metrics['maxError'] if metricsPath is not None: validation.dump(metricsPath, epoch, metrics)
def trainModel(fileVocabulary, wordVocabulary, contextProvider, model, superBatchSize, miniBatchSize, parametersPath, embeddingsPath, learningRate, l1Coefficient, l2Coefficient, epochs, metricsPath): if os.path.exists(metricsPath): os.remove(metricsPath) superBatchesCount = contextProvider.contextsCount / superBatchSize + 1 startTime = time.time() previousTotal = 0 for epoch in xrange(0, epochs): for superBatchIndex in xrange(0, superBatchesCount): contextSuperBatch = contextProvider[superBatchIndex * superBatchSize:(superBatchIndex + 1) * superBatchSize] fileIndices, wordIndices, targetWordIndices = contextSuperBatch[:,1], contextSuperBatch[:,1:-1], contextSuperBatch[:,-1] model.train(wordIndices, targetWordIndices, miniBatchSize, learningRate, l1Coefficient, l2Coefficient) metrics = validation.validate(wordVocabulary, model) customMetrics = { 'simGemJewel': similarity('gem', 'jewel', wordVocabulary, model) } validation.dump(metricsPath, epoch, superBatchIndex, *metrics, **customMetrics) validation.dump(metricsPath, epoch, superBatchIndex, *metrics) if previousTotal < sum(metrics): model.dump(parametersPath, embeddingsPath) currentTime = time.time() elapsed = currentTime - startTime secondsPerEpoch = elapsed / (epoch + 1) rg, sim353, simLex999, syntRel, sat = metrics log.progress('Training model: {0:.3f}%. Elapsed: {1}. Epoch: {2}. ({3:.3f} sec/epoch), RG: {4}. Sim353: {5}. SimLex999: {6}. SyntRel: {7}. SAT: {8}. Gem/Jewel: {9:.3f}.', epoch + 1, epochs, log.delta(elapsed), epoch, secondsPerEpoch, rg, sim353, simLex999, syntRel, sat, customMetrics['simGemJewel']) log.lineBreak() return model