2017-10-22 4 views
0

2つの畳み込みレイヤの後にLSTMレイヤを実装する必要があります。ここに私のコードは最初のコンボリューション後です:コンボリューション後のLSTMセル

convo_2 = convolutional_layer(convo_1_pooling, shape=[5, 5, 32, 64]) 
convo_2_pooling = max_pool_2by2(convo_2) 
convo_2_flat = tf.reshape(convo_2_pooling, shape=[-1, 64 * 50 * 25]) 
cell = rnn.LSTMCell(num_units=100, activation=tf.nn.relu) 
cell = rnn.OutputProjectionWrapper(cell, output_size=7) 
conv_to_rnn = int(convo_2_flat.get_shape()[1]) 
outputs, states = tf.nn.dynamic_rnn(cell, convo_2_flat, dtype=tf.float32) 

私は最後の行に、このエラーを取得する:

ValueError: Shape (?, 50, 64) must have rank 2 

私は右、convo_2_flat変数に時間ステップを示す必要がありますか?どうやって?私は本当にそれを行うためにホーを知らない。

EDIT:このリシェイプ後
:InvalidArgumentError(トレースバックについては上記参照):logitsとラベルは同じサイズでなければなりません:logits_size =

convo_2_flat = tf.reshape(convo_2_flat, shape=[-1, N_TIME_STEPS, INPUT_SIZE]) 

N_TIME_STEPS = 25 
INPUT_SIZE = int(64 * 50 * 25/N_TIME_STEPS) 

私はこのエラーを得ました[5000,7] labels_size = [50,7] on this lines: cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=outputs)) 最後の形状変更後にバッチサイズが変更されているようです。

編集2:
以下のコードは間違っていますか?

convo_2_shape = convo_2_pooling.get_shape().as_list() 
shape_convo_flat = convo_2_shape[1] * convo_2_shape[2] * convo_2_shape[3] 
N_TIME_STEPS = convo_2_shape[1] 
INPUT_SIZE = tf.cast(shape_convo_flat/N_TIME_STEPS, tf.int32) 
convo_2_out = tf.reshape(convo_2_pooling, shape=[-1, shape_convo_flat]) 
convo_2_out = tf.reshape(convo_2_out, shape=[-1, N_TIME_STEPS, INPUT_SIZE]) 

そう私はフロートINPUT_SIZEとTFは、エラーがスローされますがありますので、私はN_TIME_STEPSそのように設定します。

答えて

3

Tensorflowドキュメント(https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn)によれば

の入力は、以下の形状(私はここでデフォルトを使用)で enter image description here

、すなわち、[BATCH_SIZE、N_TIME_STEPS、INPUT_SIZE]であるべきです。次のようにそのため、あなたは出力に、 convo_2_flat

#get the shape of the output of max pooling 
shape = convo_2_pooling.get_shape().as_list() 
#flat accordingly 
convo_2_flat = tf.reshape(convo_2_pooling, [-1, shape[1] * shape[2] * shape[3]]) 

# Here shape[1] * shape[2] * shape[3]] = N_TIME_STEPS*INPUT_SIZE 

#reshape according to dynamic_rnn input 
convo_2_flat = tf.reshape(convo_2_flat, shape=[-1, N_TIME_STEPS, INPUT_SIZE]) 

outputs, states = tf.nn.dynamic_rnn(cell, convo_2_flat, dtype=tf.float32) 

# get the output of the last time step 
val = tf.transpose(outputs, [1, 0, 2]) 
lstm_last_output = val[-1] 

OUTPUT_SIZE = 7 #since you have defined in cell = rnn.OutputProjectionWrapper(cell, output_size=7) 

W = { 
     'output': tf.Variable(tf.random_normal([OUTPUT_SIZE, N_CLASSES])) 
    } 
biases = { 
     'output': tf.Variable(tf.random_normal([N_CLASSES])) 
    } 

#Dense Layer 
pred_Y= tf.matmul(lstm_last_output, W['output']) + biases['output'] 
#Softmax Layer 
pred_softmax = tf.nn.softmax(pred_Y) 

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=pred_softmax)) 

注意をの形状を変更することができます:ドキュメントによると

、dynamic_rnnの出力は

enter image description here

、以下の通りです。

、つまり[BATCH_SIZE、N_TIME_STEPS、OUTPUT _SIZE]。したがって、時間ステップごとに出力があります。上記のコードでは、最後のタイムステップの出力しか得られません。あるいは、ここで(How do we use LSTM to classify sequences?)と記述されているrnn出力の別のアーキテクチャについて考えることができます。

希望します。

+0

ありがとうございます。問題は:入力サイズではない64 * 50 * 25(私のコードから)? – Yes92

+0

64 * 50 * 25 = N_TIME_STEPS * INPUT_SIZEしたがって、これらの2つのパラメータを定義する必要があります。 –

+0

ありがとう、今私は別の問題を抱えていると私は考えていると思います。私はその質問を編集した。私を助けてくれますか? – Yes92