tensorflow sequence_loss_by_example weight
<p>I want to make weight tensors for tf.nn.seq2seq.sequence_loss_by_example.
I'm using RNN-LSTM with maximum 100 steps, and zero-padded each batch items by maximum steps (100).</p>

<p>Shapes of my logits and labels are like this.</p>

<pre><code>Tensor("dropout/mul_1:0", shape=(50000, 168), dtype=float32) # logits
Tensor("ArgMax:0", shape=(500, 100), dtype=int64)            # labels
</code></pre>

<p>50000 is for 500(batch_size) * 100(num_steps), and 168 is number of classes, and I'm passing them to sequence_loss_by_example like the ptb_word_lm.py code provided by Tensorflow. <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/rnn/ptb/ptb_word_lm.py" rel="nofollow">https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/rnn/ptb/ptb_word_lm.py</a></p>

<pre><code>loss = tf.nn.seq2seq.sequence_loss_by_example(
            [logits],
            [tf.reshape(labels, [-1])],
            [tf.ones([cf.batch_size * cf.max_time_steps], dtype=tf.float32)])
</code></pre>

<p>However, because my logits and labels are zero-padded, the losses are incorrect. From this answer, <a href="http://stackoverflow.com/a/38502547/3974129">http://stackoverflow.com/a/38502547/3974129</a>, I tried to change tf.ones([..]) part to weight tensors, but their underlying conditions are too different from mine.</p>

<p>I have the step length information like below, and I feed them when training.</p>

<pre><code>self._x_len = tf.placeholder(tf.int64, shape=[self._batch_size])
</code></pre>

<p>For example,  I feed length information [3, 10, 2, 3, 1] for batch of size 5. They are also used for sequence_length in tf.nn.rnn().</p>

<p>One way I can think of is iterating the x_len, and use each item as index of last 1 in each weight. </p>

<blockquote>
  <p>[0 0 0 0 0 .... 0 0 0]  => [1 1 1 ... 1 0 0 0 0]</p>
  
  <p>weight tensor with size of 100 (maximum time step)</p>
</blockquote>

<p>But as you know, I cannot use the values inside the tensor for index, because they are not fed yet.</p>

<p>How can I make weight tensors like this?</p>

<p>Using <a href="http://stackoverflow.com/questions/34128104/tensorflow-creating-mask-of-varied-lengths">tensorflow creating mask of varied lengths</a> and <a href="https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/dynamic_rnn.py" rel="nofollow">https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/dynamic_rnn.py</a>, problem solved. I can make indexes of each max steps, and masked them</p>

python numpy tensorflow