如何检查Keras中的lambda层是否被捕捉 - Tensorflow

2024-06-16 11:47:08 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,当我在Keras中运行以下代码时,模型起作用:

def transformer_code(inputLayer):
    hparams = transformer.transformer_base()
    encoder=transformer.TransformerEncoder(hparams,  mode=tf.estimator.ModeKeys.TRAIN)
    x = keras.backend.expand_dims(inputLayer, axis=2)
    y = encoder({"inputs": x, "targets": 0, "target_space_id": 0})
    y = keras.backend.squeeze(y[0], 2)
    return y

def trainModel(args, trainInput, trianOutput, testInput, testOutput, taskName, tags):

inputLayer = keras.layers.Input(shape=(len(trainInput[0]), len(trainInput[0][0])), dtype='float32') inputAfterDense = keras.layers.Dense(512, activation='relu')(inputLayer) crfLayer = CRF(len(tags), sparse_target=True, name='result') y = keras.layers.Lambda(transformer_code)(inputAfterDense ) modelPred = crfLayer(y) model = keras.Model(inputs=inputLayer, outputs=modelPred) model.compile( optimizer='adam', loss = {'result': crfLayer.loss_function}, metrics={'result': crfLayer.accuracy} ) print 'finish model setting' print model.summary()

但如果我去掉致密层

inputAfterDense=路缘石.层.密(512,activation='relu')(输入层)

训练中断,预测精度一直接近零。在

我怀疑lambda层没有被Keras抓到?我怎么检查?在


Tags: encodermodellenlayersdefcoderesultkeras
1条回答
网友
1楼 · 发布于 2024-06-16 11:47:08

我不确定您现在是否已经解决了您的问题,但它可能是Lambda层不是最合适的变压器层类型。根据文件:

For simple, stateless custom operations, you are probably better off using layers.core.Lambda layers. But for any custom operation that has trainable weights, you should implement your own layer.

因此,最好编写自己的Keras层。在

相关问题 更多 >