Course 4 Week 1 sampling_decode

Hello all,

I am having troubke with excercise 7. I get the following error:


TypeError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
586 if not self.has_backward:
→ 587 outputs = self.forward(x)
588 s = self.state

/opt/conda/lib/python3.7/site-packages/trax/layers/combinators.py in forward(self, xs)
67 “”“Executes this layer as part of a forward pass through the model.”“”
—> 68 self._validate_forward_inputs(xs)
69 if not self.sublayers: # No-op: outputs = inputs

/opt/conda/lib/python3.7/site-packages/trax/layers/combinators.py in _validate_forward_inputs(self, xs)
129 # TODO(jonni): Include full xs (or shape) in error message?
→ 130 len_xs = 1 if isinstance(xs, jnp.ndarray) else len(xs)
131 if len_xs < self.n_in:

TypeError: object of type ‘function’ has no len()

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
586 if not self.has_backward:
→ 587 outputs = self.forward(x)
588 s = self.state

/opt/conda/lib/python3.7/site-packages/trax/layers/combinators.py in forward(self, inputs)
210 # Note that zip silently truncates its result if lengths don’t match.
→ 211 sub_outputs, sub_state = layer.pure_fn(x, w, s, r, use_cache=True)
212 if layer.n_out == 1:

/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
605 raise LayerError(name, ‘pure_fn’,
→ 606 self._caller, signature(x), trace) from None
607

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in signature(obj)
101 else:
→ 102 return ShapeDtype(obj.shape, obj.dtype)
103

AttributeError: ‘function’ object has no attribute ‘shape’

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
586 if not self.has_backward:
→ 587 outputs = self.forward(x)
588 s = self.state

/opt/conda/lib/python3.7/site-packages/trax/layers/combinators.py in forward(self, xs)
87 inputs = inputs_from_stack(stack, layer.n_in)
—> 88 outputs, s = layer.pure_fn(inputs, w, s, rng, use_cache=True)
89 stack = outputs_onto_stack(outputs, stack, layer.n_in)

/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
605 raise LayerError(name, ‘pure_fn’,
→ 606 self._caller, signature(x), trace) from None
607

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in signature(obj)
96 if isinstance(obj, (list, tuple)):
—> 97 output = tuple(signature(x) for x in obj)
98 return output if isinstance(obj, tuple) else list(output)

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in (.0)
96 if isinstance(obj, (list, tuple)):
—> 97 output = tuple(signature(x) for x in obj)
98 return output if isinstance(obj, tuple) else list(output)

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in signature(obj)
101 else:
→ 102 return ShapeDtype(obj.shape, obj.dtype)
103

AttributeError: ‘function’ object has no attribute ‘shape’

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
in
1 # Test the function above. Try varying the temperature setting with values from 0 to 1.
2 # Run it several times with each setting and see how often the output changes.
----> 3 sampling_decode(“I love languages.”, NMTAttn=model, temperature=0.0, vocab_file=VOCAB_FILE, vocab_dir=VOCAB_DIR)

in sampling_decode(input_sentence, NMTAttn, temperature, vocab_file, vocab_dir, next_symbol, tokenize, detokenize)
38
39 # update the current output token by getting the index of the next word (hint: use next_symbol)
—> 40 cur_output, log_prob = next_symbol(NMTAttn, input_tokens, cur_output_tokens, temperature)
41
42 # append the current output token to the list of output tokens

in next_symbol(NMTAttn, input_tokens, cur_output_tokens, temperature)
33
34 # get the model prediction
—> 35 output, _ = NMTAttn((input_tokens, padded_with_batch))
36
37 # get log probabilities from the last token output

/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in call(self, x, weights, state, rng)
195 self.state = state # Needed if the model wasn’t fully initialized.
196 state = self.state
→ 197 outputs, new_state = self.pure_fn(x, weights, state, rng)
198 self.state = new_state
199 return outputs

/opt/conda/lib/python3.7/site-packages/trax/layers/base.py in pure_fn(self, x, weights, state, rng, use_cache)
604 name, trace = self._name, _short_traceback(skip=3)
605 raise LayerError(name, ‘pure_fn’,
→ 606 self._caller, signature(x), trace) from None
607
608 def output_signature(self, input_signature):

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in signature(obj)
95 “”"
96 if isinstance(obj, (list, tuple)):
—> 97 output = tuple(signature(x) for x in obj)
98 return output if isinstance(obj, tuple) else list(output)
99 elif isinstance(obj, dict):

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in (.0)
95 “”"
96 if isinstance(obj, (list, tuple)):
—> 97 output = tuple(signature(x) for x in obj)
98 return output if isinstance(obj, tuple) else list(output)
99 elif isinstance(obj, dict):

/opt/conda/lib/python3.7/site-packages/trax/shapes.py in signature(obj)
100 return {k: signature(v) for (k, v) in obj.items()}
101 else:
→ 102 return ShapeDtype(obj.shape, obj.dtype)
103
104

AttributeError: ‘function’ object has no attribute ‘shape’

Did anyone else get this and find a fix?

I have found the problem! I used trax.data.Tokenize rather than the tokenize argument in the function!