sdanaipat / fairseq-translator

A quick Tensorflow implementation of Facebook FairSeq[1] for character-level neural machine translation (EN -> JP).
14 stars 5 forks source link

I got InvalidArgumentError #3

Closed pdc-kaminaga closed 4 years ago

pdc-kaminaga commented 4 years ago

Hello.

I got InvalidArgumentError by running Test [GLU].ipynb. Please tell me how to solve this problem?

python: 3.7.5rc1 torch:1.4.0

In [19]: translate(["It's windy tomorrow"])

InvalidArgumentError Traceback (most recent call last) ~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, args) 1355 try: -> 1356 return fn(args) 1357 except errors.OpError as e:

~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1340 return self._call_tf_sessionrun( -> 1341 options, feed_dict, fetch_list, target_list, run_metadata) 1342

~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1428 self._session, options, feed_dict, fetch_list, target_list, -> 1429 run_metadata) 1430

InvalidArgumentError: indices[0,7] = 137 is not in [0, 135) [[{{node embedding/embedding_lookup}}]]

During handling of the above exception, another exception occurred:

InvalidArgumentError Traceback (most recent call last)

in ----> 1 translate(["It's windy tomorrow"]) in translate(text) 6 T[i, j] = dictionary.get(x, 2) 7 T[i, j + 1] = 3 ----> 8 zu_inputs, ze_inputs = encode(isess, T) 9 answers = beam_search(isess, zu_inputs, ze_inputs, batch_size=T.shape[0], beam_size=4, num_ans=10, max_len=402, normalize_by_len=0.1) 10 for i, ans in enumerate(answers): in encode(sess, enc_inputs) 2 input_feed = {X: enc_inputs} 3 outputs = [zu, ze] ----> 4 return sess.run(outputs, input_feed) 5 6 def decode_topk(sess, zu_inputs, ze_inputs, dec_inputs, beam_size): ~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 948 try: 949 result = self._run(None, fetches, feed_dict, options_ptr, --> 950 run_metadata_ptr) 951 if run_metadata: 952 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) ~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1171 if final_fetches or final_targets or (handle and feed_dict_tensor): 1172 results = self._do_run(handle, final_targets, final_fetches, -> 1173 feed_dict_tensor, options, run_metadata) 1174 else: 1175 results = [] ~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1348 if handle is None: 1349 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1350 run_metadata) 1351 else: 1352 return self._do_call(_prun_fn, handle, feeds, fetches) ~/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1368 pass 1369 message = error_interpolation.interpolate(message, self._graph) -> 1370 raise type(e)(node_def, op, message) 1371 1372 def _extend_graph(self): InvalidArgumentError: indices[0,7] = 137 is not in [0, 135) [[node embedding/embedding_lookup (defined at :22) ]] Errors may have originated from an input operation. Input Source operations connected to node embedding/embedding_lookup: embedding/en_emb/read (defined at :6) Placeholder (defined at :3) Original stack trace for 'embedding/embedding_lookup': File "/usr/local/var/pyenv/versions/3.7.5rc1/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/local/var/pyenv/versions/3.7.5rc1/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/traitlets/config/application.py", line 664, in launch_instance app.start() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 583, in start self.io_loop.start() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 148, in start self.asyncio_loop.run_forever() File "/usr/local/var/pyenv/versions/3.7.5rc1/lib/python3.7/asyncio/base_events.py", line 534, in run_forever self._run_once() File "/usr/local/var/pyenv/versions/3.7.5rc1/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once handle._run() File "/usr/local/var/pyenv/versions/3.7.5rc1/lib/python3.7/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/ioloop.py", line 690, in lambda f: self._run_callback(functools.partial(callback, future)) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/ioloop.py", line 743, in _run_callback ret = callback() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 787, in inner self.run() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 748, in run yielded = self.gen.send(value) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 377, in dispatch_queue yield self.process_one() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 225, in wrapper runner = Runner(result, future, yielded) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 714, in __init__ self.run() File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 748, in run yielded = self.gen.send(value) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 361, in process_one yield gen.maybe_future(dispatch(*args)) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 541, in execute_request user_expressions, allow_stdin, File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 300, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 536, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2858, in run_cell raw_cell, store_history, silent, shell_futures) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2886, in _run_cell return runner(coro) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner coro.send(None) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3063, in run_cell_async interactivity=interactivity, compiler=compiler, result=result) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3254, in run_ast_nodes if (await self.run_code(code, result, async_=asy)): File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 6, in en_emb, jp_emb = init_embedding(X, Y, reuse=False) File "", line 22, in init_embedding X_emb = tf.nn.embedding_lookup(en_emb, X) + P_in[:tf.shape(X)[1], :] File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/ops/embedding_ops.py", line 315, in embedding_lookup transform_fn=None) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/ops/embedding_ops.py", line 133, in _embedding_lookup_and_transform array_ops.gather(params[0], ids, name=name), ids, max_norm) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper return target(*args, **kwargs) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 3475, in gather return gen_array_ops.gather_v2(params, indices, axis, name=name) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 4097, in gather_v2 batch_dims=batch_dims, name=name) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper op_def=op_def) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3616, in create_op op_def=op_def) File "/Users/kami/python_env/FAIRSEQ/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 2005, in __init__ self._traceback = tf_stack.extract_stack()
sdanaipat commented 4 years ago

Hi, Thank you for your question!

This error is mainly my fault for hardcoding the vocab_sizes (sorry 🙇‍♂️). As the Tatoeba parallel corpus might have changed, en_vocab_size and jp_vocab_size also change. As a result, you're seeing the above error message:

InvalidArgumentError: indices[0,7] = 137 is not in [0, 135)

Here is how you fix this issue: in Preprocessing.ipynb please confirm the size of eng_dict and jpn_dict :

en_vocab_size = len(eng_dict)
ja_vocab_size = len(jpb_dict)

then in both notebooks used for training and testing, substitute the part where en_vocab_size and jp_vocab_size are defined with the above mentioned numbers:

en_vocab_size = <en_vocab_size> + 4
jp_vocab_size = <ja_vocab_size> + 4

I hope this fix your problem. Let me know if it's still unclear.

pdc-kaminaga commented 4 years ago

Thanks for your reply. I'll try my best!

Preprocessing.ipynb In[7] en_vocab_size = len(eng_dict) ja_vocab_size = len(jpn_dict) print(en_vocab_size) --> 139 print(ja_vocab_size) --> 3208

Train.ipynb In[5] preprocessing_en_vocab_size = 139 preprocessing_ja_vocab_size = 3208 en_vocab_size = preprocessing_en_vocab_size + 4 jp_vocab_size = preprocessing_ja_vocab_size + 4

Test.ipynb In[6] preprocessing_en_vocab_size = 139 preprocessing_ja_vocab_size = 3208 en_vocab_size = preprocessing_en_vocab_size + 4 jp_vocab_size = preprocessing_ja_vocab_size + 4