python - Keras 中的 "Could not interpret activation function identifier: 256"错误

标签 python tensorflow keras

我尝试运行以下代码,但出现错误。我是否遗漏了代码中的某些内容?

from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.activations import relu, elu, linear, sigmoid

def build_fc_model(layers):
    fc_model = Sequential()
    for i in range(len(layers)-1):
        fc_model.add( Dense(layers[i],layers[i+1]) )#, W_regularizer=l2(0.1)) )
        fc_model.add( Dropout(0.5) )
        if i < (len(layers) - 2):
            fc_model.add( Activation('relu') )
    fc_model.summary()
    return fc_model
fc_model_1 = build_fc_model([2, 256, 512, 1024, 1])

这是错误消息:

TypeError: Could not interpret activation function identifier: 256

最佳答案

此错误表明,您定义了一个不可解释的激活函数。在密集层的定义中,您传递了两个参数:layers[i]layers[i+1]

基于文档 here对于Dense函数: 第一个参数是单元(神经元)的数量,第二个参数是激活函数。因此,它将layers[i+1]视为Dense函数无法识别的激活函数。

推论: 您不需要将下一层神经元传递到密集层。因此,删除layers[i+1]参数。

此外,您必须为模型定义一个输入层,并将模型的输入形状传递给它。

因此,修改后的代码应该是这样的:

from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.activations import relu, elu, linear, sigmoid
from keras.layers import InputLayer  #import input layer 

def build_fc_model(layers):
    fc_model = Sequential()
    fc_model.add(InputLayer(input_shape=(784,))) #add input layer and specify it's shape
    for i in range(len(layers)-1):
        fc_model.add( Dense(layers[i]) ) #remove unnecessary second argument 
        if i < (len(layers) - 2):
            fc_model.add( Activation('relu') )
        fc_model.add( Dropout(0.5) )
    fc_model.summary()
    return fc_model
fc_model_1 = build_fc_model([2, 256, 512, 1024, 1])

关于python - Keras 中的 "Could not interpret activation function identifier: 256"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67840364/

相关文章:

python - 无法为 xml 下载创建 HttpResponse,Django

python - 如何正确使用 tf.scatter_update 进行 N 维更新?

tensorflow - 使用新图像更新 Tensorflow 对象检测模型

python - C++ 项目中嵌入的 TensorFlow NN

python - 有没有keras方法来拆分数据?

python - 用于将 Python 多行字符串与转义字符匹配的正则表达式

python - 读取配置文件时出现 ConfigParser.MissingSectionHeaderError Python

python - SWIG:未定义类型映射

python - 使用 TensorFlow 后端。导入 tensorflow 时出错

json - 使用keras在json中转储cnn的权重