# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6349695,"math_prob":0.5904465,"size":950,"snap":"2022-27-2022-33","text_gpt3_token_len":228,"char_repetition_ratio":0.17124736,"word_repetition_ratio":0.028985508,"special_character_ratio":0.23894736,"punctuation_ratio":0.20952381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9938957,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-12T09:23:11Z\",\"WARC-Record-ID\":\"<urn:uuid:0883761c-22b0-43aa-82a9-af41625900d1>\",\"Content-Length\":\"44783\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5d09364-a843-4a1a-882b-85c75b302042>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4b541cd-a639-4c09-a52a-4971f0369e0f>\",\"WARC-IP-Address\":\"66.45.254.227\",\"WARC-Target-URI\":\"https://www.tutorialexample.com/fix-tf-nn-dynamic_rnn-valueerror-if-there-is-no-initial_state-you-must-give-a-dtype/\",\"WARC-Payload-Digest\":\"sha1:KP7BBKSKT4B4J7P7C5WRBQCXVGYRGJUF\",\"WARC-Block-Digest\":\"sha1:KLG3JJUAFJVNNQ774D7UXQVK75QPIRO7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571597.73_warc_CC-MAIN-20220812075544-20220812105544-00450.warc.gz\"}"}