Tensorflow:从Tensor中选择每行中的一系列列

时间:2017-09-27 12:14:54

标签: tensorflow rnn

我想在张量的每一行中只选择特定的列,将其用于RNN

seq_len=[11,12,20,30] #This is the sequence length, assume 4 sequences
array=tf.ones([4,30]) #Assuming this is the array I want to index from

function(array,seq_len) #apply required function

Output=(first 11 elements from row 0, first 12 from row 2, first 20 from row 3 etc), perhaps obtained as a flat tensor

2 个答案:

答案 0 :(得分:1)

您可以使用tf.sequence_masktf.boolean_mask将其展平:

<body class="preload">
    <div class="popup">
        <h1 class="heartbeat">Lorem.</h1>
        <form>
            <input style="display:none;" type="text" placeholder="Prezývka" id="nick">
            <input type="text" placeholder="Email" id="email">
            <input type="password" placeholder="Heslo" id="password">
            <input style="display: none;" type="password" placeholder="Heslo znova" id="password-repeat">
            <button type="submit">Prihlásiť sa</button>
            <small><a href="#" id="register-link">alebo sa zaregistruj</a></small>
        </form>
    </div>
</body>

答案 1 :(得分:1)

张量流中的张量可以像numpy数组一样被切片,然后连接成一个张量。假设您从第一个元素测量序列长度。

使用[row_idx,column_idx]对张量进行切片。 slice = array[0,:]会将第一行分配给切片。

flat_slices = tf.concat([slice,slice])会将它们压缩成一个张量。

import tensorflow as tf

seq_len = [11,12,20,30]
array = tf.ones([4,30])

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()

    flatten = array[0,:seq_len[0]]

    for i in range(1,len(seq_len)):
        row = array[i,:seq_len[i]]
        flatten = tf.concat([flatten, row])

    print(sess.run(flatten))