我正在编写一个使用LSTM的分类问题程序。然而,我不知道如何对LSTM的所有输出计算交叉熵。以下是我程序的一部分。
cell_fw = tf.nn.rnn_cell.LSTMCell(num_hidden)cell_bw = tf.nn.rnn_cell.LSTMCell(num_hidden)outputs, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw,cell_bw,inputs = inputs3, dtype=tf.float32,sequence_length= seq_len)outputs = tf.concat(outputs,axis=2)#outputs [batch_size,max_timestep,num_features]outputs = tf.reshape(outputs, [-1, num_hidden*2])W = tf.Variable(tf.truncated_normal([num_hidden*2, num_classes], stddev=0.1))b = tf.Variable(tf.constant(0., shape=[num_classes]))logits = tf.matmul(outputs, W) + b
我该如何将交叉熵误差应用到这里?我是否应该为每个批次创建一个与最大时间步数相同数量的类别的向量,并用它来计算误差?
回答:
你有查看过cross_entropy
的文档吗:https://www.tensorflow.org/api_docs/python/tf/losses/softmax_cross_entropy?
onehot_labels
的维度应该能回答你的问题。