Creating constant value in Keras

前端 未结 1 1153
执笔经年
执笔经年 2020-12-18 09:31

I am trying to create a constant variable inside a keras model. What I was doing till now is to pass it as Input. But it is always a constant so I want it as a constant.(The

相关标签:
1条回答
  • 2020-12-18 10:21

    You cannot have a constant with variable size. A constant always has the same value. What you can do is have the (1, 50) constant and then tile it within TensorFlow with K.tile. Also better use np.arange instead of np.array(list(range(50)). Something like:

    from keras.layers.core import Lambda
    import keras.backend as K
    
    def operateWithConstant(input_batch):
        tf_constant = K.constant(np.arange(50).reshape((1, 50)))
        batch_size = K.shape(input_batch)[0]
        tiled_constant = K.tile(tf_constant, (batch_size, 1))
        # Do some operation with tiled_constant and input_batch
        result = ...
        return result
    
    input_batch = Input(...)
    input_operated = Lambda(operateWithConstant)(input_batch)
    # continue...
    
    0 讨论(0)
提交回复
热议问题