小编Ell*_*ale的帖子

在 tensorflow 模型中关闭 softmax

我想要做的就是下载 tensorflow 的内置模型之一(通过 keras),关闭输出层的 softmax(即用线性激活函数替换它),以便我的输出特征是输出层之前的激活应用了 softmax。

所以,我把 VGG16 作为模型,并称之为 base_model

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
base_model = VGG16()
Run Code Online (Sandbox Code Playgroud)

我看看最后一层是这样的:

base_model.get_layer('predictions').get_config()
Run Code Online (Sandbox Code Playgroud)

并得到:

{'name': 'predictions',
 'trainable': True,
 'dtype': 'float32',
 'units': 1000,
 'activation': 'softmax',
 'use_bias': True,
 'kernel_initializer': {'class_name': 'GlorotUniform',
  'config': {'seed': None, 'dtype': 'float32'}},
 'bias_initializer': {'class_name': 'Zeros', 'config': {'dtype': 'float32'}},
 'kernel_regularizer': None,
 'bias_regularizer': None,
 'activity_regularizer': None,
 'kernel_constraint': None,
 'bias_constraint': None}
Run Code Online (Sandbox Code Playgroud)

然后,我这样做是为了切换激活功能:

base_model.get_layer('predictions').activation=tf.compat.v1.keras.activations.linear
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

base_model.get_layer('predictions').get_config()
Run Code Online (Sandbox Code Playgroud)

给出:

{'name': 'predictions',
 'trainable': True,
 'dtype': 'float32',
 'units': 1000,
 'activation': 'linear',
 'use_bias': True,
 'kernel_initializer': …
Run Code Online (Sandbox Code Playgroud)

python predict keras tensorflow transfer-learning

2
推荐指数
1
解决办法
732
查看次数

标签 统计

keras ×1

predict ×1

python ×1

tensorflow ×1

transfer-learning ×1