TensorFlow Keras Softmax层输出相对于输入具有一维尺寸

问题描述 投票:0回答:1

我有一个以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()的输出summary output

python tensorflow keras tensorflow2.0 softmax
1个回答
1
投票

仅使用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
_________________________
© www.soinside.com 2019 - 2024. All rights reserved.