Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(z, lr, anneal_start, anneal_end, p, alpha, lstm_size, num_epochs, max_len, batch_size, session, dataset, sp_model, resume):
train_db = TextProjectReconstructionDatabase(dataset=dataset, phase="train", batch_size=batch_size, max_len=max_len, sp_model=sp_model or None)
valid_db = TextProjectReconstructionDatabase(dataset=dataset, phase="valid", batch_size=batch_size, max_len=max_len, sp_model=sp_model or None)
model = make_model(z, max_len, p, train_db.n_classes, lstm_size, alpha)
model.anneal_start = float(anneal_start)
model.anneal_end = float(anneal_end)
vocab = train_db.vocab
print("Using vocab with %s tokens" % len(vocab))
if resume:
model.load("session/%s/model.flt" % session)
print("Resuming session %s" % session)
#out = nn.utils.forward(model, train_db, out=model.output(model.input))
#print out.shape
#return
def main(z, lr, anneal_start, anneal_end, p, alpha, lstm_size, num_epochs, max_len, batch_size, session, dataset, sp_model, resume):
train_db = TextProjectReconstructionDatabase(dataset=dataset, phase="train", batch_size=batch_size, max_len=max_len, sp_model=sp_model or None)
valid_db = TextProjectReconstructionDatabase(dataset=dataset, phase="valid", batch_size=batch_size, max_len=max_len, sp_model=sp_model or None)
model = make_model(z, max_len, p, train_db.n_classes, lstm_size, alpha)
model.anneal_start = float(anneal_start)
model.anneal_end = float(anneal_end)
vocab = train_db.vocab
print("Using vocab with %s tokens" % len(vocab))
if resume:
model.load("session/%s/model.flt" % session)
print("Resuming session %s" % session)
#out = nn.utils.forward(model, train_db, out=model.output(model.input))
#print out.shape
ins[:, 0] = valid_db.to_inputs(valid_db.sentences[s1])
ins[:, 1] = valid_db.to_inputs(valid_db.sentences[s2])
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
f = theano.function([x], mu)
z = f(ins.astype('int32'))
s1_z = z[0]
s2_z = z[1]
n = 7
s1_z = numpy.repeat(s1_z[None, :], n, axis=0)
s2_z = numpy.repeat(s2_z[None, :], n, axis=0)
steps = numpy.linspace(0, 1, n)[:, None]
sampled = s1_z * (1 - steps) + s2_z * steps
elif mode == 'arithm':
valid_db = TextProjectReconstructionDatabase("valid", 50, batches_per_epoch=100, max_len=max_len)
s1 = numpy.random.randint(0, len(valid_db.sentences))
s2 = numpy.random.randint(0, len(valid_db.sentences))
s3 = numpy.random.randint(0, len(valid_db.sentences))
print valid_db.sentences[s1]
print valid_db.sentences[s2]
print valid_db.sentences[s3]
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
assert isinstance(sampler, Sampler)
ins = numpy.zeros((max_len, 3))
ins[:, 0] = valid_db.to_inputs(valid_db.sentences[s1])
ins[:, 1] = valid_db.to_inputs(valid_db.sentences[s2])
ins[:, 2] = valid_db.to_inputs(valid_db.sentences[s3])
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
mode = 'custom'
#mode = 'interpolate'
if mode == 'vary':
n = 7
sampled = numpy.random.normal(0, 1, (1, z))
sampled = numpy.repeat(sampled, n * z, axis=0)
for dim in xrange(z):
eps = 0.01
x = numpy.linspace(eps, 1 - eps, num=n)
x = norm.ppf(x)
sampled[dim*n:(dim+1)*n, dim] = x
n *= z
elif mode == 'interpolatereal':
valid_db = TextProjectReconstructionDatabase(dataset=dataset, phase="valid", batch_size=50, batches_per_epoch=100, max_len=max_len)
s1 = numpy.random.randint(0, len(valid_db.sentences))
s2 = numpy.random.randint(0, len(valid_db.sentences))
encoder = model.layers[0].branches[0]
sampler = encoder[-1]
assert isinstance(sampler, Sampler)
ins = numpy.zeros((max_len, 2))
ins[:, 0] = valid_db.to_inputs(valid_db.sentences[s1])
ins[:, 1] = valid_db.to_inputs(valid_db.sentences[s2])
x = T.imatrix()
z = encoder(x)
mu = sampler.mu
f = theano.function([x], mu)
z = f(ins.astype('int32'))
s1_z = z[0]
s2_z = z[1]
n = 7