我正在使用深度神经网络(多层感知器)进行关键字定位,我面临以下问题。
我必须在语音信号中检测关键字。我使用Tensorflow库,并基于此示例编写代码。
'''
A Multilayer Perceptron implementation example using TensorFlow library.
This example is using the MNIST database of handwritten digits
(http://yann.lecun.com/exdb/mnist/)
Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''
# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import tensorflow as tf
# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1
# Network Parameters
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print "Epoch:", '%04d' % (epoch+1), "cost=",
"{:.9f}".format(avg_cost)
print "Optimization Finished!"
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})
我使用双增量的MFCC特征来训练我的网络。在输入中,我有2300个输入,对应于大约600ms的MFCC,以添加上下文。在输出中,我有两个类,mykeyword
和另一个类filler
。
我有一个3x128 MLP与ReLU激活和softmax输出层。我还对数据进行了居中和漂白。
我的标签向量看起来像:[ 1 0 ]
为关键字和[ 0 1 ]
为"填充"。
我想在最后得到一个概率,它破坏了检测关键字的置信度得分,因此我可以使用阈值。然而,在softmax层之后,我只得到每个测试数据示例的0
或1
。我真的不明白为什么……
谢谢
在数字无限精确的世界中,您的模型将略有不同。实际上,您将在模型的末尾使用tf.nn.softmax
,并针对cross_entropy
进行优化。然而,数字具有精度,并且在反向传递期间计算交叉熵梯度之后的softmax将导致数值不稳定。
问题是,softmax -> cross_entropy
的组合梯度非常稳定(它只是softmax正确预测和output - 1
错误预测的输出)。因此,tensorflow允许您直接针对softmax + crossentropy
进行优化,在这种情况下,它使用稳定的计算梯度的方式。
这就引入了一个问题——图中没有节点实际计算概率,概率隐藏在目标函数中。
要解决这个问题,请在图中添加一个额外的节点:
prob = tf.nn.softmax(pred)
求值。它将返回实际的概率。
你观察到的0
和1
很可能是argmax
的输出。argmax
在某种意义上是"硬"的,它只是返回最大值,而不是概率(并且最大值方便地匹配您在softmax之前或之后计算它)。