philipperemy/keras-tcn

Ensuring unique weights when a model uses multiple TCNs

vlawhern opened this issue · 2 comments

This is probably a stupid question, but I just wanted to confirm:

When you have a model that includes multiple TCNs, say of this form:

input1 = Input(shape = (shape1))
x1     = TCN(..., name='model_1')(input1)

# note that shape2 can be different than shape1
input2 = Input(shape = (shape2))
x2     = TCN(..., name='model_2')(input2)

# concatenate learned TCN features
x      = Concatenate()([x1,x2])

# some further layers
x      = ...

# final model output
outputs = Dense(...)(x)

model  = Model(inputs = [input1, input2], outputs = outputs)

passing in unique name = (some_name) inside the TCN layer is sufficient to ensure that all model weights and layers are unique correct? that there is no sharing of weights/layers behind-the-scenes. Because when I look at the model summary, using tcn_full_summary it looks like some layers share the same name (and thus I think would share the weights too?)

krzim commented

The residual blocks are programmatically named in the TCN and I'm assuming those are the layers you're concerned with. They're all named under tf.name_scopes which is used to add a prefix to all ops under the scope. The unique name of each TCN means that even those programmatically generated names for the residual blocks fall under different scope trees (is that the right term?).

You would need to explicitly tell Tensorflow to reuse variables/share weights with something like a tf.variable_scope("some_name", reuse=True) context. There's a pretty great StackOverflow answer covering this topic here.

tl;dr: Not a stupid question and the weights should be unique.

That explanation makes sense.. I did see that the weights were being assigned under a name_scope (see https://github.com/philipperemy/keras-tcn/blob/master/tcn/tcn.py#L87) but was thrown off a bit since in the model summary some of the layer names matched.

After I posted my issue I did some further checking of the weights directly to confirm that they are all different and in fact they are. So yes all the weights/layers are unique.

Thanks for the help... will close the thread.