# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# # Using variable learning rates
# This notebook is an experiment to measure the effect of alpha (learning rate) on the gensim training.
#
# The initial question was: *does using a fixed / variable alpha has an effect on the observed convergence of w2v models?*
#
# This notebook runs two sets of models with the same parameters, except one set has a fixed learning rate while the other has a variable learning rate.
#
# Based on several runs of this notebook, there doesn't seem to be any difference in the convergence curves. This implies that the learning rate has little effect on the final model (there must be a difference, but it is not noticable on the convergence).

# %load_ext autoreload
# %autoreload 2

# %pylab inline

minSentences = 1e4
maxSentences = 1.5e5
vector_size = 300
nSteps = 25
chunkSize = 1000
randomSeed = 1
y0 = 1785

years = np.arange(1785, 1795)

from convergence import *

# +
modelFolder = './models'

generateModelsForPeriod(years, minSentences, maxSentences, nSteps, chunkSize, randomSeed, vector_size, modelFolder)
convergence, sentenceYearCounter, vocabSize = computeConvergenceOverYearRangeWithBuildModels(modelFolder, vector_size)

fname = modelFolder + '/convergenceRange_%d.pkl'%y0
pkl.dump((convergence, sentenceYearCounter, vocabSize), open(fname, 'wb'))

# +
modelFolder = './models-b'

generateModelsForPeriod(years, minSentences, maxSentences, nSteps, chunkSize, randomSeed, vector_size, modelFolder, fixedAlpha=True)
convergence, sentenceYearCounter, vocabSize = computeConvergenceOverYearRangeWithBuildModels(modelFolder, vector_size)

fname = modelFolder + '/convergenceRange_%d.pkl'%y0
pkl.dump((convergence, sentenceYearCounter, vocabSize), open(fname, 'wb'))


# -

def doPlots(convergence, sentenceYearCounter):
    semilogx(convergence.keys(), convergence.values(), 'x-')
    xlabel('# Sentences')
    ylabel('Convergence')
    for year,sentenceCount in sentenceYearCounter.iteritems():
        plot([sentenceCount,sentenceCount], [0,1],'--', color='#aaaaaa')
        text(sentenceCount, 0.5, str(year), rotation=270)
    ax = axis()
    # axis([ ax[0], ax[1], 0.0, 1.0 ])



# +
files = [ 'models/convergenceRange_1785.pkl', 'models-b/convergenceRange_1785.pkl' ]

for f in files:
    convergence, sentenceYearCounter, vocabSize = pkl.load(open(f, 'rb'))
    doPlots(convergence, sentenceYearCounter)

StarcoderdataJupyterScriptsDedupFiltered