# Attention modelf

**URL:** <https://community.deeplearning.ai/t/attention-modelf/32432>\
**Category:** Sequence Models\
**Tags:** coursera-platform\
**Created:** [September 1, 2021, 10:07am UTC](https://community.deeplearning.ai/t/attention-modelf/32432 "2021-09-01T10:07:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igba](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Igba](https://community.deeplearning.ai/u/Igba)\
**Post date:** [September 1, 2021, 10:07am UTC](https://community.deeplearning.ai/t/attention-modelf/32432/1 "2021-09-01T10:07:30Z")

</div>

Kindly help my code in week 3 assignment for attention Modelf is returning the following error;

ValueError Traceback (most recent call last)  
 in   
34  
35  
—\> 36 modelf\_test(modelf)

in modelf\_test(target)  
11  
12  
—\> 13 model = target(Tx, Ty, n\_a, n\_s, len\_human\_vocab, len\_machine\_vocab)  
14  
15 print(summary(model))

in modelf(Tx, Ty, n\_a, n\_s, human\_vocab\_size, machine\_vocab\_size)  
37  
38 # Step 2.A: Perform one step of the attention mechanism to get back the context vector at step t (≈ 1 line)  
—\> 39 context = one\_step\_attention(a[t],s[t-1])  
40  
41 # Step 2.B: Apply the post-attention LSTM cell to the “context” vector.

in one\_step\_attention(a, s\_prev)  
17 ### START CODE HERE ###  
18 # Use repeator to repeat s\_prev to be of shape (m, Tx, n\_s) so that you can concatenate it with all hidden states “a” (≈ 1 line)  
—\> 19 s\_prev = repeator(s\_prev)  
20 # Use concatenator to concatenate a and s\_prev on the last axis (≈ 1 line)  
21 # For grading purposes, please list ‘a’ first and ‘s\_prev’ second, in this order.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base\_layer.py in **call** (self, \*args, \*\*kwargs)  
924 if \_in\_functional\_construction\_mode(self, inputs, args, kwargs, input\_list):  
925 return self.\_functional\_construction\_call(inputs, args, kwargs,  
 → 926 input\_list)  
927  
928 # Maintains info about the `Layer.call` stack.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base\_layer.py in \_functional\_construction\_call(self, inputs, args, kwargs, input\_list)  
1090 # TODO(reedwm): We should assert input compatibility after the inputs  
1091 # are casted, not before.  
 → 1092 input\_spec.assert\_input\_compatibility(self.input\_spec, inputs, self.name)  
1093 graph = backend.get\_graph()  
1094 # Use `self._name_scope()` to avoid auto-incrementing the name.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input\_spec.py in assert\_input\_compatibility(input\_spec, inputs, layer\_name)  
178 ‘expected ndim=’ + str(spec.ndim) + ‘, found ndim=’ +  
179 str(ndim) + '. Full shape received: ’ +  
 → 180 str(x.shape.as\_list()))  
181 if spec.max\_ndim is not None:  
182 ndim = x.shape.ndims

ValueError: Input 0 of layer repeat\_vector is incompatible with the layer: expected ndim=2, found ndim=1. Full shape received: [64]

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [September 1, 2021, 11:55am UTC](https://community.deeplearning.ai/t/attention-modelf/32432/2 "2021-09-01T11:55:05Z")

</div>

Hi @Igba

The parameters passed to one\_step\_attention() are incorrect. It should be just a, output of the bi-directional LSTM, and s, previous state.  
a is sequence of hidden states, and s is initialised to s0, and t is the timestep. At each time step, t, until Ty, the same sequence of a is used, but s is updated after calling post\_activation\_LSTM\_cell().

---

<div class="post-metadata">

**Author:** ![Igba](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Igba](https://community.deeplearning.ai/u/Igba)\
**Post date:** [September 2, 2021, 8:11pm UTC](https://community.deeplearning.ai/t/attention-modelf/32432/3 "2021-09-02T20:11:21Z")

</div>

Thanks sir, but i am still getting same error. I sent you a private message of my code to vet please.

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [September 2, 2021, 8:47pm UTC](https://community.deeplearning.ai/t/attention-modelf/32432/4 "2021-09-02T20:47:07Z")

</div>

Hi @Igba

Two problems with your call to Bidirectional LSTM:

1. wrong input units, it should be : units=n\_a , not 1,  
because n\_a – hidden state size of the Bi-LSTM (see comments)
2. input shape is missing, it should be : input\_shape=(m,Tx,n\_a\*2)

---

<div class="post-metadata">

**Author:** ![Igba](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Igba](https://community.deeplearning.ai/u/Igba)\
**Post date:** [September 3, 2021, 5:02am UTC](https://community.deeplearning.ai/t/attention-modelf/32432/5 "2021-09-03T05:02:04Z")

</div>

> [@Kic](#):
>
> input\_shape=(m,Tx,n\_a\*2)

Thanks sir, I did this : a = Bidirectional(LSTM(units= n\_a, return\_sequences= True),input\_shape=(m,Tx,n\_a\*2))

but i still got this : AttributeError: ‘Bidirectional’ object has no attribute ‘shape’

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [September 3, 2021, 10:32am UTC](https://community.deeplearning.ai/t/attention-modelf/32432/6 "2021-09-03T10:32:44Z")

</div>

you missed the (X) at the end.

---

<div class="post-metadata">

**Author:** ![Igba](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Igba](https://community.deeplearning.ai/u/Igba)\
**Post date:** [September 4, 2021, 4:35am UTC](https://community.deeplearning.ai/t/attention-modelf/32432/7 "2021-09-04T04:35:02Z")

</div>

Many thanks sir, it worked.
