小编har*_*nen的帖子

如何使用可选输入创建Keras模型

我正在寻找一种使用可选输入创建Keras模型的方法。在原始TensorFlow中,您可以使用以下可选输入创建占位符:

import numpy as np
import tensorflow as tf


def main():
    required_input = tf.placeholder(
        tf.float32,
        shape=(None, 2),
        name='required_input')

    default_optional_input = tf.random_uniform(
        shape=(tf.shape(required_input)[0], 3))
    optional_input = tf.placeholder_with_default(
        default_optional_input,
        shape=(None, 3),
        name='optional_input')

    output = tf.concat((required_input, optional_input), axis=-1)

    with tf.Session() as session:
        with_optional_input_output_np = session.run(output, feed_dict={
            required_input: np.random.uniform(size=(4, 2)),
            optional_input: np.random.uniform(size=(4, 3)),
        })

        print(f"with optional input: {with_optional_input_output_np}")

        without_optional_input_output_np = session.run(output, feed_dict={
            required_input: np.random.uniform(size=(4, 2)),
        })

        print(f"without optional input: {without_optional_input_output_np}")


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

以类似的方式,我希望能够为Keras模型提供可选输入。似乎keras.layers.Input .__ init__中tensor参数可能正是我想要的,但至少它没有按我的预期工作(即,与上面所示的方式相同)。这是一个中断的示例:tf.placeholder_with_default

import …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

8
推荐指数
1
解决办法
549
查看次数

使用TensorFlow图层的"kernel_constraint"实现权重规范化

一些TensorFlow层,如tf.layers.densetf.layers.conv2d,接受一个kernel_constraint参数,根据tf api docs文档实现了

由优化器更新后应用于内核的可选投影函数(例如,用于实现层权重的范数约束或值约束).

[1]中,Salimans等人.提出了一种神经网络归一化技术,称为权重归一化,它对网络层的权重向量进行归一化,与例如批量归一化[2]形成对比,后者归一化流经该层的实际数据批量.在某些情况下,权重归一化方法的计算开销较低,并且还可以在使用批量归一化不可行的情况下使用.

我的问题是:是否可以使用上述TensorFlow层实现权重标准化kernel_constraint?假设x是一个带有形状的输入(batch, height, width, channels),我想我可以按如下方式实现它:

x = tf.layers.conv2d(
    inputs=x,
    filters=16,
    kernel_size=(3, 3),
    strides=(1, 1),
    kernel_constraint=lambda kernel: (
        tf.nn.l2_normalize(w, list(range(kernel.shape.ndims-1)))))
Run Code Online (Sandbox Code Playgroud)

什么是验证/使我的解决方案无效的简单测试用例?

[1] SALIMANS,蒂姆; KINGMA,Diederik P.体重标准化:一种简单的重新参数化,可加速深层神经网络的训练.在:神经信息处理系统的进展.2016.p.901-909.

[2] IOFFE,谢尔盖; SZEGEDY,Christian.批量标准化:通过减少内部协变量偏移来加速深度网络训练.arXiv preprint arXiv:1502.03167,2015.

python neural-network deep-learning tensorflow

6
推荐指数
1
解决办法
1379
查看次数