我可以在 python 中使用现有操作(例如 conv2d 和张量操作)在 tensorflow 中编写自定义层吗?
Can I write a custom layer in tensorflow in python with existing ops such as conv2d and tensor operations?
如标题所述,我想用现有的ops和张量操作在tensorflow中实现一个自定义层。我想知道我是否可以在 python 中做到这一点,就像在 theano 中可以做到的那样。在这一层中,输入可能是一些矩阵,批量输入和一些权重和偏差需要学习。经过一些张量操作后,输出将被馈送到下一层。这一层的计算可能比较复杂,不知道tensorflow能不能帮我做auto-diff
如果您的层是现有操作的组合,它肯定会正常工作。这就是 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, ...)
定义了一个函数,它为您提供了一个 "integrated" 层,该层执行基本卷积层的常规步骤,然后您可以将其用作
layer_1 = conv(input, ...)
layer_2 = conv(layer_1, ...)
等等。只要您只是编写具有渐变的操作,自动微分就会起作用。
如标题所述,我想用现有的ops和张量操作在tensorflow中实现一个自定义层。我想知道我是否可以在 python 中做到这一点,就像在 theano 中可以做到的那样。在这一层中,输入可能是一些矩阵,批量输入和一些权重和偏差需要学习。经过一些张量操作后,输出将被馈送到下一层。这一层的计算可能比较复杂,不知道tensorflow能不能帮我做auto-diff
如果您的层是现有操作的组合,它肯定会正常工作。这就是 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, ...)
定义了一个函数,它为您提供了一个 "integrated" 层,该层执行基本卷积层的常规步骤,然后您可以将其用作
layer_1 = conv(input, ...)
layer_2 = conv(layer_1, ...)
等等。只要您只是编写具有渐变的操作,自动微分就会起作用。