如何初始化LSTM中的变量?

时间:2018-06-09 06:39:56

标签: python python-3.x tensorflow

当我尝试在TensorFlow中运行此LSTM模型时,我遇到了困难。我对图书馆来说比较新,所以请原谅我。

from tensorflow.contrib import ffmpeg, rnn
import tensorflow as tf
import os
import time

# hyperparameters
learning_rate = 0.01
total_iterations = 100
layer_width = 5
network_depth = 10

# path variables
mp3_output_path = "/Users/espresso/Documents/Projects/Martin/ouput"
mp3_input_path = "/Users/espresso/Documents/Projects/Martin/input/Caravan.mp3"

# Function Definitions
def import_audio(mp3_location):
    binary_audio = tf.read_file(mp3_location)
    audio_tensor = ffmpeg.decode_audio(binary_audio, file_format="mp3", samples_per_second=int(44100/2), channel_count=1)
    reformatted_audio_tensor = tf.reshape(audio_tensor, [1, tf.shape(audio_tensor)[0], 1])
    return reformatted_audio_tensor


def export_audio(audio_tensor, mp3_target_dest):
    encoded_audio = ffmpeg.encode_audio(audio_tensor, file_format="mp3", samples_per_second=(44100/2))
    mus_file = open(mp3_target_dest, 'wb+')
    mus_file.write(encoded_audio)
    mus_file.close()
    print("Export complete. File located at {}".format(mp3_target_dest))
    return 0


def lstm_layer(cell_quantity):
    return rnn.LSTMCell(cell_quantity)


def stacked_lstm(layer_width, depth):
    if type(layer_width) == list:
        lstm_layer_lib = [lstm_layer(layer_width[element]) for element in layer_width]
    else:
        lstm_layer_lib = [lstm_layer(layer_width) for element in range(depth)]
        lstm_layer_lib.insert(0, lstm_layer(3))
        lstm_layer_lib.append(lstm_layer(1))

    return rnn.MultiRNNCell(lstm_layer_lib)

def inaccuracy(net_loss):
    return net_loss

# variable definitions
input = tf.placeholder(dtype=tf.float32, shape=[1, 5282816, 1], name="Audio_Input")
y_labels = tf.placeholder(dtype=tf.float32, shape=[1, 5282816, 1], name="Y_Labels")

init_op = tf.global_variables_initializer()

network_outline = stacked_lstm(layer_width, network_depth)
hypothesis, state = tf.nn.dynamic_rnn(network_outline, input, dtype=tf.float32)
cross_entropy =   tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_labels, logits=hypothesis))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

with tf.Session() as sess:
    sess.run(init_op)

    input_mp3_tensor = sess.run(import_audio(mp3_input_path))
    vector_size = int(sess.run(tf.shape(input_mp3_tensor))[1]) - 1


    y_labels_tensor = sess.run(tf.convert_to_tensor([input_mp3_tensor[0][element+1][0] for element in range(vector_size)] + [0]))
    reformatted_y_labels_tensor = sess.run(tf.reshape(y_labels_tensor, [1, tf.shape(y_labels_tensor)[0], 1]))

    for iteration in range(total_iterations):
        loss, _ = sess.run([cross_entropy, optimizer], feed_dict={input: input_mp3_tensor, y_labels: reformatted_y_labels_tensor})

如您所见,我在变量init_op中声明了tf.global_variables_intilizer(),并在执行Session中的任何其他操作之前执行它。仍然,代码完成后,将返回以下错误消息:

Caused by op 'rnn/multi_rnn_cell/cell_4/lstm_cell/bias/read', defined at:
  File "martin.py", line 41, in <module>
hypothesis, state = tf.nn.dynamic_rnn(network_outline, input, dtype=tf.float32)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 635, in dynamic_rnn
dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 832, in _dynamic_rnn_loop
swap_memory=swap_memory)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3202, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2940, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2877, in _BuildLoop
body_result = body(*packed_vars_for_body)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3178, in <lambda>
body = lambda i, lv: (i + 1, orig_body(*lv))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 803, in _time_step
(output, new_state) = call_cell()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 789, in <lambda>
call_cell = lambda: cell(input_t, state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 191, in __call__
return super(RNNCell, self).__call__(inputs, state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 714, in __call__
outputs = self.call(inputs, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1242, in call
cur_inp, new_state = cell(cur_inp, cur_state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 298, in __call__
*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 696, in __call__
self.build(input_shapes)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 730, in build
initializer=init_ops.zeros_initializer(dtype=self.dtype))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 546, in add_variable
partitioner=partitioner)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/checkpointable.py", line 415, in _add_variable_with_custom_getter
**kwargs_for_getter)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1297, in get_variable
constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1093, in get_variable
constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 431, in get_variable
return custom_getter(**custom_getter_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 194, in _rnn_get_variable
variable = getter(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 408, in _true_getter
use_resource=use_resource, constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 800, in _get_single_variable
use_resource=use_resource)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2157, in variable
use_resource=use_resource)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2147, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2130, in default_variable_creator
constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 235, in __init__
constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 388, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
return gen_array_ops.identity(input, name=name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3053, in identity
"Identity", input=input, name=name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value rnn/multi_rnn_cell/cell_4/lstm_cell/bias
 [[Node: rnn/multi_rnn_cell/cell_4/lstm_cell/bias/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](rnn/multi_rnn_cell/cell_4/lstm_cell/bias)]]

我不完全确定我是否真的应该创建权重并将它们作为参数传递给TensorFlow函数。但我不知道为什么程序不成功。

1 个答案:

答案 0 :(得分:0)

错误的原因是您在定义变量之前定义了init_op(即在调用stacked_lstmtf.nn.dynamic_rnn之前。这意味着您在调用{{{{}}之后定义的变量1}}不会被初始化。

您应该执行以下操作:

init_op
相关问题