Python 为什么tf.reduce_mean()返回NaN值?I';我试图计算均方误差,但我一直得到NaN值

Python 为什么tf.reduce_mean()返回NaN值?I';我试图计算均方误差,但我一直得到NaN值,python,tensorflow,Python,Tensorflow,这是我正在使用的代码: tf.reset_default_graph() self.sess = tf.InteractiveSession() self.X = tf.placeholder(tf.float32, [None, self.state_space]) self.Y = tf.placeholder(tf.float32, [None, self.action_space]) layer1 = tf.layers.dense(self.X, 128, activation

这是我正在使用的代码:

tf.reset_default_graph()
self.sess = tf.InteractiveSession() 
self.X = tf.placeholder(tf.float32, [None, self.state_space]) 
self.Y = tf.placeholder(tf.float32, [None, self.action_space]) 
layer1 = tf.layers.dense(self.X, 128, activation = tf.nn.leaky_relu) 
layer2 = tf.layers.dense(layer1, 64, activation = tf.nn.leaky_relu) 
layer3 = tf.layers.dense(layer2, 32, activation = tf.nn.leaky_relu)
self.outputs = tf.layers.dense(layer3, self.action_space, activation='softmax') 
self.loss = tf.reduce_mean(tf.square(self.Y - self.outputs))
#self.loss = tf.sqrt(tf.reduce_sum(tf.square(self.Y - self.outputs)) + 1.0e-12)
self.optimizer = tf.train.AdamOptimizer(1e-6).minimize(self.loss)   
self.sess.run(tf.global_variables_initializer()) 
我尝试了一些基本的更改,例如添加一个小常量、使用tf.log()等,但到目前为止都没有效果


提前向任何能提供帮助的人表示感谢

通过更改tensorflow版本,最终使损失函数起作用

from tensorflow.python.framework import ops
%tensorflow_version 1.x

ops.reset_default_graph()
然后,在设置我的trader(在我的代码中定义为类)时,我添加了以下代码段:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
我很高兴现在可以这样做,但我仍然不明白为什么不能用我的原始代码来解决这个问题:

import tensorflow as tf 
在添加这些代码片段之前,我一直在使用Tensorflow 2.4.1。
如果有人知道答案,请在下面评论/回答。干杯

您的数据中可能有一些NAN。检查数据。我对数据集进行了预处理,然后重新检查了NaN值。我在那里没有价值观。