简体   繁体   English

如何theano张量将具有nan值的数组求和

[英]How to theano tensor sum an array with nan values

I am writing an autoencoder model in using Theano, (I am very new to Theano). 我正在使用Theano编写一个自动编码器模型,(对于Theano来说我很陌生)。 The cost function has a sparsity constraint. 成本函数具有稀疏性约束。 The KL divergent function produces NaN values in the array, when I sum the array to add it to the overall cost it gives a NaN value. KL发散函数会在数组中生成NaN值,当我对数组求和以将其加到总成本中时,它将给出NaN值。 Is there any way to get around this problem. 有什么办法可以解决这个问题。

KL = rho * (T.log(rho/rho_hat)) + (1 - rho) * (T.log((1 - rho)/(1 - rho_hat)))
# sparsity cost
SPcost = beta * KL.nansum()
# the loss function 
loss = T.nnet.categorical_crossentropy(y_hat, y).mean() + loss_reg 

I am trying to debug using a test function 我正在尝试使用测试功能进行调试

test=theano.function([X], SPcost)
test(train_X)

SPcost should give me a single scalar value, instead it shows array(nan) I have tried to use numpy nansum() but that gives me an error. SPcost应该给我一个标量值,相反它显示了我尝试使用numpy nansum()的array(nan),但这给了我一个错误。 What is the correct way of summing the array with the NaN values? 用NaN值对数组求和的正确方法是什么? Any suggestion would be much appreciated. 任何建议将不胜感激。

due to numerical issues NaN may pop up anytime, so it is basically unavoidable. 由于数值问题,NaN随时可能弹出,因此基本上是不可避免的。 I looked for functions in theano for dealing with nan but did not find anything that helps me. 我在theano中寻找用于处理nan的函数,但没有找到任何对我有用的东西。

When you're stuck with nan 's of which you fully understand the implications, switch() and isnan() offer a way out: 当您完全了解nan的含义时, switch()isnan()提供了一条出路:

KL = T.switch(T.isnan(KL), 0., KL)

Where KL would be a tensor containing nan 's that you wish to replace with 0 's (albeit, at a certain cost). 其中KL是包含nan的张量,您希望将其替换为0 (尽管有一定的代价)。

You can then T.sum(KL) like any normal day. 然后,您可以像平常的任何一天一样T.sum(KL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM