2017-09-05 6 views
1

의 각 행에 대해 서브 어레이를 얻는 방법 :]TensorFlow : I 코드 한 다음 텐서

[1,2,3], [2,3- 다음으로

import numpy as np 
import tensorflow as tf 

series = tf.placeholder(tf.float32, shape=[None, 5]) 
series_length = tf.placeholder(tf.int32, shape=[None]) 
useful_series = tf.magic_slice_function(series, series_length) 

with tf.Session() as sess: 
    input_x = np.array([[1, 2, 3, 0, 0], 
         [2, 3, 0, 0, 0], 
         [1, 0, 0, 0, 0]]) 
    input_y = np.array([[3], [2], [1]]) 
    print(sess.run(useful_series, feed_dict={series: input_x, series_length: input_y})) 

예상 출력 [1]]

저는 tf.gather, tf.slice 등 여러 가지 기능을 시도했습니다. 그들 모두는 작동하지 않습니다. magic_slice_function은 무엇입니까?

+1

아마, 당신은이 밖에 Tensorflow 할 필요가있다. –

답변

1

그것은 조금 까다로운 :

import numpy as np 
import tensorflow as tf 

series = tf.placeholder(tf.float32, shape=[None, 5]) 
series_length = tf.placeholder(tf.int64) 

def magic_slice_function(input_x, input_y): 
    array = [] 
    for i in range(len(input_x)): 
     temp = [input_x[i][j] for j in range(input_y[i])] 
     array.extend(temp) 
    return [array] 

with tf.Session() as sess: 
    input_x = np.array([[1, 2, 3, 0, 0], 
         [2, 3, 0, 0, 0], 
         [1, 0, 0, 0, 0]]) 

    input_y = np.array([3, 2, 1], dtype=np.int64) 

    merged_series = tf.py_func(magic_slice_function, [series, series_length], tf.float32, name='slice_func') 

    out = tf.split(merged_series, input_y) 
    print(sess.run(out, feed_dict={series: input_x, series_length: input_y})) 

출력 될 것입니다 : 당신이 얻으려는 것은 텐서 아니기 때문에

[array([ 1., 2., 3.], dtype=float32), array([ 2., 3.], dtype=float32), array([ 1.], dtype=float32)]