in encoder.py [0:0]
def __init__(self, nbatch=128, nsteps=64):
global hps
hps = HParams(
load_path='model_params/params.jl',
nhidden=4096,
nembd=64,
nsteps=nsteps,
nbatch=nbatch,
nstates=2,
nvocab=256,
out_wn=False,
rnn_wn=True,
rnn_type='mlstm',
embd_wn=True,
)
global params
params = [np.load('model/%d.npy'%i) for i in range(15)]
params[2] = np.concatenate(params[2:6], axis=1)
params[3:6] = []
X = tf.placeholder(tf.int32, [None, hps.nsteps])
M = tf.placeholder(tf.float32, [None, hps.nsteps, 1])
S = tf.placeholder(tf.float32, [hps.nstates, None, hps.nhidden])
cells, states, logits = model(X, S, M, reuse=False)
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)
def seq_rep(xmb, mmb, smb):
return sess.run(states, {X: xmb, M: mmb, S: smb})
def seq_cells(xmb, mmb, smb):
return sess.run(cells, {X: xmb, M: mmb, S: smb})
def transform(xs):
tstart = time.time()
xs = [preprocess(x) for x in xs]
lens = np.asarray([len(x) for x in xs])
sorted_idxs = np.argsort(lens)
unsort_idxs = np.argsort(sorted_idxs)
sorted_xs = [xs[i] for i in sorted_idxs]
maxlen = np.max(lens)
offset = 0
n = len(xs)
smb = np.zeros((2, n, hps.nhidden), dtype=np.float32)
for step in range(0, ceil_round_step(maxlen, nsteps), nsteps):
start = step
end = step+nsteps
xsubseq = [x[start:end] for x in sorted_xs]
ndone = sum([x == b'' for x in xsubseq])
offset += ndone
xsubseq = xsubseq[ndone:]
sorted_xs = sorted_xs[ndone:]
nsubseq = len(xsubseq)
xmb, mmb = batch_pad(xsubseq, nsubseq, nsteps)
for batch in range(0, nsubseq, nbatch):
start = batch
end = batch+nbatch
batch_smb = seq_rep(
xmb[start:end], mmb[start:end],
smb[:, offset+start:offset+end, :])
smb[:, offset+start:offset+end, :] = batch_smb
features = smb[0, unsort_idxs, :]
print('%0.3f seconds to transform %d examples' %
(time.time() - tstart, n))
return features
def cell_transform(xs, indexes=None):
Fs = []
xs = [preprocess(x) for x in xs]
for xmb in tqdm(
iter_data(xs, size=hps.nbatch), ncols=80, leave=False,
total=len(xs)//hps.nbatch):
smb = np.zeros((2, hps.nbatch, hps.nhidden))
n = len(xmb)
xmb, mmb = batch_pad(xmb, hps.nbatch, hps.nsteps)
smb = sess.run(cells, {X: xmb, S: smb, M: mmb})
smb = smb[:, :n, :]
if indexes is not None:
smb = smb[:, :, indexes]
Fs.append(smb)
Fs = np.concatenate(Fs, axis=1).transpose(1, 0, 2)
return Fs
self.transform = transform
self.cell_transform = cell_transform