python - 如何将 tanh 添加到 keras 中的一个嵌入层

标签 python tensorflow machine-learning keras

我想使用 keras 功能 api 将一个 tanh 层添加到嵌入层:

x=layers.Embedding(vocab_size, 8, input_length=max_length)(input)
output=keras.activations.tanh(x)

model = Model(inputs=input, outputs=output)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(data, labels)

但是系统告诉我我必须使用keras层,而不是张量。我搜索了很多keras教程。解决这个问题只有一种方法:

model.add(Activation('tanh'))

但它是我不想使用的顺序模型。有没有一些方法可以使用函数式 api 来解决这个问题?

最佳答案

使用函数式 API,它几乎与顺序模型相同:

output = Activation('tanh')(x)

关于python - 如何将 tanh 添加到 keras 中的一个嵌入层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807153/

相关文章:

Python - 从文件加载 JSON 不起作用

python - 将负数、NaN 和 0 替换为下一个和上一个正数的平均值

python - 将 None 传递给 DRF SerializerField 的 to_representation

TensorFlow 可与 Slurm Interactive Session 配合使用,但不适用于 Slurm Job

python - 了解 LSTM - 层数据维度

python - Pandas 根据频率表绘制密度图

python - TensorFlow,什么时候可以使用类似 Python 的负索引?

python - Tensorflow:如何在调用 tf.reduce_sum 时忽略数组的一部分

machine-learning - 有人会如何创建一种机器学习算法来从书籍/小说中提取说话者?

python - 正确训练 Stacked Autoencoder