如何创建一个层来反转softmax(TensforFlow,python)?

如何创建一个层来反转softmax(TensforFlow,python)?,python,tensorflow,deep-learning,softmax,deconvolution,Python,Tensorflow,Deep Learning,Softmax,Deconvolution,我正在建立一个反褶积网络。我想给它添加一个与softmax相反的图层。我试图编写一个基本的python函数,返回给定矩阵的softmax的倒数,并将其放入tensorflow Lambda中,然后将其添加到我的模型中。 我没有错误,但当我进行预测时,出口处只有0。当我没有将这个层添加到我的网络时,我输出的不是零。因此,这证明它们是由于我的inv_softmax功能不好造成的。 你能告诉我怎么做吗 我将我的函数定义为: def inv_softmax(x): C=0 S = np.ze

我正在建立一个反褶积网络。我想给它添加一个与softmax相反的图层。我试图编写一个基本的python函数,返回给定矩阵的softmax的倒数,并将其放入tensorflow Lambda中,然后将其添加到我的模型中。 我没有错误,但当我进行预测时,出口处只有0。当我没有将这个层添加到我的网络时,我输出的不是零。因此,这证明它们是由于我的inv_softmax功能不好造成的。 你能告诉我怎么做吗

我将我的函数定义为:

def inv_softmax(x):
   C=0
   S = np.zeros((1,1,10)) #(1,1,10) is the shape of the datas that my layer will receive
   try:
      for j in range(np.max(np.shape(x))):
         C+=np.exp(x[0,0,j])
      for i in range(np.max(np.shape(x))):
         S[0,0,i] = np.log(x[0,0,i]+C
   except ValueError:
      print("ValueError in inv_softmax")
      pass
   S = tf.convert_to_tensor(S,dtype=tf.float32)
   return S
我将其作为以下内容添加到我的网络:

x = ...
x = layers.Lambda(lambda x : inv_softmax(x),name='inv_softmax',output_shape=[1,1,10])(x)
x = ...
如果您需要更多我的代码或其他信息,请询问我。

试试这个:

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())
谢谢,这很有效! 我说:


如果我在我的卷积网络中进入XiFax,我可以把SoftMax转换成(我不是这样做的,谢谢你的话,我会改变它)我席席总是不同于0和1。但问题是,如果要在模型中实现我的函数,我认为我做错了什么。席席(席)/席(Exp(席))Xi=Ln(Si)+Ln(和(Exp(Xi))Ln(和(Xp))是我在我的CONV模型中制作SOFTTMAX时所计算的常数。我的TunSoFrof没有属性数学:(什么是TF版本?V0.12 0RCL,我不能改变它的NP.Log.)。在我的TF(V.2.3.1)中,当我有一个形状数组时,它不可能是另一个信息(也许有用)。(1,1,10)例如,使用值​​我把它转换成张量(我的数组,dtype=tf.float32),然后打印这个东西,我只得到“tf.tensor'Const'shape(1,1,10)dtype=float32”,没有值。当我在网上查看时,人们会得到值​​当他们打印出来的时候。
import keras.backend as K

def inv_softmax(x,C):
   return K.log(x)+K.log(C)