def log(step,
         wtr=tf.summary.FileWriter("../log/{}".format(trial)),
         log=tf.summary.merge((tf.summary.scalar('step_xid', dummy[0]),
                               tf.summary.scalar('step_err', dummy[1]))),
         fet=(valid.xid, valid.err),
         inp=(valid.src_idx, valid.len_src, valid.tgt_idx, valid.len_tgt),
         src=src_valid,
         tgt=tgt_valid,
         bat=256):
     stats = [
         sess.run(fet, dict(zip(inp, feed(src[i:j], tgt[i:j]))))
         for i, j in partition(len(tgt), bat)
     ]
     stats = [np.mean(np.concatenate(stat)) for stat in zip(*stats)]
     wtr.add_summary(sess.run(log, dict(zip(dummy, stats))), step)
     wtr.flush()
Beispiel #2
0
def infer_avg(sent, samples=128):
   bat = [sp.encode_capped_sample(vocab, sent) for _ in range(samples)]
   bat = vpack(bat, (len(bat), max(map(len, bat))), vocab.eos_id(), np.int32)
   z = model.z.eval({model.src: bat})
   return np.mean(z, axis=0)
Beispiel #3
0
from util_io import load_txt, save_txt
from util_np import np, partition, vpack
from util_np import vpack
import pandas as pd
import util_sp as sp

# load data
df = pd.read_csv(path_csv)
emb = np.load(path_emb)
emb_sp = np.load(path_emb_sp)

# load sentencepiece model
vocab = sp.load_spm(path_vocab)

# Load the model
model = vAe('infer')
# Restore the session
sess = tf.InteractiveSession()
tf.train.Saver().restore(sess, path_ckpt)

###########################
# generate from centroids #
###########################

for col in "euc euc_sp cos cos_sp".split():
    cluster = df["cls_{}".format(col)].values
    centroids = np.stack(
        [np.mean(emb[cluster == c], axis=0) for c in range(cluster.max() + 1)])
    y = decode(sess, model, centroids, steps=512)
    save_txt("../trial/centroids_{}".format(col), sp.decode(vocab, y))
Beispiel #4
0
 def match(self, image):
     if not hasattr(self, 'glyphs'): self.glyphs = self._glyphs()
     return np.argmin(np.mean(np.abs(self.glyphs -
                                     np.expand_dims(image, -2)),
                              axis=-1),
                      axis=-1)