# Fix tf.nn.dynamic_rnn() ValueError: If there is no initial_state, you must give a dtype.

By | August 5, 2020

TensorFlow tf.nn.dynamic_rnn() function allows us to create a dynamic rnn or lstm model. However, you may get a ValueError: If there is no initial_state, you must give a dtype. In this tutorial, we will introduce how to fix this error.

If you want to know how to use tf.nn.dynamic_rnn() in your model, you can read this tutorial:

Understand tf.nn.dynamic_rnn() for TensorFlow Beginners

How to fix this error?

Look at example code below:

cell = tf.contrib.rnn.BasicLSTMCell(num_units=2*hidden_dim, state_is_tuple=True)

outputx, hs = tf.nn.dynamic_rnn(cell, inputs=inputx)

In this code, we do not set an initial_state for tf.nn.dynamic_rnn(). Run this code, you will get a value error.

This ValueError likes:

If you do not set an initial_state for tf.nn.dynamic_rnn(), you must set the dtype for it.

We can modify this code like below:

outputx, hs = tf.nn.dynamic_rnn(cell, inputs=inputx, dtype=tf.float32)

Then you will find this value error is fixed.