TensorFlow Keras Softmax 层输出相对于输入多了一维

TensorFlow Keras Softmax layer output has one more dimension respect to the input

我有一个以 Softmax 层结尾的 keras 模型。 根据定义,Softmax 的输出形状与输入相同,但在我的例子中它有一个额外的维度:[1, None, 20] 而不是 [None, 20]

谁能给我解释一下为什么?现在我用挤压修复了,但它仍然很奇怪

谢谢!

 def create_keras_model_embedding():
  l = tf.keras.layers
  a = l.Input(shape=(784,))
  embedded_lookup_feature = tf.feature_column.numeric_column('x', shape=(784))
  dense_features = l.DenseFeatures(embedded_lookup_feature)({'x': a})#{'x': a}
  dense = l.Dense(784)(dense_features)
  dense_2 = l.Dense(10, kernel_initializer='zeros')(dense),
  output = l.Softmax(axis=1)(dense_2)
  output = tf.squeeze(output)
  return tf.keras.Model(inputs=a, outputs=output)

model.summary() 的输出

只需使用 Activation,这是更标准和通用的做法。

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
def create_keras_model_embedding():
  l = tf.keras.layers
  a = l.Input(shape=(784,))
  embedded_lookup_feature = tf.feature_column.numeric_column('x', shape=(784))
  dense_features = l.DenseFeatures(embedded_lookup_feature)({'x': a})#{'x': a}
  dense = l.Dense(784)(dense_features)
  dense_2 = l.Dense(10, kernel_initializer='zeros')(dense)
  output = l.Activation('softmax')(dense_2)
  return tf.keras.Model(inputs=a, outputs=output)

model = create_keras_model_embedding()
model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 784)]             0         
_________________________________________________________________
dense_features_2 (DenseFeatu (None, 784)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 784)               615440    
_________________________________________________________________
dense_5 (Dense)              (None, 10)                7850      
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 623,290
Trainable params: 623,290
Non-trainable params: 0
_________________________

如果要使用softmax,请使用axis=-1

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
def create_keras_model_embedding():
  l = tf.keras.layers
  a = l.Input(shape=(784,))
  embedded_lookup_feature = tf.feature_column.numeric_column('x', shape=(784))
  dense_features = l.DenseFeatures(embedded_lookup_feature)({'x': a})#{'x': a}
  dense = l.Dense(784)(dense_features)
  dense_2 = l.Dense(10, kernel_initializer='zeros')(dense)
  output = l.Softmax(axis=-1)(dense_2)
  return tf.keras.Model(inputs=a, outputs=output)

model = create_keras_model_embedding()
model.summary()
Model: "model_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         [(None, 784)]             0         
_________________________________________________________________
dense_features_3 (DenseFeatu (None, 784)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 784)               615440    
_________________________________________________________________
dense_7 (Dense)              (None, 10)                7850      
_________________________________________________________________
softmax_1 (Softmax)          (None, 10)                0         
=================================================================
Total params: 623,290
Trainable params: 623,290
Non-trainable params: 0