C5W4 : week-4 Exercise 5 - Encoder : 'Embedding' object has no attribute 'embeddings'

I am getting :

ipython-input-21-68aa9b8f5815> in
1 # UNIT TEST
----> 2 Encoder_test(Encoder)

~/work/W4A1/public_tests.py in Encoder_test(target)
114 x = np.array([[2, 1, 3], [1, 2, 0]])
115
→ 116 encoderq_output = encoderq(x, True, None)
117
118 assert tf.is_tensor(encoderq_output), “Wrong type. Output must be a tensor”

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
→ 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:

in call(self, x, training, mask)
47 # Pass input through the Embedding layer
48
—> 49 x = self.embedding.call(x) # (batch_size, input_seq_len, embedding_dim)
50 # Scale embedding by multiplying it by the square root of the embedding dimension
51 x *= np.sqrt(tf.cast(embedding_dim, tf.float32))

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/embeddings.py in call(self, inputs)
191 if dtype != ‘int32’ and dtype != ‘int64’:
192 inputs = math_ops.cast(inputs, ‘int32’)
→ 193 if isinstance(self.embeddings, sharded_variable.ShardedVariable):
194 out = embedding_ops.embedding_lookup_v2(self.embeddings.variables, inputs)
195 else:

AttributeError: ‘Embedding’ object has no attribute ‘embeddings’

The embedding is defined in the given __init__ function:

def __init__(self, num_layers, embedding_dim, num_heads, fully_connected_dim, input_vocab_size,
               maximum_position_encoding, dropout_rate=0.1, layernorm_eps=1e-6):
        super(Encoder, self).__init__()

        self.embedding_dim = embedding_dim
        self.num_layers = num_layers

        self.embedding = Embedding(input_vocab_size, self.embedding_dim)

The problem is that this is not the correct syntax to invoke it:

Try just:

x = self.embedding(x)

2 Likes

Thanks,

It seems to work now, I tried it before and it did. Don’t know why. But the main thing is it works now.

Dov

2 Likes