如何在Tensorflow中有多个Softmax输出?

2024-10-03 04:38:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个具有多个softmax输出的张量流网络,每个输出都有不同的大小。网络架构是: 输入->;LSTM->;退出。然后我有2个softmax层:10个输出的softmax和20个输出的softmax。这样做的原因是,我想生成两组输出(10和20),然后将它们组合起来生成最终输出。我不知道如何在Tensorflow中做到这一点。在

以前,要制作一个如所述的网络,但是有了一个softmax,我想我可以做这样的事情。在

inputs = tf.placeholder(tf.float32, [batch_size, maxlength, vocabsize])
lengths = tf.placeholders(tf.int32, [batch_size])
embeddings = tf.Variable(tf.random_uniform([vocabsize, 256], -1, 1))
lstm = {}
lstm[0] = tf.contrib.rnn.LSTMCell(hidden_layer_size, state_is_tuple=True, initializer=tf.contrib.layers.xavier_initializer(seed=random_seed))
lstm[0] = tf.contrib.rnn.DropoutWrapper(lstm[0], output_keep_prob=0.5)
lstm[0] = tf.contrib.rnn.MultiRNNCell(cells=[lstm[0]] * 1, state_is_tuple=True)
output_layer = {}
output_layer[0] = Layer.W(1 * hidden_layer_size, 20, 'OutputLayer')
output_bias = {}
output_bias[0] = Layer.b(20, 'OutputBias')
outputs = {}
fstate = {}
with tf.variable_scope("lstm0"):
    # create the rnn graph at run time
  outputs[0], fstate[0] = tf.nn.dynamic_rnn(lstm[0], tf.nn.embedding_lookup(embeddings, inputs),
                                      sequence_length=lengths, 
                                      dtype=tf.float32)
logits = {}
logits[0] = tf.matmul(tf.concat([f.h for f in fstate[0]], 1), output_layer[0]) + output_bias[0]
loss = {}
loss[0] = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits[0], labels=labels[0]))

但是,现在,我希望我的RNN输出(在丢失之后)流入2个softmax层,一个大小为10,另一个大小为20。有人知道怎么做吗?在

谢谢

编辑:理想情况下,我想使用softmax的一个版本,比如在这个Knet Julia库中定义的。Tensorflow有等价物吗? https://github.com/denizyuret/Knet.jl/blob/1ef934cc58f9671f2d85063f88a3d6959a49d088/deprecated/src7/op/actf.jl#L103


Tags: gt网络layeroutputsizetftensorflownn
2条回答

您可以对您调用的dynamic_rnn的输出执行以下操作,以便计算两个softmax和相应的损耗:

with tf.variable_scope("softmax_0"):
    # Transform you RNN output to the right output size = 10
    W = tf.get_variable("kernel_0", [output[0].get_shape()[1], 10])
    logits_0 = tf.matmul(inputs, W)
    # Apply the softmax function to the logits (of size 10)
    output_0 = tf.nn.softmax(logits_0, name = "softmax_0")
    # Compute the loss (as you did in your question) with softmax_cross_entropy_with_logits directly applied on logits
    loss_0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_0, labels=labels[0]))

with tf.variable_scope("softmax_1"):  
    # Transform you RNN output to the right output size = 20
    W = tf.get_variable("kernel_1", [output[0].get_shape()[1], 20])
    logits_1 = tf.matmul(inputs, W)
    # Apply the softmax function to the logits (of size 20)
    output_1 = tf.nn.softmax(logits_1, name = "softmax_1")
    # Compute the loss (as you did in your question) with softmax_cross_entropy_with_logits directly applied on logits
    loss_1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_1, labels=labels[1]))

如果与您的申请相关,您可以将这两种损失合并:

^{pr2}$

编辑 要在评论中回答您的问题,您需要如何处理这两个softmax输出:您可以大致执行以下操作:

with tf.variable_scope("second_part"):
    W1 = tf.get_variable("W_1", [output_1.get_shape()[1], n])
    W2 = tf.get_variable("W_2", [output_2.get_shape()[1], n])
    prediction = tf.matmul(output_1, W1) + tf.matmul(output_2, W2)
with tf.variable_scope("optimization_part"):
    loss = tf.reduce_mean(tf.squared_difference(prediction, label))

您只需要定义n,W1和W2的列数。在

您没有在代码中为大小为10的softmax层定义登录名,您必须显式地这样做。在

完成后,可以使用tf.nn.softmax,将其分别应用于两个logit张量。在

例如,对于您的20类softmax张量:

softmax20 = tf.nn.softmax(logits[0])

对于另一层,可以执行以下操作:

^{pr2}$

还有一个tf.contrib.layers.softmax,它允许你在大于2维的张量的最后一个轴上应用softmax,但是看起来你不需要这样的东西。tf.nn.softmax应该在这里工作。在

旁注:output_layer不是该列表的最佳名称-应该是一些涉及权重的名称。这些权重和偏差(output_layeroutput_bias)也不代表您网络的输出层(因为无论您对softmax输出做什么,都会产生这种影响,对吗?)。[抱歉,我忍不住了。]

相关问题 更多 >