keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.61k stars 19.42k forks source link

Understanding Sharing Keras Layers #5310

Closed nikcheerla closed 7 years ago

nikcheerla commented 7 years ago

I'm trying to build a variational autoencoder that encodes and decodes music sequence data (of shape (50, 84)). Simultaneously, I want to predict the next element inside the music sequence (vector of shape (84,)). So I need to reuse a trainable decoder that maps from the latent space to the output space – for both encoding and next element prediction.

autoencode_output = TimeDistributed(decoder) (z)

x = GRU(64, dropout_W=0.1, dropout_U=0.1, return_sequences=True) (z)
x = GRU(12, dropout_W=0.1, dropout_U=0.1, return_sequences=False) (x)
next_output = decoder(x)

model = Model(input_img, [autoencode_output, next_output])

This is the relevant part of the code; I reuse the "decoder" model twice (in the TimeDistributed wrapper and also by itself). I keep on getting a 'this shared variable already has an update expression' error, however. When do these errors happen when reusing models, and what's the cleanest way to fix them?

Thanks!

unrealwill commented 7 years ago

Hello, I tried to reproduce your bug but couldn't : def buildDecoderModel(): inp = Input(batch_shape=(None,100)) decout = Dense(10)(inp) decoder = Model([inp],[decout] ) inp2 = Input(batch_shape=(None,30,100)) emb = TimeDistributed( decoder )(inp2) gru = GRU(100,return_sequences=True)(emb) gru2 = GRU(100,return_sequences=False)(gru) dec = decoder(gru2) model = Model( [inp2],[gru,dec]) model.compile( loss=["mse","mse"], optimizer="adam" ) return model

I tested with theano :

import numpy as np m= buildDecoderModel() m.fit(np.random.randn(10,30,100),[np.zeros((10,30,100)) , np.zeros((10,10)) ] )

It works fine.

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.