Tensorflow RNN用于单输出分类

时间:2018-05-09 15:19:43

标签: python tensorflow rnn

我想在Tensorflow中创建一个RNN,用于对基于每个字母的短文本进行分类。为此,我创建了一个numpy 2D数组,其中每个文本都是填充或截断的,其中每个元素都是一个字符代码。输出只是clasess的向量,表示为单热编码的numpy 2D数组。

以下是一个例子:

train_x.shape, train_y.shape
  

((91845,50),(91845,5))

输入由90K行各50个字符组成,输出为90K行,5个类。接下来,我想建立一个如下图所示的网络。

Network

结构看起来微不足道,但我在Tensorflow中缺乏知识,并且在尝试至少进行培训时会遇到各种各样的问题。这是我用来构建网络的代码部分

chars = sequence_categorical_column_with_identity('chars', params['domain_size']+1)
chars_emb = tf.feature_column.embedding_column(chars, dimension=10)
columns = [chars_emb]

input_layer, sequence_length = sequence_input_layer(features, columns)

hidden_units = 32
lstm = tf.nn.rnn_cell.LSTMCell(hidden_units, state_is_tuple=True)
rnn_outputs, state = tf.nn.dynamic_rnn(lstm, 
                                      inputs = input_layer,
                                      sequence_length=sequence_length,
                                      dtype=tf.float32)

output = rnn_outputs[:,-1,:]
logits = tf.layers.dense(output, params['n_classes'], activation=tf.nn.tanh)
# apply projection to every timestep.
# Compute predictions.
predicted_classes = tf.nn.softmax(logits)

# Compute loss.

loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=labels, logits=logits)
# Compute evaluation metrics.
accuracy = tf.metrics.accuracy(labels=labels,
                               predictions=predicted_classes,
                               name='acc_op')

但是我收到了错误

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 8 values, but the requested shape has 1
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](softmax_cross_entropy_with_logits, sequence_input_layer/chars_embedding/assert_equal/Const)]]

您可以找到here这个更全面的小例子。很可能你需要Tensorflow 1.8.0。

1 个答案:

答案 0 :(得分:0)

添加

loss = tf.reduce_mean(loss)

现在允许训练网络,但结果并不令人满意。