对于一个研究项目,我需要在评估损失之前通过python函数传递Tensorflow图的输出。我正在尝试从这里使用该方法:https://gist.github.com/harpone/3453185b41d8d985356cbe5e57d67342,但是仍然出现以下错误:
Traceback (most recent call last):
File "train.py", line 111, in <module>
train_fn = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(error)
File "/usr/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py", line 421, in minimize
([str(v) for _, v in grads_and_vars], loss))
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel:0' shape=(121, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_2/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_2/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_3/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_3/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_4/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_4/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_5/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_5/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_6/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_6/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_7/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_7/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_8/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_8/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_9/basic_lstm_cell/kernel:0' shape=(240, 480) dtype=float32_ref>", "<tf.Variable 'rnn/multi_rnn_cell/cell_9/basic_lstm_cell/bias:0' shape=(480,) dtype=float32_ref>", "<tf.Variable 'fully_connected/weights:0' shape=(120, 16) dtype=float32_ref>", "<tf.Variable 'fully_connected/biases:0' shape=(16,) dtype=float32_ref>"] and loss Tensor("Mean:0", shape=(), dtype=float32).
这是相关的代码摘录:
# Define custom py_func which takes also a grad op as argument:
def py_func(func, inp, Tout, stateful=False, name=None, grad=None):
# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
tf.RegisterGradient(rnd_name)(grad) # see _MySquareGrad for grad example
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
def voicelayer(xin, name=None):
with tf.name_scope(name, "VoiceLayer", [xin]) as name:
xout = py_func(wave_gen,
[xin],
[tf.float32],
stateful=False,
name=name,
grad=_VoiceLayerGrad)
return xout[0]
def _VoiceLayerGrad(op, grad):
x = op.inputs[0]
return grad * x
# Define placeholder Tensors
inputs = tf.placeholder(tf.float32, (None, None, INPUT_SIZE)) # (time, batch, in)
outputs = tf.placeholder(tf.float32, (None, PADDING_SIZE)) # (batch, out)
# Add LSTM cells to model
cell = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.BasicLSTMCell(RNN_HIDDEN, state_is_tuple=True) for _ in range(RNN_LAYERS)], state_is_tuple=True)
# Initialize LSTM cells
batch_size = tf.shape(inputs)[1]
initial_state = cell.zero_state(batch_size, tf.float32)
rnn_outputs, rnn_states = tf.nn.dynamic_rnn(cell, inputs, initial_state=initial_state, time_major=True)
# Resize output from LSTMs and apply activation function
final_projection = lambda x: layers.linear(x, num_outputs=OUTPUT_SIZE, activation_fn=tf.nn.sigmoid)
predicted_outputs = tf.map_fn(final_projection, rnn_outputs)
# Add the synthsizer layer
final_outputs = voicelayer(predicted_outputs, name="Synth_output")
#error = tf.losses.mean_squared_error(outputs, final_outputs)
# compute elementwise cross entropy.
error = -(outputs * tf.log(final_outputs + TINY) + (1.0 - outputs) * tf.log(1.0 - final_outputs + TINY))
error = tf.reduce_mean(error)
# optimize
train_fn = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(error)
有什么想法吗?预先感谢!