<jupyter_start><jupyter_text>Create a basic RNN<jupyter_code>import tensorflow as tf
import numpy as np
tf.reset_default_graph()
sess = tf.InteractiveSession()
num_steps = 1
num_lst_units = 5
x_dim = 2
y_dim = 2
learning_rate = 1e-3
cell = tf.nn.rnn_cell.LSTMCell(num_lst_units, state_is_tuple=True)
cell_input = tf.placeholder(
    tf.float32,
    [None, x_dim]
)
cell_state = (tf.placeholder(tf.float32, [None, num_lst_units]),
              tf.placeholder(tf.float32, [None, num_lst_units]))
with tf.variable_scope("foo") as my_scope:
    cell_output = cell(cell_input, cell_state)
rnn_inputs_ph = tf.placeholder(
    tf.float32,
    [None, num_steps, x_dim],
    name='input_placeholder',
)

rnn_inputs = tf.unpack(rnn_inputs_ph, axis=1)

my_scope.reuse_variables()
with tf.variable_scope(my_scope):
    rnn_outputs, final_state = tf.nn.rnn(
        cell,
        rnn_inputs,
        initial_state=cell_state,
        dtype=tf.float32,
        scope=my_scope,
    )

# labels_ph = tf.placeholder(
#     tf.float32,
#     [None, num_steps, y_dim],
#     name='labels_placeholder',
# )
# labels = tf.unpack(labels_ph, axis=1)
# 
# with tf.variable_scope('softmax'):
#     W = tf.get_variable('W', [num_lst_units, y_dim])
#     b = tf.get_variable('b', [y_dim],
#                         initializer=tf.constant_initializer(0.0))
# logits = [tf.matmul(rnn_output, W) + b for rnn_output in rnn_outputs]
# predictions = [tf.nn.softmax(logit) for logit in logits]
# 
# total_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits, labels)
# train_step = tf.train.AdamOptimizer(
#     learning_rate).minimize(
#     total_loss)
sess.run(tf.global_variables_initializer())
batch_size = 4

rnn_input_values = np.random.rand(batch_size, num_steps, x_dim)
input_values = rnn_input_values[:, 0, :]
state_values = (np.zeros((batch_size, num_lst_units)),
                np.zeros((batch_size, num_lst_units)))
print(cell_input, input_values)
print(cell_state, state_values)
print(rnn_inputs, rnn_input_values)
print(cell_output)
print(rnn_outputs)
cell_output_value = sess.run(
    cell_output,
    {
        cell_input: input_values,
        cell_state: state_values,
    }
)
rnn_output_values, final_state = sess.run(
    [rnn_outputs, final_state],
    {
        rnn_inputs_ph: rnn_input_values,
        cell_state: state_values,
    }
)<jupyter_output><empty_output><jupyter_text><jupyter_code>print(cell_output_value[0])
print(rnn_output_values)
print(cell_output_value[1])
print(final_state)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12241653418124006, 'fraction_numerical': 0.010333863275039745, 'mean_word_length': 4.378205128205129, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '369872', 'license': 'MIT', 'n_tokens_mistral': 981, 'n_tokens_neox': 962, 'n_words': 151, 'path': 'notebooks/rnns.ipynb', 'repo_name': 'jcoreyes/erl', 'size': '2516'}