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

hoc*_*bro 8 python deep-learning tensorflow

我正在尝试使用多个softmax输出创建一个张量流网络,每个输出都有不同的大小.网络架构是:输入 - > LSTM - > Dropout.然后我有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]))
Run Code Online (Sandbox Code Playgroud)

但是,现在,我希望我的RNN输出(在丢失之后)流入2个softmax层,一个大小为10,另一个大小为20.有没有人知道如何做到这一点?

谢谢

编辑:理想情况下,我想使用softmax版本,例如此Knet Julia库中定义的内容.Tensorflow有同等效力吗? https://github.com/denizyuret/Knet.jl/blob/1ef934cc58f9671f2d85063f88a3d6959a49d088/deprecated/src7/op/actf.jl#L103

Nee*_*yap 5

您没有在代码中定义10号softmax图层的logits,您必须明确地执行此操作.

完成后,您可以使用tf.nn.softmax,将其单独应用于两个logit张量.

例如,对于你的20级softmax张量:

softmax20 = tf.nn.softmax(logits[0])
Run Code Online (Sandbox Code Playgroud)

对于其他图层,您可以执行以下操作:

output_layer[1] = Layer.W(1 * hidden_layer_size, 10, 'OutputLayer10')
output_bias[1] = Layer.b(10, 'OutputBias10')

logits[1] = tf.matmul(tf.concat([f.h for f in fstate[0]], 1), 
output_layer[1]) + output_bias[1]

softmax10 = tf.nn.softmax(logits[1])
Run Code Online (Sandbox Code Playgroud)

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

旁注: output_layer不是该列表的最大名称 - 应该是涉及权重的东西.这些权重和偏差(output_layer,output_bias)也不代表网络的输出层(因为这将来自你对softmax输出做的任何事情,对吧?).[抱歉,忍不住了.]


Pop*_*Pop 4

dynamic_rnn您可以对调用的输出执行以下操作output[0],以计算两个 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]))
Run Code Online (Sandbox Code Playgroud)

如果这与您的应用相关,您可以将这两个损失合并起来:

total_loss = loss_0 + loss_1
Run Code Online (Sandbox Code Playgroud)

编辑 要回答您在评论中关于您具体需要对两个 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))
Run Code Online (Sandbox Code Playgroud)

您只需定义nW1 和 W2 的列数。