0

현재 회귀를 위해 RNN을 구현하려고합니다. 오디오 샘플을 mfcc 기능의 벡터로 변환 할 수있는 신경망을 만들어야합니다. 각 오디오 샘플의 기능이 무엇인지 이미 알고 있으므로 오디오 샘플 목록을 원하는 MFCC 기능으로 변환 할 수있는 신경망을 만드는 것이 자체적 인 작업입니다.Tensorflow를 사용한 RNN 회귀?

제가 직면 한 두 번째 문제는 샘플링 할 오디오 파일의 길이가 다르므로 오디오 샘플 목록의 길이가 달라져서 피드 길이에 문제가 발생할 수 있습니다. 신경 네트워크. 가변 길이 길이를 처리하는 방법에 대한 this post을 발견하고 RNN의 구현에 통합하려고 시도했지만 설명 할 수없는 이유로 많은 오류를 얻을 수없는 것 같습니다.

잘못된 것 내 구현으로? 날이 있습니다

tf.one_hot (아이폰에 (출력), MAX_LENGTH를) 변경 :

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) 

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 

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

오류 메시지 :

Traceback (most recent call last): 
    File "tensorflow_test.py", line 177, in <module> 
    last = last_relevant(output)#tf.gather(val, int(val.get_shape()[0]) - 1) ## Appedning as last 
    File "tensorflow_test.py", line 132, in last_relevant 
    relevant = tf.reduce_sum(tf.mul(output, tf.expand_dims(tf.one_hot(length, max_length), -1)), 1) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2778, in one_hot 
    name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1413, in _one_hot 
    axis=axis, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 454, in apply_op 
    as_ref=input_arg.is_ref) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 621, in convert_to_tensor 
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function 
    return constant(v, dtype=dtype, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 163, in constant 
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape)) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 421, in make_tensor_proto 
    tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values]) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 45, in as_bytes 
    (bytes_or_text,)) 
TypeError: Expected binary or unicode string, got <function length at 0x7f51a7a3ede8> 

편집 여기

코드입니다 오류 메시지 :

Traceback (most recent call last): 
    File "tensorflow_test.py", line 184, in <module> 
    cross_entropy = cost(output,target)# How far am I from correct value? 
    File "tensorflow_test.py", line 121, 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) 

답변

0
tf.one_hot(length, ...) 

여기 길이는 텐서가 아닌 함수입니다. 대신 길이 (무언가)를 사용해보십시오.

+0

'last_relevant'는 0으로 채워지지 않은 것을 의미하는 관련 출력을 추출하는 것으로 가정합니다.이 출력은 'length (output)'을 입력하면 새로운 오류 메시지가 나타납니다. –