python - 使用 Keras Sequential 模型实现快捷方式

标签 python tensorflow machine-learning keras deep-learning

我已经通过 Keras 功能模型实现了快捷方式:

inputs = ...

# shortcut path
shortcut = ShortcutLayer()(inputs)

# main path
outputs = MainLayer()(inputs)

# add main and shortcut together
outputs = Add()([outputs, shortcut])

是否可以将其转换为顺序模型,这样我就不需要提前知道输入

基本上,我想要实现的目标如下:

def my_model_with_shortcut():
    # returns a Sequential model equivalent to the functional one above

model = my_model_with_shortcut()

inputs = ...
outputs = model(inputs)

最佳答案

我会尝试以下操作;

def my_model_with_shortcut():
    def _create_shortcut(inputs):
         # here create model as in case you know inputs, e.g.:
         aux = Dense(10, activation='relu')(inputs)
         output = Dense(10, activation='relu')(aux)
         return output  
    return _create_shortcut

现在您的场景应该可行了。

关于python - 使用 Keras Sequential 模型实现快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212464/

相关文章:

python - 在 Python 中获取相差 N 或更多的最小坐标

python - tf.train.range_input_producer(epoch_size, shuffle=True) 不会终止也不会引起 CPU/GPU 负载

python - Tensorflow 不使用 GPU

python - ValueError : Error when checking target: expected dense_19 to have 3 dimensions, 但得到形状为 (5, 3) 的数组

python - 使用 one-hot 编码处理 sklearn 中的分类变量

python - 上传 Csv 文件 Google colab

python - 具有数据初始化的 CSV 列的 JSON 键值

Python:如何使 ANSI 转义码也能在 Windows 中工作?

python - 在 Python 和 IronPython 之间传递 pickle

python - 分类拟合得到ValueError : setting an array element with a sequence