我正在尝试构建一个带有一个隐藏层的简单神经网络。我原本期望在训练之前,模型会输出看似随机的值。但实际上,对于所有输入,我得到的输出都是1.0。这是为什么呢?
import tensorflow as tffrom tensorflow import kerasimport numpy as npdef NewModel(): return keras.Sequential([ keras.layers.Dense(20, input_shape=(18,), activation=tf.nn.relu, name="inputLayer"), keras.layers.Dense(1, activation=tf.nn.softmax, name="outputLayer"), ])model = NewModel()i = np.array([[0.2]*18])print(model.predict(i))
回答:
你不能在单个输出神经元上使用softmax函数,因为它通过除以所有神经元的输出进行归一化,这会产生一个恒定的1.0值,这就是你所看到的结果。