2016-10-22 142 views
0

我目前正试图训练这个RNN网络,但似乎遇到了奇怪的错误,我无法解码。Tensorflow RNN培训不会执行?

我的网络输入是数字采样音频文件。由于音频文件的长度可能不同,采样音频的矢量也会有不同的长度。

神经网络的输出或目标是重新创建一个14维向量,其中包含音频文件的某些信息。我已经知道目标,通过手动计算,但需要使它与神经网络一起工作。

我目前使用tensorflow作为框架。

我的网络设置是这样的:

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, max_length), -1)), 1) 
    return relevant 

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. 
    cross_entropy = target * tf.log(output) 
    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) 
#----------------------------------------------------------------------# 
#----------------------------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() 

的错误似乎是功能last_relevant,即应采取的输出,并反馈的使用。

这是错误消息:

TypeError: Expected binary or unicode string, got <function length at 0x7f846594dde8> 

反正告诉这可能是错在这里?

+0

您定义长度的函数。然后你将它传递给tf.one_hot。你故意这么做吗? –

+0

是...掩盖不相关的相关部分。长度给我的长度,并且max_length具有全长 –

回答

1

我试图在我的本地生成你的代码。 有一个在它是你叫tf.one_hot代码的根本错误,但你传真的不适合与预期是什么:

阅读文档在这里: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/api_docs/python/functions_and_classes/shard6/tf.one_hot.md

tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None) 

然而,你正在传递一个函数指针(“length”是你的代码中的一个函数,我建议用有意义的方式命名你的函数,避免使用通用关键字)而不是第一个参数。

对于野生指南,你可以把你作为指数第一个参数(而不是我的占位符空列表),这将是固定

relevant = tf.reduce_sum(
     tf.mul(output, tf.expand_dims(tf.one_hot([], max_length), -1)), 1)