I am trying to update the below code from TF 1.2 to TF 2.0. When I run the code with all the "old" lines (inditaced by the comment old above them"), I get the following warnings:
WARNING: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.
WARNING: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.
WARNING: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API
So I have done updates as instructed in the warnings (These are the lines with new as comment above them.). However, I am getting the following error:
in setupRNN ((fw, bw), _) = tf.keras.layers.Bidirectional(tf.keras.layers.RNN(stacked))
TypeError: 'Bidirectional' object is not iterable
What is the cause of this error and any tips on how to implement the 'tf.keras.layers.Bidirectional' better in code?
Thank you for your effort and help in advance.
def setupRNN(self):
rnnIn3d = tf.squeeze(self.cnnOut4d, axis=[2])
numHidden = 256
# old:
# cells = [tf.compat.v1.nn.rnn_cell.LSTMCell(num_units=numHidden, state_is_tuple=True) for _ in range(2)] # 2 layers
# new:
cells = [tf.keras.layers.LSTMCell(units=numHidden) for _ in range(2)]
# old:
# stacked = tf.compat.v1.nn.rnn_cell.MultiRNNCell(cells, state_is_tuple=True)
# new:
stacked = tf.keras.layers.StackedRNNCells(cells)
# old:
# ((fw, bw), _) = tf.compat.v1.nn.bidirectional_dynamic_rnn(cell_fw=stacked, cell_bw=stacked, inputs=rnnIn3d, dtype=rnnIn3d.dtype)
# new:
((fw, bw), _) = tf.keras.layers.Bidirectional(tf.keras.layers.RNN(stacked))
concat = tf.expand_dims(tf.concat([fw, bw], 2), 2)
kernel = tf.Variable(tf.random.truncated_normal([1, 1, numHidden * 2, len(self.charList) + 1], stddev=0.1))
self.rnnOut3d = tf.squeeze(tf.nn.atrous_conv2d(value=concat, filters=kernel, rate=1, padding='SAME'), axis=[2])