Python K.函数返回“;类型错误:不可损坏的类型:';列表'&引用;

Python K.函数返回“;类型错误:不可损坏的类型:';列表'&引用;,python,tensorflow,deep-learning,keras,Python,Tensorflow,Deep Learning,Keras,我想计算权重的梯度。我正在使用以下代码: (假设模型已定义) (数据和标签也作为输入输入) 但是,这将返回以下错误: 我使用Keras(v2.0.6)和Tensorflow(v1.2.1)作为后端 另外,有没有其他方法不使用K函数来实现这一点?(我不熟悉Keras和Tensorflow)。发现了错误:model.sample\u权重是一个列表,这是一个问题。将其更改为model.sample_weights[0]有效 您是否尝试过使用元组而不是列表?@pdowling,这个answer()实际

我想计算权重的梯度。我正在使用以下代码:

(假设模型已定义)

(数据和标签也作为输入输入)

但是,这将返回以下错误:

我使用Keras(v2.0.6)和Tensorflow(v1.2.1)作为后端

另外,有没有其他方法不使用K函数来实现这一点?(我不熟悉Keras和Tensorflow)。

发现了错误:model.sample\u权重是一个列表,这是一个问题。将其更改为model.sample_weights[0]有效


您是否尝试过使用元组而不是列表?@pdowling,这个answer()实际上表示K.function将列表作为输入。这就是为什么这个错误很奇怪。
weights = model.trainable_weights
gradients = model.optimizer.get_gradients(model.total_loss, weights)
input_tensors = [model.input, model.sample_weights, K.learning_phase() ]
getGradients = K.function(inputs=input_tensors, outputs=gradients)
sampleWeights = np.ones(len(image_data))
inputs = [[image_data, labels], sampleWeights, 0 ]
print getGradients(inputs)
input_tensors = [model.input, model.sample_weights[0], K.learning_phase() ]