How to give a constant input to keras

Viewed 18251

My network has two time-series inputs. One of the input has a fixed vector repeating for every time step. Is there an elegant way to load this fixed vector into the model just once and use it for computation?

3 Answers

Something to add: When you come to compile the model you need to give the constant input as an input otherwise the graph disconnects

#your input
inputs = Input(shape = (input_shape,))

# an array of ones
constants = [1] * input_shape

# make the array a variable
k_constants = K.variable(constants, name = "ones_variable") 

# make the variable a tensor
ones_tensor = Input(tensor=k_constants, name = "ones_tensor")

# do some layers
inputs = (Some_Layers())(inputs)

# get the complementary of the outputs
output = Subtract()([ones_tensor,inputs])

model = Model([inputs, ones_tensor],output)
model.complie(some_params)

when you train you can just feed in the data you have, you don't need the constant layer anymore.

I have found that no matter what you try it's usually easier to just use a custom layer and take advantage of the power of numpy:

class Complementry(Layer):

    def __init__(self, **kwargs):
        super(Complementry, self).__init__(**kwargs)

    def build(self, input_shape):
        super(Complementry, self).build(input_shape)  # Be sure to call this at the end

    def call(self, x):
        return 1-x  # here use MyArray + x
Related