C3W1 Cannot build model error

C3 W1 -->Error in cell 47:

Expected output
Model: "grulm"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 embedding (Embedding)    (None, 100, 256)          20992     
                                                                 
 gru (GRU)                [(None, 100, 512),        1182720   
                              (None, 512)]                       
                                                                 
 dense (Dense)            (None, 100, 82)           42066     
                                                                 
=================================================================
Total params: 1245778 (4.75 MB)
Trainable params: 1245778 (4.75 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Error:
Traceback (most recent call last):
File “/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py”, line 521, in build
self.call(x, **kwargs)
File “/tmp/ipykernel_14/3772264745.py”, line 35, in call
x, states = self.gru(x, initial_state=states, training=training)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py”, line 528, in iter
self._disallow_iteration()
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py”, line 524, in _disallow_iteration
self._disallow_in_graph_mode(“Iterating over a symbolic tf.Tensor”)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py”, line 493, in _disallow_in_graph_mode
raise errors.OperatorNotAllowedInGraphError(
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: Iterating over a symbolic tf.Tensor is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/tmp/ipykernel_14/936901248.py”, line 5, in
model.build(input_shape=(BATCH_SIZE, 100))
File “/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py”, line 523, in build
raise ValueError(
ValueError: You cannot build your model by calling build if your layers do not support float type inputs. Instead, in order to instantiate and build your model, call your model on real tensor data (of the correct dtype).

The actual error from call is: Iterating over a symbolic tf.Tensor is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function

None of the default values were changed.

I think the problem here might be in how you define the self.gru in the innit method!

2 Likes

There have been no changes to the code , whatever was pre programmed, I have tried using the same

It is always a better option to share a screenshot of the error rather than copy paste, is this the complete error log? nithya

@Deepti_Prasad Yes this is the complete error log.

Copying the screenshots of the error logs as well



Thank you @nithyau_ramkumar this does give a better picture at your issue, so your error is stating for your model.build the input used in your assignment code is float dtype data whereas it is looking for tensor tf type data, also make sure if the initial state=state is correct as per instructions provides in the self call gru.

using input_shape=(batch_size, max_length) that’s where this data type error shows. so look if the input was correctly converted into tensor type in any of the previous grade cell.

Regards
DP

Hi @Deepti_Prasad , I am not sure if there needs to be any more changes to this code.
At cell 98, all the inputs are converted into tensor,

Create an embedding layer to map token indices to embedding vectors

    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    # Define a GRU (Gated Recurrent Unit) layer for sequence modeling
    self.gru = tf.keras.layers.GRU(rnn_units)
    # Apply a dense layer with log-softmax activation to predict next tokens
    self.dense = tf.keras.layers.Dense(vocab_size,activation=tf.nn.log_softmax)

I am not sure, if I have missed something here.
def call(self, inputs, states=None, return_state=False, training=False):
x = inputs
# Map input tokens to embedding vectors
x = self.embedding(x, training=training)
if states is None:
# Get initial state from the GRU layer
states = self.gru.get_initial_state(x)
x, states = self.gru(x, initial_state=states, training=training)
# Predict the next tokens and apply log-softmax activation
x = self.dense(x, training=training)
if return_state:
return x, states
else:
return x
When I do a type(x), it says its a tensor.
[<tf.Tensor ‘zeros:0’ shape=(64, 512) dtype=float32>]

Also, I am not able to edit the code in the other cells

@nithyau_ramkumar can I know the model.build where the input is defined is nongraded cell, I mean to ask if the way you recalled inputs with batch_size, max_length was it already given.

Can you share the codes in person DM for model.build

it is probably looking for this to tf.float or ‘utf-8’ type tensorflow data type.

check in the dataset grade cell, if this is the datatype it is looking for!!

Yes , this value is already coded in :slight_smile:
where you call the model:

Length of the vocabulary in StringLookup Layer

vocab_size = 82

The embedding dimension

embedding_dim = 256

RNN layers

rnn_units = 512

model = GRULM(
vocab_size=vocab_size,
embedding_dim=embedding_dim,
rnn_units = rnn_units)

and where the model is built:

testing your model

try:
# Simulate inputs of length 100. This allows to compute the shape of all inputs and outputs of our network
model.build(input_shape=(BATCH_SIZE, 100))
model.call(Input(shape=(100)))
model.summary()
except:
print(“\033[91mError! \033[0mA problem occurred while building your model. This error can occur due to wrong initialization of the return_sequences parameter\n\n”)
traceback.print_exc()

The initialization is already a tf right?

tf.float and utf8 is different type of tensorflow data where as one of float type data, the utf8 is unicode type data.

But according to the latest error you shared

this error means you need to check if initial_state=state seems incorrect as I can see you are suppose to pass it as None

Regards
DP


As per this screenshot when the call method is initiated the states is assigned to None which gets called at a later line as initial_states=states (equals to None).

Not sure if I am missing something. Also about changing the return type to float, it might require me to change the code and use a .to_tensor method, not sure if that would work…

@nithyau_ramkumar your GRU implementation is incomplete.

Consider, in an RNN, what is required to ensure the data ‘flows’ down the chain ?

Thank you @nithyau_ramkumar for the images :slightly_smiling_face:

in the gru layer, instruction mentions return_sequences and return_state need to be set to true which you haven’t, please add these too, so the sequence modelling passed on to the def call

Regards
DP

1 Like

After changing the gru layer and saving the code, the models are building without errors.

1 Like