Unverified Commit 6b9d5fba authored by Toby Boyd's avatar Toby Boyd Committed by GitHub
Browse files

Merge branch 'master' into patch-1

parents 5fd687c5 5fa2a4e6
...@@ -19,6 +19,7 @@ from __future__ import division ...@@ -19,6 +19,7 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
import tensorflow as tf import tensorflow as tf
from six.moves import xrange
import networks import networks
......
...@@ -24,6 +24,7 @@ from __future__ import print_function ...@@ -24,6 +24,7 @@ from __future__ import print_function
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
ds = tf.contrib.distributions ds = tf.contrib.distributions
......
...@@ -22,6 +22,7 @@ import os ...@@ -22,6 +22,7 @@ import os
import numpy as np import numpy as np
import scipy.misc import scipy.misc
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
from mnist import data_provider from mnist import data_provider
......
...@@ -119,8 +119,8 @@ First ensure that you have installed the following required packages: ...@@ -119,8 +119,8 @@ First ensure that you have installed the following required packages:
* **NumPy** ([instructions](http://www.scipy.org/install.html)) * **NumPy** ([instructions](http://www.scipy.org/install.html))
* **Natural Language Toolkit (NLTK)**: * **Natural Language Toolkit (NLTK)**:
* First install NLTK ([instructions](http://www.nltk.org/install.html)) * First install NLTK ([instructions](http://www.nltk.org/install.html))
* Then install the NLTK data ([instructions](http://www.nltk.org/data.html)) * Then install the NLTK data package "punkt" ([instructions](http://www.nltk.org/data.html))
* **Unzip**
### Prepare the Training Data ### Prepare the Training Data
To train the model you will need to provide training data in native TFRecord To train the model you will need to provide training data in native TFRecord
...@@ -145,7 +145,7 @@ available space for storing the downloaded and processed data. ...@@ -145,7 +145,7 @@ available space for storing the downloaded and processed data.
MSCOCO_DIR="${HOME}/im2txt/data/mscoco" MSCOCO_DIR="${HOME}/im2txt/data/mscoco"
# Build the preprocessing script. # Build the preprocessing script.
cd tensorflow-models/im2txt cd research/im2txt
bazel build //im2txt:download_and_preprocess_mscoco bazel build //im2txt:download_and_preprocess_mscoco
# Run the preprocessing script. # Run the preprocessing script.
...@@ -212,7 +212,7 @@ INCEPTION_CHECKPOINT="${HOME}/im2txt/data/inception_v3.ckpt" ...@@ -212,7 +212,7 @@ INCEPTION_CHECKPOINT="${HOME}/im2txt/data/inception_v3.ckpt"
MODEL_DIR="${HOME}/im2txt/model" MODEL_DIR="${HOME}/im2txt/model"
# Build the model. # Build the model.
cd tensorflow-models/im2txt cd research/im2txt
bazel build -c opt //im2txt/... bazel build -c opt //im2txt/...
# Run the training script. # Run the training script.
...@@ -306,7 +306,7 @@ VOCAB_FILE="${HOME}/im2txt/data/mscoco/word_counts.txt" ...@@ -306,7 +306,7 @@ VOCAB_FILE="${HOME}/im2txt/data/mscoco/word_counts.txt"
IMAGE_FILE="${HOME}/im2txt/data/mscoco/raw-data/val2014/COCO_val2014_000000224477.jpg" IMAGE_FILE="${HOME}/im2txt/data/mscoco/raw-data/val2014/COCO_val2014_000000224477.jpg"
# Build the inference binary. # Build the inference binary.
cd tensorflow-models/im2txt cd research/im2txt
bazel build -c opt //im2txt:run_inference bazel build -c opt //im2txt:run_inference
# Ignore GPU devices (only necessary if your GPU is currently memory # Ignore GPU devices (only necessary if your GPU is currently memory
......
...@@ -97,6 +97,7 @@ import threading ...@@ -97,6 +97,7 @@ import threading
import nltk.tokenize import nltk.tokenize
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
tf.flags.DEFINE_string("train_image_dir", "/tmp/train2014/", tf.flags.DEFINE_string("train_image_dir", "/tmp/train2014/",
......
...@@ -76,7 +76,7 @@ def evaluate_model(sess, model, global_step, summary_writer, summary_op): ...@@ -76,7 +76,7 @@ def evaluate_model(sess, model, global_step, summary_writer, summary_op):
start_time = time.time() start_time = time.time()
sum_losses = 0. sum_losses = 0.
sum_weights = 0. sum_weights = 0.
for i in xrange(num_eval_batches): for i in range(num_eval_batches):
cross_entropy_losses, weights = sess.run([ cross_entropy_losses, weights = sess.run([
model.target_cross_entropy_losses, model.target_cross_entropy_losses,
model.target_cross_entropy_loss_weights model.target_cross_entropy_loss_weights
...@@ -143,7 +143,7 @@ def run_once(model, saver, summary_writer, summary_op): ...@@ -143,7 +143,7 @@ def run_once(model, saver, summary_writer, summary_op):
global_step=global_step, global_step=global_step,
summary_writer=summary_writer, summary_writer=summary_writer,
summary_op=summary_op) summary_op=summary_op)
except Exception, e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
tf.logging.error("Evaluation failed.") tf.logging.error("Evaluation failed.")
coord.request_stop(e) coord.request_stop(e)
......
...@@ -70,7 +70,7 @@ def main(_): ...@@ -70,7 +70,7 @@ def main(_):
generator = caption_generator.CaptionGenerator(model, vocab) generator = caption_generator.CaptionGenerator(model, vocab)
for filename in filenames: for filename in filenames:
with tf.gfile.GFile(filename, "r") as f: with tf.gfile.GFile(filename, "rb") as f:
image = f.read() image = f.read()
captions = generator.beam_search(sess, image) captions = generator.beam_search(sess, image)
print("Captions for image %s:" % os.path.basename(filename)) print("Captions for image %s:" % os.path.basename(filename))
......
...@@ -21,6 +21,7 @@ import sys ...@@ -21,6 +21,7 @@ import sys
import time import time
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
from learned_optimizer.optimizer import trainable_optimizer from learned_optimizer.optimizer import trainable_optimizer
......
...@@ -29,6 +29,7 @@ import numpy as np ...@@ -29,6 +29,7 @@ import numpy as np
from scipy.misc import imresize from scipy.misc import imresize
from scipy.misc import imrotate from scipy.misc import imrotate
from scipy.ndimage import imread from scipy.ndimage import imread
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
......
...@@ -23,6 +23,7 @@ published as a conference paper at ICLR 2017. ...@@ -23,6 +23,7 @@ published as a conference paper at ICLR 2017.
""" """
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
......
...@@ -26,6 +26,7 @@ import os ...@@ -26,6 +26,7 @@ import os
import random import random
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
import data_utils import data_utils
......
...@@ -18,6 +18,7 @@ from __future__ import print_function ...@@ -18,6 +18,7 @@ from __future__ import print_function
import h5py import h5py
import numpy as np import numpy as np
import os import os
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
from utils import write_datasets from utils import write_datasets
......
...@@ -18,6 +18,7 @@ from __future__ import print_function ...@@ -18,6 +18,7 @@ from __future__ import print_function
import os import os
import h5py import h5py
import numpy as np import numpy as np
from six.moves import xrange
from synthetic_data_utils import generate_data, generate_rnn from synthetic_data_utils import generate_data, generate_rnn
from synthetic_data_utils import get_train_n_valid_inds from synthetic_data_utils import get_train_n_valid_inds
......
...@@ -21,6 +21,7 @@ import sys ...@@ -21,6 +21,7 @@ import sys
import time import time
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
import program_utils import program_utils
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
import time import time
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
from tensorflow.python.framework import function from tensorflow.python.framework import function
...@@ -500,8 +501,10 @@ class NeuralGPU(object): ...@@ -500,8 +501,10 @@ class NeuralGPU(object):
return tf.reduce_sum(encoder_outputs * tf.expand_dims(mask, 2), 1) return tf.reduce_sum(encoder_outputs * tf.expand_dims(mask, 2), 1)
with tf.variable_scope("decoder"): with tf.variable_scope("decoder"):
def decoder_loop_fn((state, prev_cell_out, _), (cell_inp, cur_tgt)): def decoder_loop_fn(state__prev_cell_out__unused, cell_inp__cur_tgt):
"""Decoder loop function.""" """Decoder loop function."""
state, prev_cell_out, _ = state__prev_cell_out__unused
cell_inp, cur_tgt = cell_inp__cur_tgt
attn_q = tf.layers.dense(prev_cell_out, height * nmaps, attn_q = tf.layers.dense(prev_cell_out, height * nmaps,
name="attn_query") name="attn_query")
attn_res = attention_query(attn_q, tf.get_variable( attn_res = attention_query(attn_q, tf.get_variable(
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
# ============================================================================== # ==============================================================================
"""Neural GPU.""" """Neural GPU."""
from __future__ import print_function
import math import math
import os import os
import random import random
...@@ -22,6 +24,7 @@ import threading ...@@ -22,6 +24,7 @@ import threading
import time import time
import numpy as np import numpy as np
from six.moves import xrange
import tensorflow as tf import tensorflow as tf
import program_utils import program_utils
...@@ -144,7 +147,7 @@ def read_data(source_path, target_path, buckets, max_size=None, print_out=True): ...@@ -144,7 +147,7 @@ def read_data(source_path, target_path, buckets, max_size=None, print_out=True):
while source and target and (not max_size or counter < max_size): while source and target and (not max_size or counter < max_size):
counter += 1 counter += 1
if counter % 100000 == 0 and print_out: if counter % 100000 == 0 and print_out:
print " reading data line %d" % counter print(" reading data line %d" % counter)
sys.stdout.flush() sys.stdout.flush()
source_ids = [int(x) for x in source.split()] source_ids = [int(x) for x in source.split()]
target_ids = [int(x) for x in target.split()] target_ids = [int(x) for x in target.split()]
...@@ -188,7 +191,7 @@ def read_data_into_global(source_path, target_path, buckets, ...@@ -188,7 +191,7 @@ def read_data_into_global(source_path, target_path, buckets,
global_train_set["wmt"].append(data_set) global_train_set["wmt"].append(data_set)
train_total_size = calculate_buckets_scale(data_set, buckets, "wmt") train_total_size = calculate_buckets_scale(data_set, buckets, "wmt")
if print_out: if print_out:
print " Finished global data reading (%d)." % train_total_size print(" Finished global data reading (%d)." % train_total_size)
def initialize(sess=None): def initialize(sess=None):
...@@ -552,7 +555,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False, ...@@ -552,7 +555,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False,
for h in history] for h in history]
tgt_set = set(target) tgt_set = set(target)
if print_out: if print_out:
print "target: ", tgt_prog print("target: ", tgt_prog)
inps, tgt_outs = [], [] inps, tgt_outs = [], []
for i in xrange(3): for i in xrange(3):
ilist = [inp[i + 1, l] for l in xrange(inp.shape[1])] ilist = [inp[i + 1, l] for l in xrange(inp.shape[1])]
...@@ -566,11 +569,11 @@ def score_beams_prog(beams, target, inp, history, print_out=False, ...@@ -566,11 +569,11 @@ def score_beams_prog(beams, target, inp, history, print_out=False,
if len(olist) == 1: if len(olist) == 1:
tgt_outs.append(olist[0]) tgt_outs.append(olist[0])
else: else:
print [program_utils.prog_vocab[x] for x in ilist if x > 0] print([program_utils.prog_vocab[x] for x in ilist if x > 0])
print olist print(olist)
print tgt_prog print(tgt_prog)
print program_utils.evaluate(tgt_prog, {"a": inps[-1]}) print(program_utils.evaluate(tgt_prog, {"a": inps[-1]}))
print "AAAAA" print("AAAAA")
tgt_outs.append(olist[0]) tgt_outs.append(olist[0])
if not test_mode: if not test_mode:
for _ in xrange(7): for _ in xrange(7):
...@@ -602,7 +605,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False, ...@@ -602,7 +605,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False,
best_prog = b_prog best_prog = b_prog
best_score = score best_score = score
if print_out: if print_out:
print "best score: ", best_score, " best prog: ", best_prog print("best score: ", best_score, " best prog: ", best_prog)
return best, best_score return best, best_score
...@@ -719,7 +722,7 @@ def train(): ...@@ -719,7 +722,7 @@ def train():
inp = new_inp inp = new_inp
# If all results are great, stop (todo: not to wait for all?). # If all results are great, stop (todo: not to wait for all?).
if FLAGS.nprint > 1: if FLAGS.nprint > 1:
print scores print(scores)
if sum(scores) / float(len(scores)) >= 10.0: if sum(scores) / float(len(scores)) >= 10.0:
break break
# The final step with the true target. # The final step with the true target.
...@@ -735,7 +738,7 @@ def train(): ...@@ -735,7 +738,7 @@ def train():
errors, total, seq_err = data.accuracy( errors, total, seq_err = data.accuracy(
inp, res, target, batch_size, 0, new_target, scores) inp, res, target, batch_size, 0, new_target, scores)
if FLAGS.nprint > 1: if FLAGS.nprint > 1:
print "seq_err: ", seq_err print("seq_err: ", seq_err)
acc_total += total acc_total += total
acc_errors += errors acc_errors += errors
acc_seq_err += seq_err acc_seq_err += seq_err
...@@ -944,8 +947,8 @@ def interactive(): ...@@ -944,8 +947,8 @@ def interactive():
for v in tf.trainable_variables(): for v in tf.trainable_variables():
shape = v.get_shape().as_list() shape = v.get_shape().as_list()
total += mul(shape) total += mul(shape)
print (v.name, shape, mul(shape)) print(v.name, shape, mul(shape))
print total print(total)
# Start interactive loop. # Start interactive loop.
sys.stdout.write("Input to Neural GPU Translation Model.\n") sys.stdout.write("Input to Neural GPU Translation Model.\n")
sys.stdout.write("> ") sys.stdout.write("> ")
...@@ -960,7 +963,7 @@ def interactive(): ...@@ -960,7 +963,7 @@ def interactive():
normalize_digits=FLAGS.normalize_digits) normalize_digits=FLAGS.normalize_digits)
else: else:
token_ids = wmt.sentence_to_token_ids(inpt, en_vocab) token_ids = wmt.sentence_to_token_ids(inpt, en_vocab)
print [rev_en_vocab[t] for t in token_ids] print([rev_en_vocab[t] for t in token_ids])
# Which bucket does it belong to? # Which bucket does it belong to?
buckets = [b for b in xrange(len(data.bins)) buckets = [b for b in xrange(len(data.bins))
if data.bins[b] >= max(len(token_ids), len(cures))] if data.bins[b] >= max(len(token_ids), len(cures))]
...@@ -986,12 +989,12 @@ def interactive(): ...@@ -986,12 +989,12 @@ def interactive():
loss = loss[0] - (data.bins[bucket_id] * FLAGS.length_norm) loss = loss[0] - (data.bins[bucket_id] * FLAGS.length_norm)
outputs = [int(np.argmax(logit, axis=1)) outputs = [int(np.argmax(logit, axis=1))
for logit in output_logits] for logit in output_logits]
print [rev_fr_vocab[t] for t in outputs] print([rev_fr_vocab[t] for t in outputs])
print loss, data.bins[bucket_id] print(loss, data.bins[bucket_id])
print linearize(outputs, rev_fr_vocab) print(linearize(outputs, rev_fr_vocab))
cures.append(outputs[gen_idx]) cures.append(outputs[gen_idx])
print cures print(cures)
print linearize(cures, rev_fr_vocab) print(linearize(cures, rev_fr_vocab))
if FLAGS.simple_tokenizer: if FLAGS.simple_tokenizer:
cur_out = outputs cur_out = outputs
if wmt.EOS_ID in cur_out: if wmt.EOS_ID in cur_out:
...@@ -1002,11 +1005,11 @@ def interactive(): ...@@ -1002,11 +1005,11 @@ def interactive():
if loss < result_cost: if loss < result_cost:
result = outputs result = outputs
result_cost = loss result_cost = loss
print ("FINAL", result_cost) print("FINAL", result_cost)
print [rev_fr_vocab[t] for t in result] print([rev_fr_vocab[t] for t in result])
print linearize(result, rev_fr_vocab) print(linearize(result, rev_fr_vocab))
else: else:
print "TOOO_LONG" print("TOOO_LONG")
sys.stdout.write("> ") sys.stdout.write("> ")
sys.stdout.flush() sys.stdout.flush()
inpt = sys.stdin.readline(), "" inpt = sys.stdin.readline(), ""
......
...@@ -177,7 +177,7 @@ def evaluate(program_str, input_names_to_vals, default="ERROR"): ...@@ -177,7 +177,7 @@ def evaluate(program_str, input_names_to_vals, default="ERROR"):
with stdoutIO() as s: with stdoutIO() as s:
# pylint: disable=bare-except # pylint: disable=bare-except
try: try:
exec exec_str + " print(out)" exec(exec_str + " print(out)")
return s.getvalue()[:-1] return s.getvalue()[:-1]
except: except:
return default return default
...@@ -290,7 +290,7 @@ class Program(object): ...@@ -290,7 +290,7 @@ class Program(object):
with stdoutIO() as s: with stdoutIO() as s:
# pylint: disable=exec-used # pylint: disable=exec-used
exec inp_str + self.body + "; print(out)" exec(inp_str + self.body + "; print(out)")
# pylint: enable=exec-used # pylint: enable=exec-used
return s.getvalue()[:-1] return s.getvalue()[:-1]
...@@ -412,11 +412,11 @@ def gen(max_len, how_many): ...@@ -412,11 +412,11 @@ def gen(max_len, how_many):
else: else:
outcomes_to_programs[outcome_str] = t.flat_str() outcomes_to_programs[outcome_str] = t.flat_str()
if counter % 5000 == 0: if counter % 5000 == 0:
print "== proggen: tried: " + str(counter) print("== proggen: tried: " + str(counter))
print "== proggen: kept: " + str(len(outcomes_to_programs)) print("== proggen: kept: " + str(len(outcomes_to_programs)))
if counter % 250000 == 0 and save_prefix is not None: if counter % 250000 == 0 and save_prefix is not None:
print "saving..." print("saving...")
save_counter = 0 save_counter = 0
progfilename = os.path.join(save_prefix, "prog_" + str(counter) + ".txt") progfilename = os.path.join(save_prefix, "prog_" + str(counter) + ".txt")
iofilename = os.path.join(save_prefix, "io_" + str(counter) + ".txt") iofilename = os.path.join(save_prefix, "io_" + str(counter) + ".txt")
...@@ -431,7 +431,7 @@ def gen(max_len, how_many): ...@@ -431,7 +431,7 @@ def gen(max_len, how_many):
for (o, p) in outcomes_to_programs.iteritems(): for (o, p) in outcomes_to_programs.iteritems():
save_counter += 1 save_counter += 1
if save_counter % 500 == 0: if save_counter % 500 == 0:
print "saving %d of %d" % (save_counter, len(outcomes_to_programs)) print("saving %d of %d" % (save_counter, len(outcomes_to_programs)))
fp.write(p+"\n") fp.write(p+"\n")
fi.write(o+"\n") fi.write(o+"\n")
ftp.write(str(tokenize(p, tokens))+"\n") ftp.write(str(tokenize(p, tokens))+"\n")
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
# ============================================================================== # ==============================================================================
"""Utilities for downloading data from WMT, tokenizing, vocabularies.""" """Utilities for downloading data from WMT, tokenizing, vocabularies."""
from __future__ import print_function
import gzip import gzip
import os import os
import re import re
...@@ -53,20 +55,20 @@ _WMT_ENFR_DEV_URL = "http://www.statmt.org/wmt15/dev-v2.tgz" ...@@ -53,20 +55,20 @@ _WMT_ENFR_DEV_URL = "http://www.statmt.org/wmt15/dev-v2.tgz"
def maybe_download(directory, filename, url): def maybe_download(directory, filename, url):
"""Download filename from url unless it's already in directory.""" """Download filename from url unless it's already in directory."""
if not tf.gfile.Exists(directory): if not tf.gfile.Exists(directory):
print "Creating directory %s" % directory print("Creating directory %s" % directory)
os.mkdir(directory) os.mkdir(directory)
filepath = os.path.join(directory, filename) filepath = os.path.join(directory, filename)
if not tf.gfile.Exists(filepath): if not tf.gfile.Exists(filepath):
print "Downloading %s to %s" % (url, filepath) print("Downloading %s to %s" % (url, filepath))
filepath, _ = urllib.request.urlretrieve(url, filepath) filepath, _ = urllib.request.urlretrieve(url, filepath)
statinfo = os.stat(filepath) statinfo = os.stat(filepath)
print "Successfully downloaded", filename, statinfo.st_size, "bytes" print("Successfully downloaded", filename, statinfo.st_size, "bytes")
return filepath return filepath
def gunzip_file(gz_path, new_path): def gunzip_file(gz_path, new_path):
"""Unzips from gz_path into new_path.""" """Unzips from gz_path into new_path."""
print "Unpacking %s to %s" % (gz_path, new_path) print("Unpacking %s to %s" % (gz_path, new_path))
with gzip.open(gz_path, "rb") as gz_file: with gzip.open(gz_path, "rb") as gz_file:
with open(new_path, "wb") as new_file: with open(new_path, "wb") as new_file:
for line in gz_file: for line in gz_file:
...@@ -80,7 +82,7 @@ def get_wmt_enfr_train_set(directory): ...@@ -80,7 +82,7 @@ def get_wmt_enfr_train_set(directory):
tf.gfile.Exists(train_path +".en")): tf.gfile.Exists(train_path +".en")):
corpus_file = maybe_download(directory, "training-giga-fren.tar", corpus_file = maybe_download(directory, "training-giga-fren.tar",
_WMT_ENFR_TRAIN_URL) _WMT_ENFR_TRAIN_URL)
print "Extracting tar file %s" % corpus_file print("Extracting tar file %s" % corpus_file)
with tarfile.open(corpus_file, "r") as corpus_tar: with tarfile.open(corpus_file, "r") as corpus_tar:
corpus_tar.extractall(directory) corpus_tar.extractall(directory)
gunzip_file(train_path + ".fr.gz", train_path + ".fr") gunzip_file(train_path + ".fr.gz", train_path + ".fr")
...@@ -95,7 +97,7 @@ def get_wmt_enfr_dev_set(directory): ...@@ -95,7 +97,7 @@ def get_wmt_enfr_dev_set(directory):
if not (tf.gfile.Exists(dev_path + ".fr") and if not (tf.gfile.Exists(dev_path + ".fr") and
tf.gfile.Exists(dev_path + ".en")): tf.gfile.Exists(dev_path + ".en")):
dev_file = maybe_download(directory, "dev-v2.tgz", _WMT_ENFR_DEV_URL) dev_file = maybe_download(directory, "dev-v2.tgz", _WMT_ENFR_DEV_URL)
print "Extracting tgz file %s" % dev_file print("Extracting tgz file %s" % dev_file)
with tarfile.open(dev_file, "r:gz") as dev_tar: with tarfile.open(dev_file, "r:gz") as dev_tar:
fr_dev_file = dev_tar.getmember("dev/" + dev_name + ".fr") fr_dev_file = dev_tar.getmember("dev/" + dev_name + ".fr")
en_dev_file = dev_tar.getmember("dev/" + dev_name + ".en") en_dev_file = dev_tar.getmember("dev/" + dev_name + ".en")
...@@ -206,7 +208,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size, ...@@ -206,7 +208,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
normalize_digits: Boolean; if true, all digits are replaced by 0s. normalize_digits: Boolean; if true, all digits are replaced by 0s.
""" """
if not tf.gfile.Exists(vocabulary_path): if not tf.gfile.Exists(vocabulary_path):
print "Creating vocabulary %s from data %s" % (vocabulary_path, data_path) print("Creating vocabulary %s from data %s" % (vocabulary_path, data_path))
vocab, chars = {}, {} vocab, chars = {}, {}
for c in _PUNCTUATION: for c in _PUNCTUATION:
chars[c] = 1 chars[c] = 1
...@@ -218,7 +220,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size, ...@@ -218,7 +220,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
line = " ".join(line_in.split()) line = " ".join(line_in.split())
counter += 1 counter += 1
if counter % 100000 == 0: if counter % 100000 == 0:
print " processing fr line %d" % counter print(" processing fr line %d" % counter)
for c in line: for c in line:
if c in chars: if c in chars:
chars[c] += 1 chars[c] += 1
...@@ -240,7 +242,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size, ...@@ -240,7 +242,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
line = " ".join(line_in.split()) line = " ".join(line_in.split())
counter += 1 counter += 1
if counter % 100000 == 0: if counter % 100000 == 0:
print " processing en line %d" % counter print(" processing en line %d" % counter)
for c in line: for c in line:
if c in chars: if c in chars:
chars[c] += 1 chars[c] += 1
...@@ -371,7 +373,7 @@ def data_to_token_ids(data_path, target_path, vocabulary_path, ...@@ -371,7 +373,7 @@ def data_to_token_ids(data_path, target_path, vocabulary_path,
normalize_digits: Boolean; if true, all digits are replaced by 0s. normalize_digits: Boolean; if true, all digits are replaced by 0s.
""" """
if not tf.gfile.Exists(target_path): if not tf.gfile.Exists(target_path):
print "Tokenizing data in %s" % data_path print("Tokenizing data in %s" % data_path)
vocab, _ = initialize_vocabulary(vocabulary_path) vocab, _ = initialize_vocabulary(vocabulary_path)
with tf.gfile.GFile(data_path, mode="rb") as data_file: with tf.gfile.GFile(data_path, mode="rb") as data_file:
with tf.gfile.GFile(target_path, mode="w") as tokens_file: with tf.gfile.GFile(target_path, mode="w") as tokens_file:
...@@ -379,7 +381,7 @@ def data_to_token_ids(data_path, target_path, vocabulary_path, ...@@ -379,7 +381,7 @@ def data_to_token_ids(data_path, target_path, vocabulary_path,
for line in data_file: for line in data_file:
counter += 1 counter += 1
if counter % 100000 == 0: if counter % 100000 == 0:
print " tokenizing line %d" % counter print(" tokenizing line %d" % counter)
token_ids = sentence_to_token_ids(line, vocab, tokenizer, token_ids = sentence_to_token_ids(line, vocab, tokenizer,
normalize_digits) normalize_digits)
tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n") tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n")
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
"""Functions for constructing vocabulary, converting the examples to integer format and building the required masks for batch computation Author: aneelakantan (Arvind Neelakantan) """Functions for constructing vocabulary, converting the examples to integer format and building the required masks for batch computation Author: aneelakantan (Arvind Neelakantan)
""" """
from __future__ import print_function
import copy import copy
import numbers import numbers
import numpy as np import numpy as np
...@@ -536,15 +538,15 @@ def add_special_words(utility): ...@@ -536,15 +538,15 @@ def add_special_words(utility):
utility.reverse_word_ids[utility.word_ids[ utility.reverse_word_ids[utility.word_ids[
utility.entry_match_token]] = utility.entry_match_token utility.entry_match_token]] = utility.entry_match_token
utility.entry_match_token_id = utility.word_ids[utility.entry_match_token] utility.entry_match_token_id = utility.word_ids[utility.entry_match_token]
print "entry match token: ", utility.word_ids[ print("entry match token: ", utility.word_ids[
utility.entry_match_token], utility.entry_match_token_id utility.entry_match_token], utility.entry_match_token_id)
utility.words.append(utility.column_match_token) utility.words.append(utility.column_match_token)
utility.word_ids[utility.column_match_token] = len(utility.word_ids) utility.word_ids[utility.column_match_token] = len(utility.word_ids)
utility.reverse_word_ids[utility.word_ids[ utility.reverse_word_ids[utility.word_ids[
utility.column_match_token]] = utility.column_match_token utility.column_match_token]] = utility.column_match_token
utility.column_match_token_id = utility.word_ids[utility.column_match_token] utility.column_match_token_id = utility.word_ids[utility.column_match_token]
print "entry match token: ", utility.word_ids[ print("entry match token: ", utility.word_ids[
utility.column_match_token], utility.column_match_token_id utility.column_match_token], utility.column_match_token_id)
utility.words.append(utility.dummy_token) utility.words.append(utility.dummy_token)
utility.word_ids[utility.dummy_token] = len(utility.word_ids) utility.word_ids[utility.dummy_token] = len(utility.word_ids)
utility.reverse_word_ids[utility.word_ids[ utility.reverse_word_ids[utility.word_ids[
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
"""Author: aneelakantan (Arvind Neelakantan) """Author: aneelakantan (Arvind Neelakantan)
""" """
from __future__ import print_function
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
import nn_utils import nn_utils
...@@ -545,7 +547,7 @@ class Graph(): ...@@ -545,7 +547,7 @@ class Graph():
self.batch_log_prob = tf.zeros([self.batch_size], dtype=self.data_type) self.batch_log_prob = tf.zeros([self.batch_size], dtype=self.data_type)
#Perform max_passes and at each pass select operation and column #Perform max_passes and at each pass select operation and column
for curr_pass in range(max_passes): for curr_pass in range(max_passes):
print "step: ", curr_pass print("step: ", curr_pass)
output, select, softmax, soft_softmax, column_softmax, soft_column_softmax = self.one_pass( output, select, softmax, soft_softmax, column_softmax, soft_column_softmax = self.one_pass(
select, question_embedding, hidden_vectors, hprev, prev_select_1, select, question_embedding, hidden_vectors, hprev, prev_select_1,
curr_pass) curr_pass)
...@@ -636,7 +638,7 @@ class Graph(): ...@@ -636,7 +638,7 @@ class Graph():
self.total_cost = self.compute_error() self.total_cost = self.compute_error()
optimize_params = self.params.values() optimize_params = self.params.values()
optimize_names = self.params.keys() optimize_names = self.params.keys()
print "optimize params ", optimize_names print("optimize params ", optimize_names)
if (self.utility.FLAGS.l2_regularizer > 0.0): if (self.utility.FLAGS.l2_regularizer > 0.0):
reg_cost = 0.0 reg_cost = 0.0
for ind_param in self.params.keys(): for ind_param in self.params.keys():
...@@ -645,7 +647,7 @@ class Graph(): ...@@ -645,7 +647,7 @@ class Graph():
grads = tf.gradients(self.total_cost, optimize_params, name="gradients") grads = tf.gradients(self.total_cost, optimize_params, name="gradients")
grad_norm = 0.0 grad_norm = 0.0
for p, name in zip(grads, optimize_names): for p, name in zip(grads, optimize_names):
print "grads: ", p, name print("grads: ", p, name)
if isinstance(p, tf.IndexedSlices): if isinstance(p, tf.IndexedSlices):
grad_norm += tf.reduce_sum(p.values * p.values) grad_norm += tf.reduce_sum(p.values * p.values)
elif not (p == None): elif not (p == None):
...@@ -675,4 +677,3 @@ class Graph(): ...@@ -675,4 +677,3 @@ class Graph():
self.step = adam.apply_gradients(zip(grads, optimize_params), self.step = adam.apply_gradients(zip(grads, optimize_params),
global_step=self.global_step) global_step=self.global_step)
self.init_op = tf.global_variables_initializer() self.init_op = tf.global_variables_initializer()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment