How to create a layer to invert a softmax (TensforFlow,python)?

后端 未结 2 414
迷失自我
迷失自我 2021-01-29 03:02

I am building a deconvolution network. I would like to add a layer to it which is the reverse of a softmax. I tried to write a basic python function that returns the inverse of

2条回答
  •  自闭症患者
    2021-01-29 03:35

    Try this:

    import tensorflow as tf
    
    def inv_softmax(x, C):
       return tf.math.log(x) + C
    
    import math
    input = tf.keras.layers.Input(shape=(1,10))
    x = tf.keras.layers.Lambda(lambda x : inv_softmax(x, math.log(10.)),name='inv_softmax')(input)
    model = tf.keras.Model(inputs=input, outputs=x)
    
    a = tf.zeros([1, 1, 10])
    a = tf.nn.softmax(a)
    a = model(a)
    print(a.numpy())
    

提交回复
热议问题