How do I access layer weights inside a Keras model?

Viewed 215

I am trying to access the weights of a Keras layer and use the weight values themselves as the input to a different layer.

Here's a rough outline of what I'm hoping to achieve:

def generate_myModel(SEQUENCE_LENGTH, FILT_NUM, FILT_SIZE):

  ip = keras.layers.Input(shape = (SEQUENCE_LENGTH,1))

  conv_layer = keras.layers.Conv1D(filters = FILT_NUM, kernel_size = FILT_SIZE)
  y = conv_layer(ip)

  y = keras.layers.GlobalMaxPooling1D()(y)

  out_y = keras.layers.Dense(units = 1, activation = 'linear')(y)

  # Acquire the actual weights from the previous convolution layers
  w1 = <WEIGHTS FROM conv_layer - THIS IS THE PART IN QUESTION>
  out_w1 = keras.layers.Lambda( lambda x: K.std(x)/K.abs(K.mean(x)) )(w1)

  myModel = keras.models.Model(inputs = ip, outputs = [out_y, out_w1])

  return myModel

I'm aware that, once you have an instantiated model, you can use model.layers[i].get_weights(), but I'd like to be able to do this inside the actual architecture of the model.

Is this possible?

EDIT------------------------------------

Attempting the solution from the comments, I add layer.get_weights() into the model's architecture as so:

def generate_myModel(SEQUENCE_LENGTH, FILT_NUM, FILT_SIZE):

  ip = keras.layers.Input(shape = (SEQUENCE_LENGTH,1))

  conv_layer = keras.layers.Conv1D(filters = FILT_NUM, kernel_size = FILT_SIZE)
  y = conv_layer(ip)

  # Acquire the actual weights from the previous convolution layers
  w1 = K.constant(conv_layer.get_weights()) # layer.get_weights returns a Numpy 
                                            # array, I need a Keras Tensor - 
                                            # so I use K.constant()

  y = keras.layers.GlobalMaxPooling1D()(y)

  out_y = keras.layers.Dense(units = 1, activation = 'linear')(y)
  out_w1 = keras.layers.Lambda( lambda x: K.std(x)/K.abs(K.mean(x)) )(w1)

  myModel = keras.models.Model(inputs = ip, outputs = [out_y, out_w1])

  return myModel

but this leaves me with the following error:

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

Any guidance would be greatly appreciated!

0 Answers
Related