python - TensorFlow:实现网络,如果一层的特征图没有连接到下一层的所有特征图
问题描述
我正在尝试在 tensorflow 中实现 LeNet-5,如http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf中所述。
我在定义 C3(第 7 页最后一段-第 8 页第一段)时遇到了一点麻烦,因为我不知道如何具体告诉网络 S2 中的哪些特征图与 C3 中的哪些特征图相连(即我只知道如何连接所有特征图)。
我的代码是:
def LeNet(x):
# Hyperparameters for initliazitation of weights
mu = 0
sigma = 0.1
#This is the first convolutional layer C1
#Initialize weights for the first convolutional layer. 6 feature maps connected to
#one (1) 5x5 neighborhood in the input. 5*5*1*6=150 trainable parameters
C1_w = tf.Variable(tf.truncated_normal(shape = [5,5,1,6],mean = mu, stddev = sigma))
#Bias for each feature map. 6 parameters, with the weights we have 156 parameters
C1_b = tf.Variable(tf.zeros(6))
#Define the convolution layer with the weights and biases defined.
C1 = tf.nn.conv2d(x, C1_w, strides = [1,1,1,1], padding = 'VALID') + C1_b
#LeCun uses a sigmoidal activation function here.
#This is the sub-sampling layer S2
#Subsampling (also known as average pooling) with 2x2 receptive fields. 12 parameters.
S2 = tf.nn.avg_pool(C1, ksize = [1,2,2,1], strides = [1,2,2,1], padding = 'VALID')
#The result is passed to a sigmoidal function
S2 = tf.nn.sigmoid(S2)
#Another convolutional layer C3.
#Initlialize weights. 16 feature maps connected connected to 5*5 neighborhoods
C3_w = tf.Variable(tf.truncated_normal(shape = [5,5,6,16], mean = mu, stddev = sigma)) #This is the line I would want to change.
C3_b = tf.Variable(tf.zeros(16))
正确知道代码正在运行(当然,附加了其余代码,只显示了重要部分),但我没有按照论文中的描述进行操作,我想更密切地关注它。我在 C3 中有 5x5x6x16=2400+16=2416 个可训练参数,网络在这里应该有 1516 个可训练参数。
也许可以将 C3_w 定义为一个矩阵,其中一些值是 tf.constants 而一些是 tf.Variables?一个人会怎么做?
更新#1:
好的,我正在尝试使用示例中的拆分功能。我想做以下事情:
split1, split2 = tf.split(C3_w, [10, 6], axis=1)
也就是说,沿第一个维度拆分 [10, 6] (因为我的张量是 [5, 5, 6, 16] 。但这向我显示了这些错误:
ValueError: Sum of output sizes must match the size of the original Tensor along the split dimension or the sum of the positive sizes must be less if it contains a -1 for 'split' (op: 'SplitV') with input shapes: [5,5,6,16], [2], [] and with computed input tensors: input[1] = <10 6>, input[2] = <1>.
更新#2
即使更新#1 中的代码正常工作,我想我也不会实施本文中描述的过程。我将在该维度旁边采用“第一个”10 个连接并丢弃“下一个”6 个。这不是论文中的做法(参见第 8 页中的表 I,稍微复杂一点。
解决方案
只需根据需要将特征图拆分为多个变量tf.split
。然后,您有单独的变量可以输入下一个适当的层。通过这样的操作,Backprop 可以很好地工作。
我不知道这篇论文的细节,但如果你要在一个轨道上处理整个特征图,并将分割的特征图输入其他轨道,它会同样有效,所有这些场景都可以很好地工作。
推荐阅读
- typescript - 带有组合 API 和 TypeScript 的 Vue 3 注入插件
- python - BeautifulSoup 试图从列表中删除 HTML 数据
- java - 使用 Java 的 Selenium 代码中没有此类元素异常
- video - 如何在 Adobe After Effects 中编写 LaTeX 和绘图功能?
- google-sheets - 列匹配字符串时的参考范围
- python - 我使用 python 烧瓶创建简单的登录表单,但我无法重定向到我的主页,
- arrays - 结构 C 数组的用户输入
- laravel - 数据为空时如何显示反馈消息
- git - (libgit2) 确定 push/fetch refspec 是否需要强制
- typescript - 如何在不导入 firebase 模块的情况下访问 Firebase 类型?