Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
6b9d5fba
"...pipelines/animatediff/pipeline_animatediff_controlnet.py" did not exist on "81780882b8cfb7628a2e09dbaae566ead5d760e8"
Unverified
Commit
6b9d5fba
authored
Jan 24, 2018
by
Toby Boyd
Committed by
GitHub
Jan 24, 2018
Browse files
Merge branch 'master' into patch-1
parents
5fd687c5
5fa2a4e6
Changes
147
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
100 additions
and
78 deletions
+100
-78
research/gan/image_compression/networks_test.py
research/gan/image_compression/networks_test.py
+1
-0
research/gan/mnist/util.py
research/gan/mnist/util.py
+1
-0
research/gan/mnist_estimator/train.py
research/gan/mnist_estimator/train.py
+1
-0
research/im2txt/README.md
research/im2txt/README.md
+5
-5
research/im2txt/im2txt/data/build_mscoco_data.py
research/im2txt/im2txt/data/build_mscoco_data.py
+1
-0
research/im2txt/im2txt/evaluate.py
research/im2txt/im2txt/evaluate.py
+2
-2
research/im2txt/im2txt/run_inference.py
research/im2txt/im2txt/run_inference.py
+1
-1
research/learned_optimizer/metaopt.py
research/learned_optimizer/metaopt.py
+1
-0
research/learning_to_remember_rare_events/data_utils.py
research/learning_to_remember_rare_events/data_utils.py
+1
-0
research/learning_to_remember_rare_events/memory.py
research/learning_to_remember_rare_events/memory.py
+1
-0
research/learning_to_remember_rare_events/train.py
research/learning_to_remember_rare_events/train.py
+1
-0
research/lfads/synth_data/generate_itb_data.py
research/lfads/synth_data/generate_itb_data.py
+5
-4
research/lfads/synth_data/generate_labeled_rnn_data.py
research/lfads/synth_data/generate_labeled_rnn_data.py
+1
-0
research/neural_gpu/data_utils.py
research/neural_gpu/data_utils.py
+1
-0
research/neural_gpu/neural_gpu.py
research/neural_gpu/neural_gpu.py
+4
-1
research/neural_gpu/neural_gpu_trainer.py
research/neural_gpu/neural_gpu_trainer.py
+26
-23
research/neural_gpu/program_utils.py
research/neural_gpu/program_utils.py
+21
-21
research/neural_gpu/wmt_utils.py
research/neural_gpu/wmt_utils.py
+13
-11
research/neural_programmer/data_utils.py
research/neural_programmer/data_utils.py
+6
-4
research/neural_programmer/model.py
research/neural_programmer/model.py
+7
-6
No files found.
research/gan/image_compression/networks_test.py
View file @
6b9d5fba
...
...
@@ -19,6 +19,7 @@ from __future__ import division
from
__future__
import
print_function
import
tensorflow
as
tf
from
six.moves
import
xrange
import
networks
...
...
research/gan/mnist/util.py
View file @
6b9d5fba
...
...
@@ -24,6 +24,7 @@ from __future__ import print_function
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
ds
=
tf
.
contrib
.
distributions
...
...
research/gan/mnist_estimator/train.py
View file @
6b9d5fba
...
...
@@ -22,6 +22,7 @@ import os
import
numpy
as
np
import
scipy.misc
from
six.moves
import
xrange
import
tensorflow
as
tf
from
mnist
import
data_provider
...
...
research/im2txt/README.md
View file @
6b9d5fba
...
...
@@ -119,8 +119,8 @@ First ensure that you have installed the following required packages:
*
**NumPy**
(
[
instructions
](
http://www.scipy.org/install.html
)
)
*
**Natural Language Toolkit (NLTK)**
:
*
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
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.
MSCOCO_DIR
=
"
${
HOME
}
/im2txt/data/mscoco"
# Build the preprocessing script.
cd
tensorflow-models
/im2txt
cd
research
/im2txt
bazel build //im2txt:download_and_preprocess_mscoco
# Run the preprocessing script.
...
...
@@ -212,7 +212,7 @@ INCEPTION_CHECKPOINT="${HOME}/im2txt/data/inception_v3.ckpt"
MODEL_DIR
=
"
${
HOME
}
/im2txt/model"
# Build the model.
cd
tensorflow-models
/im2txt
cd
research
/im2txt
bazel build
-c
opt //im2txt/...
# Run the training script.
...
...
@@ -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"
# Build the inference binary.
cd
tensorflow-models
/im2txt
cd
research
/im2txt
bazel build
-c
opt //im2txt:run_inference
# Ignore GPU devices (only necessary if your GPU is currently memory
...
...
research/im2txt/im2txt/data/build_mscoco_data.py
View file @
6b9d5fba
...
...
@@ -97,6 +97,7 @@ import threading
import
nltk.tokenize
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
tf
.
flags
.
DEFINE_string
(
"train_image_dir"
,
"/tmp/train2014/"
,
...
...
research/im2txt/im2txt/evaluate.py
View file @
6b9d5fba
...
...
@@ -76,7 +76,7 @@ def evaluate_model(sess, model, global_step, summary_writer, summary_op):
start_time
=
time
.
time
()
sum_losses
=
0.
sum_weights
=
0.
for
i
in
x
range
(
num_eval_batches
):
for
i
in
range
(
num_eval_batches
):
cross_entropy_losses
,
weights
=
sess
.
run
([
model
.
target_cross_entropy_losses
,
model
.
target_cross_entropy_loss_weights
...
...
@@ -143,7 +143,7 @@ def run_once(model, saver, summary_writer, summary_op):
global_step
=
global_step
,
summary_writer
=
summary_writer
,
summary_op
=
summary_op
)
except
Exception
,
e
:
# pylint: disable=broad-except
except
Exception
as
e
:
# pylint: disable=broad-except
tf
.
logging
.
error
(
"Evaluation failed."
)
coord
.
request_stop
(
e
)
...
...
research/im2txt/im2txt/run_inference.py
View file @
6b9d5fba
...
...
@@ -70,7 +70,7 @@ def main(_):
generator
=
caption_generator
.
CaptionGenerator
(
model
,
vocab
)
for
filename
in
filenames
:
with
tf
.
gfile
.
GFile
(
filename
,
"r"
)
as
f
:
with
tf
.
gfile
.
GFile
(
filename
,
"r
b
"
)
as
f
:
image
=
f
.
read
()
captions
=
generator
.
beam_search
(
sess
,
image
)
print
(
"Captions for image %s:"
%
os
.
path
.
basename
(
filename
))
...
...
research/learned_optimizer/metaopt.py
View file @
6b9d5fba
...
...
@@ -21,6 +21,7 @@ import sys
import
time
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
from
learned_optimizer.optimizer
import
trainable_optimizer
...
...
research/learning_to_remember_rare_events/data_utils.py
View file @
6b9d5fba
...
...
@@ -29,6 +29,7 @@ import numpy as np
from
scipy.misc
import
imresize
from
scipy.misc
import
imrotate
from
scipy.ndimage
import
imread
from
six.moves
import
xrange
import
tensorflow
as
tf
...
...
research/learning_to_remember_rare_events/memory.py
View file @
6b9d5fba
...
...
@@ -23,6 +23,7 @@ published as a conference paper at ICLR 2017.
"""
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
...
...
research/learning_to_remember_rare_events/train.py
View file @
6b9d5fba
...
...
@@ -26,6 +26,7 @@ import os
import
random
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
import
data_utils
...
...
research/lfads/synth_data/generate_itb_data.py
View file @
6b9d5fba
...
...
@@ -18,6 +18,7 @@ from __future__ import print_function
import
h5py
import
numpy
as
np
import
os
from
six.moves
import
xrange
import
tensorflow
as
tf
from
utils
import
write_datasets
...
...
@@ -47,12 +48,12 @@ flags.DEFINE_float("max_firing_rate", 30.0,
flags
.
DEFINE_float
(
"u_std"
,
0.25
,
"Std dev of input to integration to bound model"
)
flags
.
DEFINE_string
(
"checkpoint_path"
,
"SAMPLE_CHECKPOINT"
,
"""Path to directory with checkpoints of model
"""Path to directory with checkpoints of model
trained on integration to bound task. Currently this
is a placeholder which tells the code to grab the
checkpoint that is provided with the code
(in /trained_itb/..). If you have your own checkpoint
you would like to restore, you would point it to
checkpoint that is provided with the code
(in /trained_itb/..). If you have your own checkpoint
you would like to restore, you would point it to
that path."""
)
FLAGS
=
flags
.
FLAGS
...
...
research/lfads/synth_data/generate_labeled_rnn_data.py
View file @
6b9d5fba
...
...
@@ -18,6 +18,7 @@ from __future__ import print_function
import
os
import
h5py
import
numpy
as
np
from
six.moves
import
xrange
from
synthetic_data_utils
import
generate_data
,
generate_rnn
from
synthetic_data_utils
import
get_train_n_valid_inds
...
...
research/neural_gpu/data_utils.py
View file @
6b9d5fba
...
...
@@ -21,6 +21,7 @@ import sys
import
time
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
import
program_utils
...
...
research/neural_gpu/neural_gpu.py
View file @
6b9d5fba
...
...
@@ -17,6 +17,7 @@
import
time
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
from
tensorflow.python.framework
import
function
...
...
@@ -500,8 +501,10 @@ class NeuralGPU(object):
return
tf
.
reduce_sum
(
encoder_outputs
*
tf
.
expand_dims
(
mask
,
2
),
1
)
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."""
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
,
name
=
"attn_query"
)
attn_res
=
attention_query
(
attn_q
,
tf
.
get_variable
(
...
...
research/neural_gpu/neural_gpu_trainer.py
View file @
6b9d5fba
...
...
@@ -14,6 +14,8 @@
# ==============================================================================
"""Neural GPU."""
from
__future__
import
print_function
import
math
import
os
import
random
...
...
@@ -22,6 +24,7 @@ import threading
import
time
import
numpy
as
np
from
six.moves
import
xrange
import
tensorflow
as
tf
import
program_utils
...
...
@@ -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
):
counter
+=
1
if
counter
%
100000
==
0
and
print_out
:
print
" reading data line %d"
%
counter
print
(
" reading data line %d"
%
counter
)
sys
.
stdout
.
flush
()
source_ids
=
[
int
(
x
)
for
x
in
source
.
split
()]
target_ids
=
[
int
(
x
)
for
x
in
target
.
split
()]
...
...
@@ -188,7 +191,7 @@ def read_data_into_global(source_path, target_path, buckets,
global_train_set
[
"wmt"
].
append
(
data_set
)
train_total_size
=
calculate_buckets_scale
(
data_set
,
buckets
,
"wmt"
)
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
):
...
...
@@ -552,7 +555,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False,
for
h
in
history
]
tgt_set
=
set
(
target
)
if
print_out
:
print
"target: "
,
tgt_prog
print
(
"target: "
,
tgt_prog
)
inps
,
tgt_outs
=
[],
[]
for
i
in
xrange
(
3
):
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,
if
len
(
olist
)
==
1
:
tgt_outs
.
append
(
olist
[
0
])
else
:
print
[
program_utils
.
prog_vocab
[
x
]
for
x
in
ilist
if
x
>
0
]
print
olist
print
tgt_prog
print
program_utils
.
evaluate
(
tgt_prog
,
{
"a"
:
inps
[
-
1
]})
print
"AAAAA"
print
(
[
program_utils
.
prog_vocab
[
x
]
for
x
in
ilist
if
x
>
0
]
)
print
(
olist
)
print
(
tgt_prog
)
print
(
program_utils
.
evaluate
(
tgt_prog
,
{
"a"
:
inps
[
-
1
]})
)
print
(
"AAAAA"
)
tgt_outs
.
append
(
olist
[
0
])
if
not
test_mode
:
for
_
in
xrange
(
7
):
...
...
@@ -602,7 +605,7 @@ def score_beams_prog(beams, target, inp, history, print_out=False,
best_prog
=
b_prog
best_score
=
score
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
...
...
@@ -719,7 +722,7 @@ def train():
inp
=
new_inp
# If all results are great, stop (todo: not to wait for all?).
if
FLAGS
.
nprint
>
1
:
print
scores
print
(
scores
)
if
sum
(
scores
)
/
float
(
len
(
scores
))
>=
10.0
:
break
# The final step with the true target.
...
...
@@ -735,7 +738,7 @@ def train():
errors
,
total
,
seq_err
=
data
.
accuracy
(
inp
,
res
,
target
,
batch_size
,
0
,
new_target
,
scores
)
if
FLAGS
.
nprint
>
1
:
print
"seq_err: "
,
seq_err
print
(
"seq_err: "
,
seq_err
)
acc_total
+=
total
acc_errors
+=
errors
acc_seq_err
+=
seq_err
...
...
@@ -944,8 +947,8 @@ def interactive():
for
v
in
tf
.
trainable_variables
():
shape
=
v
.
get_shape
().
as_list
()
total
+=
mul
(
shape
)
print
(
v
.
name
,
shape
,
mul
(
shape
))
print
total
print
(
v
.
name
,
shape
,
mul
(
shape
))
print
(
total
)
# Start interactive loop.
sys
.
stdout
.
write
(
"Input to Neural GPU Translation Model.
\n
"
)
sys
.
stdout
.
write
(
"> "
)
...
...
@@ -960,7 +963,7 @@ def interactive():
normalize_digits
=
FLAGS
.
normalize_digits
)
else
:
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?
buckets
=
[
b
for
b
in
xrange
(
len
(
data
.
bins
))
if
data
.
bins
[
b
]
>=
max
(
len
(
token_ids
),
len
(
cures
))]
...
...
@@ -986,12 +989,12 @@ def interactive():
loss
=
loss
[
0
]
-
(
data
.
bins
[
bucket_id
]
*
FLAGS
.
length_norm
)
outputs
=
[
int
(
np
.
argmax
(
logit
,
axis
=
1
))
for
logit
in
output_logits
]
print
[
rev_fr_vocab
[
t
]
for
t
in
outputs
]
print
loss
,
data
.
bins
[
bucket_id
]
print
linearize
(
outputs
,
rev_fr_vocab
)
print
(
[
rev_fr_vocab
[
t
]
for
t
in
outputs
]
)
print
(
loss
,
data
.
bins
[
bucket_id
]
)
print
(
linearize
(
outputs
,
rev_fr_vocab
)
)
cures
.
append
(
outputs
[
gen_idx
])
print
cures
print
linearize
(
cures
,
rev_fr_vocab
)
print
(
cures
)
print
(
linearize
(
cures
,
rev_fr_vocab
)
)
if
FLAGS
.
simple_tokenizer
:
cur_out
=
outputs
if
wmt
.
EOS_ID
in
cur_out
:
...
...
@@ -1002,11 +1005,11 @@ def interactive():
if
loss
<
result_cost
:
result
=
outputs
result_cost
=
loss
print
(
"FINAL"
,
result_cost
)
print
[
rev_fr_vocab
[
t
]
for
t
in
result
]
print
linearize
(
result
,
rev_fr_vocab
)
print
(
"FINAL"
,
result_cost
)
print
(
[
rev_fr_vocab
[
t
]
for
t
in
result
]
)
print
(
linearize
(
result
,
rev_fr_vocab
)
)
else
:
print
"TOOO_LONG"
print
(
"TOOO_LONG"
)
sys
.
stdout
.
write
(
"> "
)
sys
.
stdout
.
flush
()
inpt
=
sys
.
stdin
.
readline
(),
""
...
...
research/neural_gpu/program_utils.py
View file @
6b9d5fba
...
...
@@ -31,7 +31,7 @@ class ListType(object):
if
not
isinstance
(
other
,
ListType
):
return
False
return
self
.
arg
==
other
.
arg
def
__hash__
(
self
):
return
hash
(
self
.
arg
)
...
...
@@ -68,7 +68,7 @@ class FunctionType(object):
class
Function
(
object
):
def
__init__
(
self
,
name
,
arg_types
,
output_type
,
fn_arg_types
=
None
):
self
.
name
=
name
self
.
name
=
name
self
.
arg_types
=
arg_types
self
.
fn_arg_types
=
fn_arg_types
or
[]
self
.
output_type
=
output_type
...
...
@@ -113,14 +113,14 @@ def minus_one(x): return x - 1
def
times_two
(
x
):
return
x
*
2
def
neg
(
x
):
return
x
*
(
-
1
)
def
div_two
(
x
):
return
int
(
x
/
2
)
def
sq
(
x
):
return
x
**
2
def
sq
(
x
):
return
x
**
2
def
times_three
(
x
):
return
x
*
3
def
div_three
(
x
):
return
int
(
x
/
3
)
def
times_four
(
x
):
return
x
*
4
def
div_four
(
x
):
return
int
(
x
/
4
)
# Int -> Bool
def
pos
(
x
):
return
x
>
0
# Int -> Bool
def
pos
(
x
):
return
x
>
0
def
neg
(
x
):
return
x
<
0
def
even
(
x
):
return
x
%
2
==
0
def
odd
(
x
):
return
x
%
2
==
1
...
...
@@ -131,24 +131,24 @@ def sub(x, y): return x - y
def
mul
(
x
,
y
):
return
x
*
y
# HOFs
f_map
=
Function
(
"map"
,
[
ListType
(
"Int"
)],
ListType
(
"Int"
),
f_map
=
Function
(
"map"
,
[
ListType
(
"Int"
)],
ListType
(
"Int"
),
[
FunctionType
([
"Int"
,
"Int"
])])
f_filter
=
Function
(
"filter"
,
[
ListType
(
"Int"
)],
ListType
(
"Int"
),
f_filter
=
Function
(
"filter"
,
[
ListType
(
"Int"
)],
ListType
(
"Int"
),
[
FunctionType
([
"Int"
,
"Bool"
])])
f_count
=
Function
(
"c_count"
,
[
ListType
(
"Int"
)],
"Int"
,
f_count
=
Function
(
"c_count"
,
[
ListType
(
"Int"
)],
"Int"
,
[
FunctionType
([
"Int"
,
"Bool"
])])
def
c_count
(
f
,
xs
):
return
len
([
x
for
x
in
xs
if
f
(
x
)])
f_zipwith
=
Function
(
"c_zipwith"
,
[
ListType
(
"Int"
),
ListType
(
"Int"
)],
ListType
(
"Int"
),
f_zipwith
=
Function
(
"c_zipwith"
,
[
ListType
(
"Int"
),
ListType
(
"Int"
)],
ListType
(
"Int"
),
[
FunctionType
([
"Int"
,
"Int"
,
"Int"
])])
#FIX
def
c_zipwith
(
f
,
xs
,
ys
):
return
[
f
(
x
,
y
)
for
(
x
,
y
)
in
zip
(
xs
,
ys
)]
f_scan
=
Function
(
"c_scan"
,
[
ListType
(
"Int"
)],
ListType
(
"Int"
),
ListType
(
"Int"
),
[
FunctionType
([
"Int"
,
"Int"
,
"Int"
])])
def
c_scan
(
f
,
xs
):
out
=
xs
...
...
@@ -177,7 +177,7 @@ def evaluate(program_str, input_names_to_vals, default="ERROR"):
with
stdoutIO
()
as
s
:
# pylint: disable=bare-except
try
:
exec
exec_str
+
" print(out)"
exec
(
exec_str
+
" print(out)"
)
return
s
.
getvalue
()[:
-
1
]
except
:
return
default
...
...
@@ -186,7 +186,7 @@ def evaluate(program_str, input_names_to_vals, default="ERROR"):
class
Statement
(
object
):
"""Statement class."""
def
__init__
(
self
,
fn
,
output_var
,
arg_vars
,
fn_args
=
None
):
self
.
fn
=
fn
self
.
output_var
=
output_var
...
...
@@ -290,7 +290,7 @@ class Program(object):
with
stdoutIO
()
as
s
:
# pylint: disable=exec-used
exec
inp_str
+
self
.
body
+
"; print(out)"
exec
(
inp_str
+
self
.
body
+
"; print(out)"
)
# pylint: enable=exec-used
return
s
.
getvalue
()[:
-
1
]
...
...
@@ -412,11 +412,11 @@ def gen(max_len, how_many):
else
:
outcomes_to_programs
[
outcome_str
]
=
t
.
flat_str
()
if
counter
%
5000
==
0
:
print
"== proggen: tried: "
+
str
(
counter
)
print
"== proggen: kept: "
+
str
(
len
(
outcomes_to_programs
))
print
(
"== proggen: tried: "
+
str
(
counter
)
)
print
(
"== proggen: kept: "
+
str
(
len
(
outcomes_to_programs
))
)
if
counter
%
250000
==
0
and
save_prefix
is
not
None
:
print
"saving..."
print
(
"saving..."
)
save_counter
=
0
progfilename
=
os
.
path
.
join
(
save_prefix
,
"prog_"
+
str
(
counter
)
+
".txt"
)
iofilename
=
os
.
path
.
join
(
save_prefix
,
"io_"
+
str
(
counter
)
+
".txt"
)
...
...
@@ -431,7 +431,7 @@ def gen(max_len, how_many):
for
(
o
,
p
)
in
outcomes_to_programs
.
iteritems
():
save_counter
+=
1
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
"
)
fi
.
write
(
o
+
"
\n
"
)
ftp
.
write
(
str
(
tokenize
(
p
,
tokens
))
+
"
\n
"
)
...
...
research/neural_gpu/wmt_utils.py
View file @
6b9d5fba
...
...
@@ -14,6 +14,8 @@
# ==============================================================================
"""Utilities for downloading data from WMT, tokenizing, vocabularies."""
from
__future__
import
print_function
import
gzip
import
os
import
re
...
...
@@ -53,20 +55,20 @@ _WMT_ENFR_DEV_URL = "http://www.statmt.org/wmt15/dev-v2.tgz"
def
maybe_download
(
directory
,
filename
,
url
):
"""Download filename from url unless it's already in directory."""
if
not
tf
.
gfile
.
Exists
(
directory
):
print
"Creating directory %s"
%
directory
print
(
"Creating directory %s"
%
directory
)
os
.
mkdir
(
directory
)
filepath
=
os
.
path
.
join
(
directory
,
filename
)
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
)
statinfo
=
os
.
stat
(
filepath
)
print
"Successfully downloaded"
,
filename
,
statinfo
.
st_size
,
"bytes"
print
(
"Successfully downloaded"
,
filename
,
statinfo
.
st_size
,
"bytes"
)
return
filepath
def
gunzip_file
(
gz_path
,
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
open
(
new_path
,
"wb"
)
as
new_file
:
for
line
in
gz_file
:
...
...
@@ -80,7 +82,7 @@ def get_wmt_enfr_train_set(directory):
tf
.
gfile
.
Exists
(
train_path
+
".en"
)):
corpus_file
=
maybe_download
(
directory
,
"training-giga-fren.tar"
,
_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
:
corpus_tar
.
extractall
(
directory
)
gunzip_file
(
train_path
+
".fr.gz"
,
train_path
+
".fr"
)
...
...
@@ -95,7 +97,7 @@ def get_wmt_enfr_dev_set(directory):
if
not
(
tf
.
gfile
.
Exists
(
dev_path
+
".fr"
)
and
tf
.
gfile
.
Exists
(
dev_path
+
".en"
)):
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
:
fr_dev_file
=
dev_tar
.
getmember
(
"dev/"
+
dev_name
+
".fr"
)
en_dev_file
=
dev_tar
.
getmember
(
"dev/"
+
dev_name
+
".en"
)
...
...
@@ -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.
"""
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
=
{},
{}
for
c
in
_PUNCTUATION
:
chars
[
c
]
=
1
...
...
@@ -218,7 +220,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
line
=
" "
.
join
(
line_in
.
split
())
counter
+=
1
if
counter
%
100000
==
0
:
print
" processing fr line %d"
%
counter
print
(
" processing fr line %d"
%
counter
)
for
c
in
line
:
if
c
in
chars
:
chars
[
c
]
+=
1
...
...
@@ -240,7 +242,7 @@ def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
line
=
" "
.
join
(
line_in
.
split
())
counter
+=
1
if
counter
%
100000
==
0
:
print
" processing en line %d"
%
counter
print
(
" processing en line %d"
%
counter
)
for
c
in
line
:
if
c
in
chars
:
chars
[
c
]
+=
1
...
...
@@ -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.
"""
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
)
with
tf
.
gfile
.
GFile
(
data_path
,
mode
=
"rb"
)
as
data_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,
for
line
in
data_file
:
counter
+=
1
if
counter
%
100000
==
0
:
print
" tokenizing line %d"
%
counter
print
(
" tokenizing line %d"
%
counter
)
token_ids
=
sentence_to_token_ids
(
line
,
vocab
,
tokenizer
,
normalize_digits
)
tokens_file
.
write
(
" "
.
join
([
str
(
tok
)
for
tok
in
token_ids
])
+
"
\n
"
)
...
...
research/neural_programmer/data_utils.py
View file @
6b9d5fba
...
...
@@ -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)
"""
from
__future__
import
print_function
import
copy
import
numbers
import
numpy
as
np
...
...
@@ -536,15 +538,15 @@ def add_special_words(utility):
utility
.
reverse_word_ids
[
utility
.
word_ids
[
utility
.
entry_match_token
]]
=
utility
.
entry_match_token
utility
.
entry_match_token_id
=
utility
.
word_ids
[
utility
.
entry_match_token
]
print
"entry match token: "
,
utility
.
word_ids
[
utility
.
entry_match_token
],
utility
.
entry_match_token_id
print
(
"entry match token: "
,
utility
.
word_ids
[
utility
.
entry_match_token
],
utility
.
entry_match_token_id
)
utility
.
words
.
append
(
utility
.
column_match_token
)
utility
.
word_ids
[
utility
.
column_match_token
]
=
len
(
utility
.
word_ids
)
utility
.
reverse_word_ids
[
utility
.
word_ids
[
utility
.
column_match_token
]]
=
utility
.
column_match_token
utility
.
column_match_token_id
=
utility
.
word_ids
[
utility
.
column_match_token
]
print
"entry match token: "
,
utility
.
word_ids
[
utility
.
column_match_token
],
utility
.
column_match_token_id
print
(
"entry match token: "
,
utility
.
word_ids
[
utility
.
column_match_token
],
utility
.
column_match_token_id
)
utility
.
words
.
append
(
utility
.
dummy_token
)
utility
.
word_ids
[
utility
.
dummy_token
]
=
len
(
utility
.
word_ids
)
utility
.
reverse_word_ids
[
utility
.
word_ids
[
...
...
research/neural_programmer/model.py
View file @
6b9d5fba
...
...
@@ -15,6 +15,8 @@
"""Author: aneelakantan (Arvind Neelakantan)
"""
from
__future__
import
print_function
import
numpy
as
np
import
tensorflow
as
tf
import
nn_utils
...
...
@@ -545,7 +547,7 @@ class Graph():
self
.
batch_log_prob
=
tf
.
zeros
([
self
.
batch_size
],
dtype
=
self
.
data_type
)
#Perform max_passes and at each pass select operation and column
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
(
select
,
question_embedding
,
hidden_vectors
,
hprev
,
prev_select_1
,
curr_pass
)
...
...
@@ -633,10 +635,10 @@ class Graph():
self
.
params
=
params
batch_size
=
self
.
batch_size
learning_rate
=
tf
.
cast
(
self
.
utility
.
FLAGS
.
learning_rate
,
self
.
data_type
)
self
.
total_cost
=
self
.
compute_error
()
self
.
total_cost
=
self
.
compute_error
()
optimize_params
=
self
.
params
.
values
()
optimize_names
=
self
.
params
.
keys
()
print
"optimize params "
,
optimize_names
print
(
"optimize params "
,
optimize_names
)
if
(
self
.
utility
.
FLAGS
.
l2_regularizer
>
0.0
):
reg_cost
=
0.0
for
ind_param
in
self
.
params
.
keys
():
...
...
@@ -645,7 +647,7 @@ class Graph():
grads
=
tf
.
gradients
(
self
.
total_cost
,
optimize_params
,
name
=
"gradients"
)
grad_norm
=
0.0
for
p
,
name
in
zip
(
grads
,
optimize_names
):
print
"grads: "
,
p
,
name
print
(
"grads: "
,
p
,
name
)
if
isinstance
(
p
,
tf
.
IndexedSlices
):
grad_norm
+=
tf
.
reduce_sum
(
p
.
values
*
p
.
values
)
elif
not
(
p
==
None
):
...
...
@@ -672,7 +674,6 @@ class Graph():
learning_rate
,
epsilon
=
tf
.
cast
(
self
.
utility
.
FLAGS
.
eps
,
self
.
data_type
),
use_locking
=
True
)
self
.
step
=
adam
.
apply_gradients
(
zip
(
grads
,
optimize_params
),
self
.
step
=
adam
.
apply_gradients
(
zip
(
grads
,
optimize_params
),
global_step
=
self
.
global_step
)
self
.
init_op
=
tf
.
global_variables_initializer
()
Prev
1
2
3
4
5
6
7
8
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment