我正在尝试创建一个具有多个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
您可以对您调用的
dynamic_rnn
的输出执行以下操作,以便计算两个softmax和相应的损耗:如果与您的申请相关,您可以将这两种损失合并:
^{pr2}$编辑 要在评论中回答您的问题,您需要如何处理这两个softmax输出:您可以大致执行以下操作:
您只需要定义
n
,W1和W2的列数。在您没有在代码中为大小为10的softmax层定义登录名,您必须显式地这样做。在
完成后,可以使用tf.nn.softmax,将其分别应用于两个logit张量。在
例如,对于您的20类softmax张量:
对于另一层,可以执行以下操作:
^{pr2}$还有一个tf.contrib.layers.softmax,它允许你在大于2维的张量的最后一个轴上应用softmax,但是看起来你不需要这样的东西。tf.nn.softmax应该在这里工作。在
旁注:
output_layer
不是该列表的最佳名称-应该是一些涉及权重的名称。这些权重和偏差(output_layer
,output_bias
)也不代表您网络的输出层(因为无论您对softmax输出做什么,都会产生这种影响,对吗?)。[抱歉,我忍不住了。]相关问题 更多 >
编程相关推荐