【问题标题】:tf.keras.models.clone_model unknown activation: ReLUtf.keras.models.clone_model 未知激活:ReLU
【发布时间】:2020-06-03 19:36:33
【问题描述】:

环境是Python 3.7.6,下面是我的导入:

import os, sys
import tensorflow as tf    # v2.2.0
tf.compat.v1.enable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
from sys import platform
import time
import random
import pickle
from tensorflow.keras.layers import ReLU

我试图克隆一个 tf.keras.Model 没有成功,因为 ReLU 是一个未知的激活。尽管如此,启动是成功的,所以系统应该知道 ReLU 是什么。我想知道如何解决这个问题。

def init_model(D=8, W=256):
    # This is a simple MLP neural network
    # D: The number of layers
    # H: The neurons in each layer
    relu = ReLU()
    dense = lambda W=W, act=relu: tf.keras.layers.Dense(W, activation=act, dtype='float32')

    inputs = tf.keras.Input(shape=(3 + 3 * 2 * L_embed))
    outputs = inputs
    for i in range(D):
        outputs = dense()(outputs)
        if i % 4 == 0 and i > 0:
            outputs = tf.concat([outputs, inputs], -1)
    outputs = dense(4, act=None)(outputs)

    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

然后我打电话给:

model_inner_copy = tf.keras.models.clone_model(model)

错误信息是:

  File "D:/Code Repository/Meta-NeRF/Code/NeRF/Tiny_MAML_NeRF.py", line 211, in train_maml_nerf
    model_inner_copy = tf.keras.models.clone_model(model)

  File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 427, in clone_model
    model, input_tensors=input_tensors, layer_fn=clone_function)

  File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 196, in _clone_functional_model
    model, new_input_layers, layer_fn)

  File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 246, in _clone_layers_and_model_config
    config = network.get_network_config(model, serialize_layer_fn=_copy_layer)

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\network.py", line 2119, in get_network_config
    layer_config = serialize_layer_fn(layer)

  File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 243, in _copy_layer
    created_layers[layer.name] = layer_fn(layer)

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 61, in _clone_layer
    return layer.__class__.from_config(layer.get_config())

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\base_layer.py", line 655, in from_config
    return cls(**config)

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\layers\core.py", line 1135, in __init__
    self.activation = activations.get(activation)

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\activations.py", line 465, in get
    identifier, printable_module_name='activation')

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 362, in deserialize_keras_object
    config, module_objects, custom_objects, printable_module_name)

File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
    raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown activation: ReLU

【问题讨论】:

  • 请将导入添加到代码中
  • 现在添加。谢谢提醒。

标签: python python-3.x tensorflow keras


【解决方案1】:

所以问题是tf.keras.layers.ReLU是一个实现ReLU激活的层,但它本身并不是一个激活函数。它旨在用作模型中的层,而不是 Dense 层的参数。

若要将函数作为激活函数作为参数提供给Dense,您应该使用tf.keras.activations.relu

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 2018-05-14
    • 2021-08-31
    • 1970-01-01
    • 2019-11-16
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多