i trying implement regularization term norm of gradient improved wgan training in keras theano backend. want penalize l2 norm of gradient based on how far away 1.
i implementing custom loss this:
def get_gradient_norm(model, y_pred): weights = model.trainable_weights gradients = model.optimizer.get_gradients(k.mean(y_pred), weights) acc = none g in gradients: s = k.sum(k.square(g)) if acc == none: acc = s else: acc = s + acc return k.sqrt(acc) def make_w_reg_loss(model): lvar = k.variable(lamb, name="lambda") def foo(y_true, y_pred): gnorm = get_gradient_norm(model, y_pred) return lvar * k.square(gnorm - 1) return foo [...] critic.compile(loss=make_w_reg_loss(critic), optimizer=rmsprop(learn_rate))
it throws disconnectedinputerror once training process tries attempt gradient of custom loss function.
why?
replacing loss standard loss works. error loss function defined.
see gist a minimal not-working example of attempt
edit:
so think know how make work now. first randomly added term loss, directly before returning foo(y_true, y_pred):
k.mean(y_pred) - k.mean(y_pred)
clearly constant zero, , if use term loss zero. if add "constant zero" regularization loss works fine. loss, non-zero comes regularization, , optimization on many train_on_batch reduce loss well.
so odd issue theano being bit overzealous in throwing exceptions? question still stands: why throw in original code. since adding constant 0 term fixes it, looks bug me?
i implement improved wgan in keras too, , i'm surprised see how solved "issue". did verify trought experiments wgan-gp loss working intended? should easy check, stable training enable use of deep discriminator ;) same work done tensorflow backend, , try looking @ code , code here: keras improved wgan
i happy hear updates, i'll write again here have working code of wgan-gp in keras/tensorflow! p.s. link above implementing procedure in tensorflow code, forcing use tf training functions. approach, can define keras loss, using of our usual keras high level api training ;)
edit: code seems work totally k backend, code should run tensorflow backend too. did try changing backend check if issue/bug related theano?
2nd edit: calculating gradient w.r.t weights, in wgan-gp paper gradient penalty calculated starting gradient w.r.t average samples between generated , real samples. bring different results. in following link can find nice improved wgan loss implementation, could work on theano too: https://github.com/farizrahman4u/keras-contrib/
Comments
Post a Comment