def load_weights(self, dump_path): if self.name is None: self.name = '{}_{}'.format(self.type, get_layer_num(self.type)) inc_layer_num(self.type) read_path = path.join(dump_path, self.name + '.pickle') with open(read_path, 'rb') as r: dump_cache = pickle.load(r) self.cache = dump_cache['cache'] self.grads = dump_cache['grads'] self.momentum_cache = dump_cache['momentum'] self.rmsprop_cache = dump_cache['rmsprop']
def forward_propagate(self, X, save_cache=False): if self.name is None: self.name = '{}_{}'.format(self.type, get_layer_num(self.type)) inc_layer_num(self.type) if 'W' not in self.params: self.params['W'], self.params['b'] = he_normal((X.shape[0], self.units)) Z = np.dot(self.params['W'], X) + self.params['b'] if save_cache: self.cache['A'] = X return Z
def forward_propagate(self, X, save_cache=False): ''' :param X: :param save_cache: :return: ''' if self.name is None: self.name = '{}_{}'.format(self.type, get_layer_num(self.type)) inc_layer_num(self.type) (num_data_points, prev_height, prev_width, prev_channels) = X.shape filter_shape_h, filter_shape_w = self.params['kernel_shape'] if 'W' not in self.params: shape = (filter_shape_h, filter_shape_w, prev_channels, self.params['filters']) self.params['W'], self.params['b'] = glorot_uniform(shape=shape) if self.params['padding'] == 'same': pad_h = int(((prev_height - 1) * self.params['stride'] + filter_shape_h - prev_height) / 2) pad_w = int(((prev_width - 1) * self.params['stride'] + filter_shape_w - prev_width) / 2) n_H = prev_height n_W = prev_width else: pad_h = 0 pad_w = 0 n_H = int( (prev_height - filter_shape_h) / self.params['stride']) + 1 n_W = int( (prev_width - filter_shape_w) / self.params['stride']) + 1 self.params['pad_h'], self.params['pad_w'] = pad_h, pad_w Z = np.zeros(shape=(num_data_points, n_H, n_W, self.params['filters'])) X_pad = pad_inputs(X, (pad_h, pad_w)) for i in range(num_data_points): x = X_pad[i] for h in range(n_H): for w in range(n_W): vert_start = self.params['stride'] * h vert_end = vert_start + filter_shape_h horiz_start = self.params['stride'] * w horiz_end = horiz_start + filter_shape_w for c in range(self.params['filters']): x_slice = x[vert_start:vert_end, horiz_start:horiz_end, :] Z[i, h, w, c] = self.conv_single_step( x_slice, self.params['W'][:, :, :, c], self.params['b'][:, :, :, c]) if save_cache: self.cache['A'] = X return Z