2016-10-23 6 views
0

저는 현재 중재적인 수의 샘플을 가져 와서 tensorflow를 사용하여 14 길이의 특징 벡터를 출력 할 수있는 회귀 목적으로 RNN 네트워크를 만들려고합니다.왜 출력이 그렇게 높은 차원입니까?

def length(sequence): ##Zero padding to fit the max lenght... Question whether that is a good idea. 
    used = tf.sign(tf.reduce_max(tf.abs(sequence), reduction_indices=2)) 
    length = tf.reduce_sum(used, reduction_indices=1) 
    length = tf.cast(length, tf.int32) 
    return length 

def cost(output, target): 
    # Compute cross entropy for each frame. 
    print output 
    cross_entropy = target * tf.log(output) 
    print "Hello world" 
    cross_entropy = -tf.reduce_sum(cross_entropy, reduction_indices=2) 
    mask = tf.sign(tf.reduce_max(tf.abs(target), reduction_indices=2)) 
    cross_entropy *= mask 
    # Average over actual sequence lengths. 
    cross_entropy = tf.reduce_sum(cross_entropy, reduction_indices=1) 
    cross_entropy /= tf.reduce_sum(mask, reduction_indices=1) 
    return tf.reduce_mean(cross_entropy) 

def last_relevant(output): 
    max_length = int(output.get_shape()[1]) 
    relevant = tf.reduce_sum(tf.mul(output, tf.expand_dims(tf.one_hot(length(output), max_length), -1)), 1) 
    return relevant 

files_train_path = [dnn_train+f for f in listdir(dnn_train) if isfile(join(dnn_train, f))] 
files_test_path = [dnn_test+f for f in listdir(dnn_test) if isfile(join(dnn_test, f))] 

files_train_name = [f for f in listdir(dnn_train) if isfile(join(dnn_train, f))] 
files_test_name = [f for f in listdir(dnn_test) if isfile(join(dnn_test, f))] 

os.chdir(dnn_train) 

train_name,train_data = generate_list_of_names_data(files_train_path) 
train_data, train_names, train_output_data, train_class_output = load_sound_files(files_train_path,train_name,train_data) 

max_length = 0 ## Used for variable sequence input 

for element in train_data: 
    if element.size > max_length: 
     max_length = element.size 

NUM_EXAMPLES = len(train_data)/2 

test_data = train_data[NUM_EXAMPLES:] 
test_output = train_output_data[NUM_EXAMPLES:] 

train_data = train_data[:NUM_EXAMPLES] 
train_output = train_output_data[:NUM_EXAMPLES] 
print("--- %s seconds ---" % (time.time() - start_time)) 

#----------------------------------------------------------------------# 
#----------------------------Main--------------------------------------# 
### Tensorflow neural network setup 

batch_size = None 
sequence_length_max = max_length 
input_dimension=1 

data = tf.placeholder(tf.float32,[batch_size,sequence_length_max,input_dimension]) 
target = tf.placeholder(tf.float32,[None,14]) 

num_hidden = 24 ## Hidden layer 
cell = tf.nn.rnn_cell.LSTMCell(num_hidden,state_is_tuple=True) ## Long short term memory 

output, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32,sequence_length = length(data)) ## Creates the Rnn skeleton 

last = last_relevant(output)#tf.gather(val, int(val.get_shape()[0]) - 1) ## Appedning as last 

weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])])) 
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]])) 

prediction = tf.nn.softmax(tf.matmul(last, weight) + bias) 

cross_entropy = cost(output,target)# How far am I from correct value? 

optimizer = tf.train.AdamOptimizer() ## TensorflowOptimizer 
minimize = optimizer.minimize(cross_entropy) 

mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1)) 
error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) 

## Training ## 

init_op = tf.initialize_all_variables() 
sess = tf.Session() 
sess.run(init_op) 

batch_size = 1000 
no_of_batches = int(len(train_data)/batch_size) 
epoch = 5000 
for i in range(epoch): 
    ptr = 0 
    for j in range(no_of_batches): 
     inp, out = train_data[ptr:ptr+batch_size], train_output[ptr:ptr+batch_size] 
     ptr+=batch_size 
     sess.run(minimize,{data: inp, target: out}) 
    print "Epoch - ",str(i) 
incorrect = sess.run(error,{data: test_data, target: test_output}) 
print('Epoch {:2d} error {:3.1f}%'.format(i + 1, 100 * incorrect)) 
sess.close() 

코드가 완전히 실행되지 않습니다 인해 오류로 :

네트워크는 .. 여기있는 내가 문제를 디버깅하기 위해 노력하고, 순간에 제대로 실행되지 않는 코드입니다 cross_entropy 기능.

Tensor("RNN/transpose:0", shape=(?, 138915, 24), dtype=float32) 
Traceback (most recent call last): 
    File "tensorflow_test.py", line 186, in <module> 
    cross_entropy = cost(output,target)# How far am I from correct value? 
    File "tensorflow_test.py", line 122, in cost 
    cross_entropy = target * tf.log(output) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 754, in binary_op_wrapper 
    return func(x, y, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 903, in _mul_dispatch 
    return gen_math_ops.mul(x, y, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1427, in mul 
    result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op 
    op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2312, in create_op 
    set_shapes_for_outputs(ret) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1704, in set_shapes_for_outputs 
    shapes = shape_func(op) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1801, in _BroadcastShape 
    % (shape_x, shape_y)) 
ValueError: Incompatible shapes for broadcasting: (?, 14) and (?, 138915, 24) 

RNN에서받는 출력은 매우 높은 차원을가집니다. 나는 단지 14 개의 요소가있는 벡터를 기대 했으므로 1 차원 벡터가됩니다. 그러나 어쨌든 나는 꽤 큰 차원으로 끝나고 있는가? 왜? 내 신경망의 설정에서 뭔가 잘못되었다고 생각합니다.

답변

0

dynamic_rnn의 출력은 [batch_size, num_steps, dim_hidden]입니다. 귀하의 경우 RNN의 타임 스텝 수는 138915입니다.

+0

왜? ... 138915는 최대 시퀀스 길이입니다. 24는 숨겨진 레이어의 수입니까? 그들이 어떻게 여기에서 발생합니까? –

+0

tf.nn.dynamic_rnn은 쉐이프 배치의 랭크 3의 텐서를 반환합니다. x n_steps x dim_hidden. 따라서 n 개의 입력을 지정하면 각 단계의 상태가 출력으로 표시됩니다. 귀하의 경우 n = 138915입니다. –

+0

제 경우에는 입력 수를 정의하는 것이 불가능합니다 ... 가변 길이를 사용할 수 있습니까? 나는 이것을 사용하여 무언가를 사용했다 : https://danijar.com/variable-sequence-lengths-in-tensorflow/ 그러나 그것은 작동하지 않는다 .. 이것이 가능하지 않아야한다, 나는 그때 일반적인 신경을 의미한다. 네트워크 감각? 이것은 tensorflow에 의한 제한입니까? –