Alk
Alk

Reputation: 5567

Converting between Keras and Lasagne

I am trying to combine a Keras model with a Lasagne layer. I am calling this function in the Lasagne layer:

def get_output_for(self, inputs, deterministic=False):
    self.p = self.nonlinearity(T.dot(inputs[1], self.pi))
    self.mask = sample_mask(self.p)
    if deterministic or T.mean(self.p) == 0:
        return self.p*inputs[0]
    else:
        return inputs[0]*self.mask

The problem is that my inputs object is the output of the previous Keras layer, which is a Tesnor object as Keras layers produce Tensor outputs. This does not work. I am not sure what type inputs are supposed to have or how to convert between Tensor and the type expected by this function.

Upvotes: 0

Views: 409

Answers (1)

user3576513
user3576513

Reputation: 16

I think it is not the best to mix Lasagne and Keras/Tensorflow objects. Instead you can convert the get_output_for method to Tensorflow. In what follows, I suppose that nonlinearity is similar to something like

self.nonlinearity = lambda x: tf.maximum(x, 0)

and that inputs is a numpy.array-like object:

def tf_get_output_for(self, inputs, deterministic=False):
    self.p = self.nonlinearity(tf.matmul(inputs[1], self.pi))
    self.mask = sample_mask(self.p)
    if deterministic or tf.reduce_mean(self.p) == 0:
        return self.p * inputs[0]
    else:
        return inputs[0] * self.mask 

The method sample_mask has to be converted to be "Tensorflow compatible"

Upvotes: 0

Related Questions