def backward(self): if self.pool_method == "MAX": self.preLayer.dA = util.max_pooling_backprop( self.preLayer.A.shape, self.dA, self.pos_tuple) else: self.preLayer.dA = util.avg_pooling_backprop( self.preLayer.A.shape, self.pool_size, self.dA, self.stride, self.padding)
def forward(self): if self.pool_method == "MAX": max_out, max_pos = util.pool_forward_max_argmax_tf_v( self.preLayer.A, self.pool_size, self.stride, self.padding) self.A = max_out self.pos_tuple = max_pos else: self.A = util.pool_forward_tf(self.preLayer.A, self.pool_size, self.stride, self.padding, "AVG")
def backward(self): if (self.isOutputLayer()): self.dZ = self.deriveLossByZ() else: # self.dA = np.matmul(self.nextLayer.W0.T, self.nextLayer.dZ) self.dZ = self.dA * self.actva.derivative(self) self.M = self.Z.shape[1] #样本数 pre_dA = np.matmul(self.W.T, self.dZ) #应该是这样的,先放这里,后续再改 if isinstance(self.preLayer, ConvLayer) or isinstance( self.preLayer, PoolLayer): # self.preLayer.dA = pre_dA.reshape(self.preLayer.A.shape) self.preLayer.dA = util.flattened_to_matrix( pre_dA, self.preLayer.A.shape) self.dW = (1 / self.M) * np.matmul( self.dZ, self.preLayer.flattened_A.T) #上一层是矩阵,要用扁平化的数组 else: self.preLayer.dA = pre_dA self.dW = (1 / self.M) * np.matmul(self.dZ, self.preLayer.A.T) #L2 regularization if (HyperParameter.L2_Reg): L2_dW = (HyperParameter.L2_lambd / self.M) * self.W self.dW = self.dW + L2_dW self.dB = (1 / self.M) * np.sum(self.dZ, axis=1, keepdims=True) self.W = self.W - HyperParameter.alpha * self.dW self.B = self.B - HyperParameter.alpha * self.dB
def initialize(self): self.pool_method = self.pool_dict["method"] self.pool_size = self.pool_dict["size"] #卷积核的大小,为一个正方形 self.stride = self.pool_dict["stride"] self.padding = self.pool_dict["padding"] h, w = util.getPadedOutShape(self.preLayer.A1_shape, self.pool_size, self.stride, self.padding) self.A1_shape = (h, w, self.preLayer.A1_shape[2]) self.A1_size = h * w * self.preLayer.A1_shape[2]
def forward(self): preA = self.preLayer.A # preA1_size = self.preLayer.A1_size if isinstance(self.preLayer, ConvLayer) or isinstance( self.preLayer, PoolLayer): flattened_preA = util.matrix_to_flattened(preA) #上一层是矩阵,要用扁平化的数组 self.preLayer.flattened_A = flattened_preA self.Z = np.matmul(self.W, flattened_preA) + self.B #(n,m) self.A = self.actva.activate(self.Z) elif isinstance(self.preLayer, FCCLayer): self.Z = np.matmul(self.W, preA) + self.B #(n,m) self.A = self.actva.activate(self.Z)
def backward(self): self.dZ = self.dA * self.actva.derivative(self) self.preLayer.dA = util.conv_backprop_input_tf( self.preLayer.A.shape, self.W, self.dZ, self.filter_dict["stride"], self.filter_dict["padding"]) self.dW = util.conv_backprop_filter_tf(self.preLayer.A, self.W.shape, self.dZ, self.filter_dict["stride"], self.filter_dict["padding"]) batch = self.dZ.shape[0] self.dB = (np.sum(self.dZ, axis=(0, 1, 2)).reshape( self.B.shape)) / batch #L2 regularization if (HyperParameter.L2_Reg): L2_dW = (HyperParameter.L2_lambd / batch) * self.W self.dW = self.dW + L2_dW self.W = self.W - HyperParameter.alpha * self.dW self.B = self.B - HyperParameter.alpha * self.dB
def initialize(self): channels = self.preLayer.A1_shape[2] size = self.filter_dict["size"] #卷积核的大小,为一个正方形 filter_count = self.filter_dict["count"] #卷积后的通道数 #就是过滤器filter的矩阵,实质就是权重参数 self.W = np.random.randn(size, size, channels, filter_count) * 0.01 #确定B的shape,这其实也是本层输出的shape stride = self.filter_dict["stride"] padding = self.filter_dict["padding"] h, w = util.getPadedOutShape(self.preLayer.A1_shape, size, stride, padding) self.A1_shape = (h, w, filter_count) self.A1_size = h * w * filter_count # self.B = np.random.randn(h,w,filter_count) #初始化B self.B = np.zeros((1, 1, filter_count), dtype=float) #初始化B
def forward(self): stride = self.filter_dict["stride"] padding = self.filter_dict["padding"] preA = self.preLayer.A self.Z = util.conv_forward_tf(preA, self.W, stride, padding) + self.B self.A = self.actva.activate(self.Z)