激活='softmax'时了解tensorflow keras LSTM
Understanding tensorflow keras LSTM when activation='softmax'
我对 tf.keras.layers.LSTM
中的 activation
kwarg 的理解与对任何其他层(例如 Dense
)的理解相同。但是下面的玩具示例会崩溃。
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM
from numpy.random import uniform
m = Sequential([
LSTM(5, activation='softmax')
])
m.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy'
)
x = uniform(size=(10, 5, 3))
y_pred = m.predict(x)
assert all(y_pred.sum(axis=1) == 1), 'Predictions are not a valid categorical distribution'
这会失败。 y_pred.sum(axis=1)
将是 0 和 1 之间的随机浮点数的向量,总和不为 1。例如:
[0.5083836, 0.50286007, 0.504391, 0.5309103, 0.5131448, 0.53934443, 0.5301699, 0.49676484, 0.51985925, 0.5021868]
LSTM 似乎没有在这里应用有效的 softmax。这是设计使然,我只是不了解 LSTMCell
的基本原理吗?
当然,我可以通过附加 tf.keras.layers.Softmax()
来实现适当的 softmax 分布,但我很好奇为什么这是必要的。
它不等于一,因为这里的激活直接应用于每个隐藏单元,正如@joelthchao 在 GitHub
上所说
我对 tf.keras.layers.LSTM
中的 activation
kwarg 的理解与对任何其他层(例如 Dense
)的理解相同。但是下面的玩具示例会崩溃。
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM
from numpy.random import uniform
m = Sequential([
LSTM(5, activation='softmax')
])
m.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy'
)
x = uniform(size=(10, 5, 3))
y_pred = m.predict(x)
assert all(y_pred.sum(axis=1) == 1), 'Predictions are not a valid categorical distribution'
这会失败。 y_pred.sum(axis=1)
将是 0 和 1 之间的随机浮点数的向量,总和不为 1。例如:
[0.5083836, 0.50286007, 0.504391, 0.5309103, 0.5131448, 0.53934443, 0.5301699, 0.49676484, 0.51985925, 0.5021868]
LSTM 似乎没有在这里应用有效的 softmax。这是设计使然,我只是不了解 LSTMCell
的基本原理吗?
当然,我可以通过附加 tf.keras.layers.Softmax()
来实现适当的 softmax 分布,但我很好奇为什么这是必要的。
它不等于一,因为这里的激活直接应用于每个隐藏单元,正如@joelthchao 在 GitHub
上所说