import tensorflow as tf

#def rnn_cell(rnn_input, state):
    #with tf.variable_scope('rnn_cell', reuse=True):
        #W = tf.get_variable('W', [num_classes+state_size, state_size])
        #b = tf.get_variable('b', [state_size], initializer=tf.constant_initializer(0.0))
    #return tf.tanh(tf.matmul(tf.concat([rnn_input, state],1),W) + b)
#'''将rnn cell添加到计算图中'''
#state = init_state
#rnn_outputs = []
#for rnn_input in rnn_inputs:
    #state = rnn_cell(rnn_input, state)  # state会重复使用，循环
    #rnn_outputs.append(state)
#final_state = rnn_outputs[-1]        # 得到最后的state

cell = tf.contrib.rnn.BasicRNNCell(num_units=state_size)
rnn_outputs, final_state = tf.nn.dynamic_rnn(cell, rnn_inputs, initial_state=init_state)

'''预测，损失，优化'''
with tf.variable_scope('softmax'):
    W = tf.get_variable('W', [state_size, num_classes])
    b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0))
    '''因为rnn_outputs是三维的，这里需要将其转成2维的，
       矩阵运算后再转换回来[batch_size, num_steps, num_classes]'''
logits = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) +b, \
                    shape=[batch_size, num_steps, num_classes])
predictions = tf.nn.softmax(logits)

