获取损失:使用自定义损失函数时,tensorflow中的nan

时间:2020-05-21 16:03:11

标签: python tensorflow keras

我正在迷失方向:使用自定义迷失时,张量流中的难点

损失功能代码:

def compute_cost(A,Y):
   cost = tf.math.divide(tf.math.multiply(-1.0, tf.reduce_sum(tf.math.multiply(Y, tf.math.log(A)) + tf.math.multiply( tf.math.subtract(1.0, Y), tf.math.log(tf.math.subtract(1.0, A))))), tf.cast(tf.size(Y), tf.float32))
   return cost 

型号:

model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation = 'relu', input_shape= (784,)),
tf.keras.layers.Dense(32, activation = 'relu'),
tf.keras.layers.Dense(10, activation = 'softmax')])
model.compile(
loss = compute_cost,
optimizer = 'adam',
metrics = ['accuracy']
)

任何解决方案

0 个答案:

没有答案