python-2.7 - TensorFlow RNN 训练不会执行?

标签 python-2.7 audio tensorflow

我目前正在尝试训练这个 RNN 网络,但似乎遇到了奇怪的错误,我无法解码。

我的 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>

无论如何要告诉这里可能出了什么问题?

最佳答案

我试图在我的本地构建你的代码。
代码中有一个根本性的错误,即您调用 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)

但是,您传递的是一个函数指针(“长度”是代码中的一个函数,我建议您通过避免使用常用关键字以有意义的方式命名您的函数)而不是第一个参数。

对于一个狂野的指南,您可以将您的索引作为第一个参数(而不是我的占位符空列表),它将被修复
relevant = tf.reduce_sum(
      tf.mul(output, tf.expand_dims(tf.one_hot([], max_length), -1)), 1)

关于python-2.7 - TensorFlow RNN 训练不会执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193632/

相关文章:

python-2.7 - 如何在 Python 中解释 Unicode 符号?

Python 正则表达式匹配直到多行字符

python - 如何在 Robot Framework 中获取当前测试用例状态通过/失败

ios - 如何在不在设备上下载 mp3 文件的情况下从 URL 流式传输音频

python类常量

iphone - 在 iPhone 和蓝牙设备之间使用蓝牙 HFP 的 Push-To-Talk (PTT) 应用程序

audio - 吉他和弦识别算法?

python - 如何重新训练用于语言翻译的序列到序列神经网络模型?

python - tf.train.get_global_step() 的值与当前训练步骤之间的差异

python - 当从 tensorflow 概率的分布中采样时,张量是不可散列的错误(在colab上)