Keras - Repeated Weighing of the Previous Layer - Converting to Keras Tensor

I am trying to reuse the weight matrix from the previous layer. As an example of toys, I want to do something like this:

import numpy as np
from keras.layers import Dense, Input
from keras.layers import merge
from keras import backend as K
from keras.models import Model

inputs = Input(shape=(4,))
inputs2 = Input(shape=(4,))
dense_layer = Dense(10, input_shape=(4,))
dense1 = dense_layer(inputs)

def my_fun(my_inputs):
    w = my_inputs[0]
    x = my_inputs[1]

    return K.dot(w, x)

merge1 = merge([dense_layer.W, inputs2], mode=my_fun)

The problem is that dense_layer.Wkeras is not a tensor. So I get the following error:

Exception: Output tensors to a Model must be Keras tensors. Found: dot.0

Any idea on how to convert dense_layer.Wto Keras tensor?

thank

+4
source share
3 answers

It seems that you want to share weights between layers. I think you can use denselayer as a common layer for inputs and inputs2.

merge1=dense_layer(inputs2)

Check out the common layers @ https://keras.io/getting-started/functional-api-guide/#shared-layers

+1
source

, , .

, , , , . .

- get_weights() , numpy, . .

0

: , :

  • W , W , - . dense.weights[0], W .

  • If you intend to use the value of the W-matrix while writing your code, and this value will never change, use K.constant(dense.get_weights[0])one that extracts the weight as a numpy array and converts to a tensor.

0
source

Source: https://habr.com/ru/post/1655014/


All Articles