Was ist der beste Weg, um den LSTM-Status zwischen Läufen in Tensorflow zu speichern? Für die Vorhersagephase muss ich die Daten jeweils um einen Schritt weiterleiten, da die Eingabe des nächsten Zeitschritts auf der Ausgabe des vorherigen Zeitschritts beruht.Speichern des LSTM-RNN-Status zwischen Läufen in Tensorflow
Ich habe den Vorschlag von diesem Beitrag verwendet: Tensorflow, best way to save state in RNNs? und getestet, indem Sie die gleiche Eingabe immer und immer wieder übergeben, ohne den Optimierer zu laufen. Wenn ich richtig verstehe, wenn die Ausgabe jedes Mal ändert, dann speichert es den Zustand, aber wenn es gleich bleibt, ist es nicht. Das Ergebnis war, dass es den Zustand das erste Mal speichert, aber dann gleich bleibt.
Hier ist mein Code:
pieces = data_generator.load_pieces(5)
batches = 100
sizes = [126, 122]
steps = 128
layers = 2
x = tf.placeholder(tf.float32, shape=[batches, steps, sizes[0]])
y_ = tf.placeholder(tf.float32, shape=[batches, steps, sizes[1]])
W = tf.Variable(tf.random_normal([sizes[0], sizes[1]]))
b = tf.Variable(tf.random_normal([sizes[1]]))
layer = tf.nn.rnn_cell.BasicLSTMCell(sizes[0], forget_bias=0.0)
lstm = tf.nn.rnn_cell.MultiRNNCell([layer] * layers)
# ~~~~~ code from linked post ~~~~~
def get_state_variables(batch_size, cell):
# For each layer, get the initial state and make a variable out of it
# to enable updating its value.
state_variables = []
for state_c, state_h in cell.zero_state(batch_size, tf.float32):
state_variables.append(tf.nn.rnn_cell.LSTMStateTuple(
tf.Variable(state_c, trainable=False),
tf.Variable(state_h, trainable=False)))
# Return as a tuple, so that it can be fed to dynamic_rnn as an initial state
return tuple(state_variables)
states = get_state_variables(batches, lstm)
outputs, new_states = tf.nn.dynamic_rnn(lstm, x, initial_state=states, dtype=tf.float32)
def get_state_update_op(state_variables, new_states):
# Add an operation to update the train states with the last state tensors
update_ops = []
for state_variable, new_state in zip(state_variables, new_states):
# Assign the new state to the state variables on this layer
update_ops.extend([state_variable[0].assign(new_state[0]),
state_variable[1].assign(new_state[1])])
# Return a tuple in order to combine all update_ops into a single operation.
# The tuple's actual value should not be used.
return tf.tuple(update_ops)
update_op = get_state_update_op(states, new_states)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
output = tf.reshape(outputs, [-1, sizes[0]])
y = tf.nn.sigmoid(tf.matmul(output, W) + b)
y = tf.reshape(y, [-1, steps, sizes[1]])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), [1, 2]))
# train_step = tf.train.AdadeltaOptimizer().minimize(cross_entropy)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
batch_x, batch_y = data_generator.get_batch(pieces)
for i in range(500):
error, _ = sess.run([cross_entropy, update_op], feed_dict={x: batch_x, y_: batch_y})
print str(i) + ': ' + str(error)
Hier ist der Fehler im Laufe der Zeit:
- 0: 419,861
- 1: 419,756
- 2: 419,756
- 3: 419,756 . ..
Mögliche Duplikat [TensorFlow: Speichern LSTM Zustand für die nächste Charge (Stateful LSTM)] (http://stackoverflow.com/questions/38241410/tensorflow-remember-lstm-Zustand-für-nächsten-Batch-stateful-lstm) –
Ich empfehle Ihnen [diese Antwort] (https://stackoverflow.com/questions/44703593/tensorflow-how-to-access- all-the-middle-states-of-a-rnn-nicht-gerade-die-letzte-sta) die ich vor ein paar tagen probiert habe. Es läuft gut. – winwin