本节所讲的神经网络输出层主要是指用于识别器中的输出层设计,识别器的输出层一般具有两个功能,第一判断图像是真实图像还是生成的图像,第二用于Conditional GANs中判断图像的类别。
如果是判别图像是否是真实图像,通常我们会直接使用sigmoid函数,
如果是判别图像的类别,且不是二分类问题的话 我们一般都会使用到softmax函数
当然也有直接输出不使用sigmoid或者softmax函数的, 但是这种情况都是和tensorflow封装好的优化器相结合,这里就不讲这部分。 等讲到优化器那一章好好谈谈这种情况
softmax函数其公式为 一般用于discriminator最后一层中输出数据属于某一类别的概率
我的理解就是求数据最有可能是那个类别 而且从结果可以看出 softmax最后输出的数据总和一定是1
import numpy as np
import tensorflow as tf
def softmax(x):
c = np.max(x)
exp = np.exp(x-c)
sum_exp = np.sum(exp)
class_y = exp / sum_exp
return class_y
sample = np.array([-1, 1, -2, 2])
result = softmax(sample)
sum_result = np.sum(result)
print("the result of softmax difined by myself is:",result)
print("the sum of softmax difined by myself is:",sum_result)
sample = tf.convert_to_tensor(sample,dtype=tf.float64)
tf_result = tf.nn.softmax(sample)
sess = tf.Session()
print("the result of sigmoid difined by tensorflow is: ", sess.run(tf_result))
'''
程序输出结果是:
the result of softmax difined by myself is: [0.03467109 0.25618664 0.01275478 0.69638749]
the sum of softmax difined by myself is: 1.0
the result of softmax difined by tensorflow is: [0.03467109 0.25618664 0.01275478 0.69638749]
'''