2017-10-22 1 views
0

두 개의 컨벌루션 레이어 뒤에 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 변수로 시간 단계를 표시 할 필요가 바로 여기 처음 컨볼 루션 후 내 코드는? 방법? 나는 그것을하기 위해 호를 정말로 모른다.

편집 :이 모양 변경 후
: InvalidArgumentError (역 추적에 대한 위 참조) : logits 및 레이블이 같은 크기이어야합니다

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

나는이 오류가 발생했습니다

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

logits_size = [5000,7] labels_size = [50,7]이 줄에 : cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=outputs)) 마지막으로 모양을 변경 한 후에 배치 크기가 변경된 것 같습니다.

편집 2 :
아래 코드가 잘못 되었습니까? 그렇지 않으면 내가 부동 INPUT_SIZE을해야하고 TF는 오류가 발생하기 때문에

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]) 

나는 N_TIME_STEPS 그런 식으로 설정합니다.

답변

3

Tensorflow 문서 (https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn)에 따르면

입력은 다음과 같은 형태 (I 여기서 기본값을 사용)에 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]. 따라서 모든 시간 단계마다 출력이 있습니다. 위 코드에서 마지막 단계의 출력 만 얻습니다. 또는 여기에 설명 된 rnn 출력의 다른 아키텍처 (How do we use LSTM to classify sequences?),

이 도움이 될 것입니다.

+0

고맙습니다. 문제는 : 입력 크기가 64 * 50 * 25 (내 코드에서)가 아닌가? – Yes92

+0

64 * 50 * 25 = N_TIME_STEPS * INPUT_SIZE 따라서 두 매개 변수를 정의해야합니다. –

+0

고마워, 이제 또 다른 문제가 생겼어. 나는 그 모양이 바뀌 었다고 생각해. 나는 그 질문을 편집했다. 도와 줄수있으세요? – Yes92