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

后端 未结 2 384
迷失自我
迷失自我 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:16

    Thanks it works ! I put :

    import keras.backend as K
    
    def inv_softmax(x,C):
       return K.log(x)+K.log(C)
    
    0 讨论(0)
  • 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())
    
    0 讨论(0)
提交回复
热议问题