2016-12-12 1 views
0

Ich möchte Elemente aus 3D-Tensor extrahieren, um 2d-Tensor zu machen. Ich habe 3d-Tensor (2000 X 2 X 3) und Indizes 1d-Tensor für 3-dim (2000). Die Indizes Tensor enthalten sicher Indizes 0 ~ 2 von 2000 Elementen.3d zu 2d Tensor mit tf.gather_nd in Tensorflow

Tatsächlich möchte ich das gleiche Ergebnis mit A[:,:,inds]. Wie geht das mit tf.gather_nd, bitte, hilf mir.

Antwort

0

Wenn der Tensor wirklich von Größe ist 2000 x 2 x 3, können Sie tf.gather:

a = tf.random_normal([2000, 2, 3]) 
b = tf.gather(a, [1007, 8, 7, 9]) 
b.get_shape() 
TensorShape([Dimension(4), Dimension(2), Dimension(3)]) 

Wenn es der Form ist 2 x 3 x 2000, können Sie entweder:

  • Änderung der Form 2000 x 2 x 3 mit tf.reshape und dann tun Sie das gleiche, wie oben
  • Verwenden Sie regelmäßige Python-Mechanismus für die Indizierung und dann pack der Tensor zurück:

    a = tf.random_normal([2, 3, 2000]) 
    indices = [1007, 8, 7, 9] 
    subtensor = [a[:, :, i] for i in indices] 
    b = tf.pack(subtensor, axis=2) 
    b.get_shape() 
    <tf.Tensor 'pack:0' shape=(2, 3, 4) dtype=float32> 
    
+0

danke für Antwort, aber was ich will, ist Elemente in Bezug auf den Indizes von 3d (0 ~ 2) nicht den 1d (0 ~ 2000) zu sammeln. –

+0

Ich verstehe nicht. Können Sie ein Codebeispiel mit einer kleinen Matrix und erwarteten Ergebnissen angeben? – sygi