问题描述
当我尝试在具有conv1d层的网络中计算X wrt Y的梯度(X或Y是什么都无关紧要)时,我收到消息维度无效索引:3、0, C",该过程终止.
When I try to compute the gradient of X w.r.t Y (doesn't really matter what X nor Y is) in a network with a conv1d layer I'm getting the message "Invalid index from the dimension: 3, 0, C" and the process dies.
最小工作示例:
import numpy as np
from tensorflow.python.keras import models
from tensorflow.python.keras import layers
from tensorflow.python.keras import backend as K
inp = layers.Input(shape=(10, 20,))
conv = layers.Conv1D(filters=10, kernel_size=2)(inp)
pool = layers.GlobalMaxPool1D()(conv)
output = layers.Dense(1, activation="sigmoid")(pool)
m = models.Model(inp, output)
m.summary()
m.compile(optimizer="adam", loss="binary_crossentropy")
似乎可以工作:
m.fit(x=np.random.randn(100, 10, 20), y=np.random.randn(100))
此中断:
loss = K.mean(m.output)
grads = K.gradients(loss, m.input)[0]
f = K.function([m.input], [grads])
print(f([np.random.randn(10, 20)]))
我的python,keras,tf版本:
My python, keras, tf versions:
import tensorflow as tf
import sys
from tensorflow.python import keras
print(tf.__version__)
print(keras.__version__)
print(sys.version)
1.12.0
2.1.6-tf
3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 14:01:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
与计算梯度到什么无关紧要..错误消息是
It doesn't really matter of what I calculate the gradient wrt to what.. The error message is
2019-04-19 17:00:58.249788: F ./tensorflow/core/util/tensor_format.h:420] Check failed: index >= 0 && index < dimension_attributes.size() Invalid index from the dimension: 3, 0, C
我看到它与基于错误消息的转换1d层有关,但我不太了解我在这里缺少的内容.谢谢你的提示.
I see that it is related to the conv 1d layer based on the error message, but I don't quite understand what I'm missing here. Thanks for any hints.
推荐答案
简短答案:形状不兼容,将调用更改为:f([np.random.randn(1, 10, 20)])
.
Short answer: shape incompatibility, change the call to: f([np.random.randn(1, 10, 20)])
.
长答案::由于您将输入形状设置为(10, 20,)
,这意味着每个输入样本的形状为(10,20)
.但是,您还必须注意,Keras模型需要一批样品作为输入.因此,在这种情况下,它将期望一个具有3维的数组,其中第一个维表示批处理维.由于您要使用一个样本来填充模型,因此输入数组的形状必须为(1, 10, 20)
.因此,您必须在randn
函数中相应地更改形状:
Long answer: Since you have set the input shape as (10, 20,)
, it means that each input sample has a shape of (10,20)
. However, you must also note that Keras models expect a batch of samples as their input. Therefore, in this case it would expect an array with 3 dimensions where the first dimension indicates the batch dimension. Since you want to feed the model with one sample, the input array must have a shape of (1, 10, 20)
. So you must change the shape in randn
function accordingly:
f([np.random.randn(1, 10, 20)])
^
|
|
batch dimension
这篇关于keras梯度计算失败“来自维度的无效索引:3、0,C";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!