我創(chuàng)建了一個自定義 Keras 層。該模型編譯得很好,但在訓練時給了我以下錯誤:ValueError:一個操作有None梯度。請確保您的所有操作都定義了漸變(即可微分)。沒有梯度的常見操作:K.argmax、K.round、K.eval。我的自定義層中是否有任何實現(xiàn)錯誤?class SpatialLayer(Layer): def __init__(self, output_dim, **kwargs): self.output_dim = output_dim super(SpatialLayer, self).__init__(**kwargs) def build(self, input_shape): self.bias = None self.built = True self.kernelA = self.add_weight(name='kernelA', shape=(input_shape[1]-2, self.output_dim), initializer='uniform', trainable=True) def compute_output_shape(self, input_shape): return (input_shape[0], input_shape[1]-2, input_shape[1]-2, self.output_dim) def call(self, inputs): x_shape = tf.shape(inputs) top_values, top_indices = tf.nn.top_k(tf.reshape(inputs, (-1,)), 10, sorted=True,) top_indices = tf.stack(((top_indices // x_shape[1]), (top_indices % x_shape[1])), -1) top_indices = tf.cast(top_indices, dtype=tf.float32) t1 = tf.reshape(top_indices, (1,10,2)) t2 = tf.reshape(top_indices, (10,1,2)) result = tf.norm(t1-t2, ord='euclidean', axis=2) x = tf.placeholder(tf.float32, shape=[None, 10, 10, 1]) tensor_zeros = tf.zeros_like(x) matrix = tensor_zeros + result return K.dot(matrix, self.kernelA) model = applications.VGG16(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3)) model.layers.pop() new_custom_layers = model.layers[-1].output model.layers[-1].trainable = False new_custom_layers = Conv2D(filters=1, kernel_size=(3, 3))(new_custom_layers) new_custom_layers = SpatialLayer(output_dim=1)(new_custom_layers) new_custom_layers = Flatten()(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) new_custom_layers = Dropout(0.5)(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers)任何幫助,將不勝感激。
1 回答

FFIVE
TA貢獻1797條經(jīng)驗 獲得超6個贊
您不能通過不可微分的函數(shù)進行反向傳播。而且你的功能是不可微的。
您丟棄了這些值top_values
,只保留了整數(shù)常量top_indices
。
在模型中使用這一層的唯一方法是,如果之前的所有內(nèi)容都不可訓練。(或者,如果您找到另一種以可微分方式計算所需內(nèi)容的方法 - 這意味著:必須涉及輸入值的操作)
添加回答
舉報
0/150
提交
取消