Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Code optimizations as per pylint suggestions #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions seq2seq/contrib/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Experiment(tf.contrib.learn.Experiment):
sharing issues."""

def __init__(self, train_steps_per_iteration=None, *args, **kwargs):
"""Sets number of training steps per iteration"""
super(Experiment, self).__init__(*args, **kwargs)
self._train_steps_per_iteration = train_steps_per_iteration

Expand Down
2 changes: 1 addition & 1 deletion seq2seq/data/input_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def make_data_provider(self, **kwargs):
items_to_descriptions={})

dataset_target = None
if len(self.params["target_files"]) > 0:
if self.params["target_files"]:
decoder_target = split_tokens_decoder.SplitTokensDecoder(
tokens_feature_name="target_tokens",
length_feature_name="target_len",
Expand Down
2 changes: 1 addition & 1 deletion seq2seq/encoders/conv_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def default_params():
"output_cnn.layers": 5,
"position_embeddings.enable": True,
"position_embeddings.combiner_fn": "tensorflow.multiply",
"position_embeddings.num_positions": 100,
"position_embeddings.num_positions": 100
}

def encode(self, inputs, sequence_length):
Expand Down
14 changes: 7 additions & 7 deletions seq2seq/encoders/pooling_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def position_encoding(sentence_size, embedding_size):
the fixed position encodings for each sentence position.
"""
encoding = np.ones((sentence_size, embedding_size), dtype=np.float32)
ls = sentence_size + 1
le = embedding_size + 1
for k in range(1, le):
for j in range(1, ls):
encoding[j-1, k-1] = (1.0 - j/float(ls)) - (
k / float(le)) * (1. - 2. * j/float(ls))
len_s = sentence_size + 1
len_e = embedding_size + 1
for k in range(1, len_e):
for j in range(1, len_s):
encoding[j-1, k-1] = (1.0 - j/float(len_s)) - (
k / float(len_e)) * (1. - 2. * j/float(len_s))
return encoding


Expand Down Expand Up @@ -120,7 +120,7 @@ def default_params():
"strides": 1,
"position_embeddings.enable": True,
"position_embeddings.combiner_fn": "tensorflow.multiply",
"position_embeddings.num_positions": 100,
"position_embeddings.num_positions": 100
}

def encode(self, inputs, sequence_length):
Expand Down
3 changes: 1 addition & 2 deletions seq2seq/encoders/rnn_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def _unpack_cell(cell):
expects a list of cells, one per layer."""
if isinstance(cell, tf.contrib.rnn.MultiRNNCell):
return cell._cells #pylint: disable=W0212
else:
return [cell]
return [cell]


def _default_rnn_cell_params():
Expand Down
9 changes: 4 additions & 5 deletions seq2seq/metrics/rouge.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _split_into_words(sentences):
def _get_word_ngrams(n, sentences):
"""Calculates word n-grams for multiple sentences.
"""
assert len(sentences) > 0
assert sentences
assert n > 0

words = _split_into_words(sentences)
Expand Down Expand Up @@ -126,12 +126,11 @@ def _recon(i, j):
"""private recon calculation"""
if i == 0 or j == 0:
return []
elif x[i - 1] == y[j - 1]:
if x[i - 1] == y[j - 1]:
return _recon(i - 1, j - 1) + [(x[i - 1], i)]
elif table[i - 1, j] > table[i, j - 1]:
if table[i - 1, j] > table[i, j - 1]:
return _recon(i - 1, j)
else:
return _recon(i, j - 1)
return _recon(i, j - 1)

recon_tuple = tuple(map(lambda x: x[0], _recon(i, j)))
return recon_tuple
Expand Down
5 changes: 2 additions & 3 deletions seq2seq/models/basic_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,5 @@ def decode(self, encoder_output, features, labels):
if self.mode == tf.contrib.learn.ModeKeys.INFER:
return self._decode_infer(decoder, bridge, encoder_output, features,
labels)
else:
return self._decode_train(decoder, bridge, encoder_output, features,
labels)
return self._decode_train(decoder, bridge, encoder_output, features,
labels)
48 changes: 0 additions & 48 deletions seq2seq/models/image2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import tensorflow as tf

from seq2seq import graph_utils
from seq2seq.data import vocab
from seq2seq.graph_utils import templatemethod
from seq2seq.models.model_base import ModelBase
from seq2seq.models.attention_seq2seq import AttentionSeq2Seq
Expand Down Expand Up @@ -69,49 +67,3 @@ def encode(self, features, _labels):

def batch_size(self, features, _labels):
return tf.shape(features["image"])[0]

def _preprocess(self, features, labels):
"""Model-specific preprocessing for features and labels:

- Creates vocabulary lookup tables for target vocab
- Converts tokens into vocabulary ids
- Prepends a speical "SEQUENCE_START" token to the target
- Appends a speical "SEQUENCE_END" token to the target
"""

# Create vocabulary look for target
target_vocab_to_id, target_id_to_vocab, target_word_to_count, _ = \
vocab.create_vocabulary_lookup_table(self.target_vocab_info.path)

# Add vocab tables to graph colection so that we can access them in
# other places.
graph_utils.add_dict_to_collection({
"target_vocab_to_id": target_vocab_to_id,
"target_id_to_vocab": target_id_to_vocab,
"target_word_to_count": target_word_to_count
}, "vocab_tables")

if labels is None:
return features, None

labels = labels.copy()

# Slices targets to max length
if self.params["target.max_seq_len"] is not None:
labels["target_tokens"] = labels["target_tokens"][:, :self.params[
"target.max_seq_len"]]
labels["target_len"] = tf.minimum(labels["target_len"],
self.params["target.max_seq_len"])

# Look up the target ids in the vocabulary
labels["target_ids"] = target_vocab_to_id.lookup(labels["target_tokens"])

labels["target_len"] = tf.to_int32(labels["target_len"])
tf.summary.histogram("target_len", tf.to_float(labels["target_len"]))

# Add to graph collection for later use
graph_utils.add_dict_to_collection(features, "features")
if labels:
graph_utils.add_dict_to_collection(labels, "labels")

return features, labels
13 changes: 6 additions & 7 deletions seq2seq/test/hooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import os
import tempfile
import shutil
import time

import tensorflow as tf
from tensorflow.python.training import monitored_session # pylint: disable=E0611
Expand All @@ -39,16 +38,16 @@ class TestPrintModelAnalysisHook(tf.test.TestCase):
def test_begin(self):
model_dir = tempfile.mkdtemp()
outfile = tempfile.NamedTemporaryFile()
tf.get_variable("weigths", [128, 128])
tf.get_variable("weights", [128, 128])
hook = hooks.PrintModelAnalysisHook(
params={}, model_dir=model_dir, run_config=tf.contrib.learn.RunConfig())
hook.begin()

with gfile.GFile(os.path.join(model_dir, "model_analysis.txt")) as file:
file_contents = file.read().strip()
file_contents = tf.compat.as_text(file.read()).strip()

self.assertEqual(file_contents.decode(), "_TFProfRoot (--/16.38k params)\n"
" weigths (128x128, 16.38k/16.38k params)")
self.assertEqual(file_contents, "_TFProfRoot (--/16.38k params)\n"
" weights (128x128, 16.38k/16.38k params)")
outfile.close()


Expand Down Expand Up @@ -94,7 +93,7 @@ def test_sampling(self):
outfile = os.path.join(self.sample_dir, "samples_000000.txt")
with open(outfile, "rb") as readfile:
self.assertIn("Prediction followed by Target @ Step 0",
readfile.read().decode("utf-8"))
tf.compat.as_text(readfile.read()))

# Should not trigger for step 9
sess.run(tf.assign(global_step, 9))
Expand All @@ -108,7 +107,7 @@ def test_sampling(self):
outfile = os.path.join(self.sample_dir, "samples_000010.txt")
with open(outfile, "rb") as readfile:
self.assertIn("Prediction followed by Target @ Step 10",
readfile.read().decode("utf-8"))
tf.compat.as_text(readfile.read()))


class TestMetadataCaptureHook(tf.test.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion seq2seq/test/pooling_encoder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def test_encode_without_pos(self):
})

if __name__ == "__main__":
tf.test.main()
tf.test.main()
8 changes: 3 additions & 5 deletions seq2seq/training/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import abc
import os

import numpy as np
import six
import yaml

Expand Down Expand Up @@ -97,10 +96,9 @@ def before_run(self, _run_context):
return
if not self._active:
return tf.train.SessionRunArgs(self._global_step)
else:
tf.logging.info("Performing full trace on next step.")
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #pylint: disable=E1101
return tf.train.SessionRunArgs(self._global_step, options=run_options)
tf.logging.info("Performing full trace on next step.")
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #pylint: disable=E1101
return tf.train.SessionRunArgs(self._global_step, options=run_options)

def after_run(self, _run_context, run_values):
if not self.is_chief or self._done:
Expand Down