2017-09-08 6 views
-1

私は言語モデルのRNNを勉強していて、Tensorflowでそのコードを本に見つけました。 しかし、私は実際にx[:t:]が以下のコードで何をするのか分かりません..... 私は機械学習の初心者です。もし誰かが知っていれば、私に手がかりを与えてください。Pythonでx [:t:]は何をしますか? <RNN>

=======コード========

def inference(x, n_batch, maxlen=None, n_hidden=None, n_out=None): 
    def weight_variable(shape): 
     initial = tf.truncated_normal(shape, stddev=0.01) 
     return tf.Variable(initial) 

    def bias_variable(shape): 
     initial = tf.zeros(shape, dtype=tf.float32) 
     return tf.Variable(initial) 

    cell = tf.contrib.rnn.BasicRNNCell(n_hidden) 
    initial_state = cell.zero_state(n_batch, tf.float32) 

    state = initial_state 
    outputs = [] 
    with tf.variable_scope('RNN'): 
     for t in range(maxlen): 
      if t > 0: 
       tf.get_variable_scope().reuse_variables() 
      (cell_output, state) = cell(x[:, t, :], state) 
      outputs.append(cell_output) 

    output = outputs[-1] 

    V = weight_variable([n_hidden, n_out]) 
    c = bias_variable([n_out]) 
    y = tf.matmul(output, V) + c 

    return y 
+5

x [:t:]またはx [:, t、:]? – ingvar

+3

これはあなたが提供しなかった 'x'のタイプに完全に依存します。 – timgeb

+0

@timgeb - タグに基づいて、テンソルフローテンソル(これは数が少ない配列のように多く動作する)がほぼ確実であると確信しています。 – mgilson

答えて

2

xが3Dマトリックスであるように見えます。その場合、[:,t,:]は、X-Z平面内でキューブの第1スライスとして2-Dマトリックスを抽出します。

>>> import numpy as np 
>>> x = np.arange(27).reshape(3,3,3) 
>>> x 
array([[[ 0, 1, 2], 
     [ 3, 4, 5], 
     [ 6, 7, 8]], 

     [[ 9, 10, 11], 
     [12, 13, 14], 
     [15, 16, 17]], 

     [[18, 19, 20], 
     [21, 22, 23], 
     [24, 25, 26]]]) 
>>> x[:,1,:] 
array([[ 3, 4, 5], 
     [12, 13, 14], 
     [21, 22, 23]]) 

:は、軸が変更されていないことを示します。 [:,:,:][1,:,:]が第一の軸に沿って第2のスライスを抽出することになる、全体のマトリックスを返す:

>>> x[1,:,:] 
array([[ 9, 10, 11], 
     [12, 13, 14], 
     [15, 16, 17]]) 

ここ対応documentationです。

関連する問題