我现在正试图在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))正确地知道,代码是工作的(当然,随附的其余代码,只是显示重要的部分),但我没有做论文所描述的,我会想要更密切地跟踪它。我有5x5x6x16=2400+16=2416可训练参数在C3和网络应该有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页的表一,稍微复杂一点)。
发布于 2018-05-16 16:04:58
只需酌情使用tf.split将功能映射拆分为多个变量即可。然后,您就有了独立的变量,这些变量输入到下一个适当的层。通过这样的操作,Backprop将非常好地工作。
我不知道论文的细节,但是如果你用一个音轨处理整个特征映射,并将分割的功能映射放到其他轨道上,那么所有这些场景都会很好地工作。
https://stackoverflow.com/questions/50374236
复制相似问题