【问题标题】:Can I write a custom layer in tensorflow in python with existing ops such as conv2d and tensor operations?我可以使用现有的操作(例如 conv2d 和张量操作)在 python 的 tensorflow 中编写自定义层吗?
【发布时间】:2016-04-05 13:05:13
【问题描述】:

如标题所述,我想在 tensorflow 中实现一个自定义层,其中包含现有的 ops 和张量操作。我想知道我是否可以像在 theano 中一样在 python 中做到这一点。在这一层,输入可能是一些矩阵,批量输入和一些权重和偏差需要学习。经过一些张量操作后,输出将被馈送到下一层。这一层的计算可能比较复杂,不知道tensorflow能不能帮我做auto-diff。

【问题讨论】:

    标签: tensorflow deep-learning


    【解决方案1】:

    如果您的图层是现有操作的组合,那么它肯定会起作用。这就是例如 TF-Slim 的工作原理。

    # Skeleton code, just to demonstrate the concept
    def conv(input, ...):
        kernel = tf.Variable(...)
        tmp = tf.nn.conv2d(input, kernel, ...)
        bias = tf.Variable(...)
        tmp = tf.nn.bias_add(tmp, bias, ...)
        return tf.nn.relu(tmp, ...)
    

    定义一个函数,为您提供一个“集成”层,该层执行基本卷积层的常规步骤,然后您可以将其用作

    layer_1 = conv(input, ...)
    layer_2 = conv(layer_1, ...)
    

    等等。只要您只是编写具有渐变的操作,自动微分就可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-13
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多