def fprop(self, x, tparams = None): # Conv layer can have only one parent. # Later, we will extend to generalize this. # For now, we satisfy with fullyconnected layer # that can embed multiple conv layer parents # into same hidden space. x = unpack(x) parname, parshape = unpack(self.parent.items()) W = self.params['W_'+parname+'__'+self.name] z = T.nnet.conv2d( x.astype('float32'), W.astype('float32'), subsample=self.step_size, border_mode=self.border_mode ) ''' z += conv2d( x.astype('float32'), W.astype('float32')) ''' if self.tied_bias: z += self.params['b_'+self.name].dimshuffle('x', 0, 'x', 'x') else: z += self.params['b_'+self.name].dimshuffle('x', 0, 1, 2) z = self.nonlin(z) z.name = self.name return z
def fprop(self, x): x = unpack(x) z = max_pool_2d(x, self.pool_size, st=self.pool_stride) z.name = self.name return z
def initialize_set_shape(self): parname, parshape = unpack(self.parent.items()) # Shape should be (batch_size, num_channels, x, y) pool_size = totuple(self.pool_size) pool_stride = totuple(self.pool_stride) if self.ignore_border: newx = (parshape[2] - pool_size[0]) // pool_stride[0] + 1 newy = (parshape[3] - pool_size[1]) // pool_stride[1] + 1 else: if pool_stride[0] > pool_size[0]: newx = (parshape[2] - 1) // pool_stride[0] + 1 else: newx = max(0, (parshape[2] - 1 - pool_size[0]) // pool_stride[0] + 1) + 1 if pool_stride[1] > pool_size[1]: newy = (parshape[3] - 1) // pool_stride[1] + 1 else: newy = max(0, (parshape[3] - 1 - pool_size[1]) // pool_stride[1] + 1) + 1 outshape = (parshape[0], parshape[1], newx, newy) self.outshape = outshape
def initialize_set_shape(self): parname, parshape = unpack(self.parent.items()) # Shape should be (batch_size, num_channels, x, y) pool_size = totuple(self.pool_size) pool_stride = totuple(self.pool_stride) if self.ignore_border: newx = (parshape[2] - pool_size[0]) // pool_stride[0] + 1 newy = (parshape[3] - pool_size[1]) // pool_stride[1] + 1 else: if pool_stride[0] > pool_size[0]: newx = (parshape[2] - 1) // pool_stride[0] + 1 else: newx = max( 0, (parshape[2] - 1 - pool_size[0]) // pool_stride[0] + 1) + 1 if pool_stride[1] > pool_size[1]: newy = (parshape[3] - 1) // pool_stride[1] + 1 else: newy = max( 0, (parshape[3] - 1 - pool_size[1]) // pool_stride[1] + 1) + 1 outshape = (parshape[0], parshape[1], newx, newy) self.outshape = outshape
def fprop(self, x): x = unpack(x) x = T.cast(x, 'int32') z = T.zeros((x.shape[0], self.nout)) z = T.set_subtensor(z[T.arange(x.size) % x.shape[0], x.T.flatten()], 1) z.name = self.name return z
def initialize(self): parname, parshape = unpack(self.parent.items()) outshape = self.outshape filtershape = self.filtershape batch_size = parshape[0] nchannels = parshape[1] if filtershape is not None: nfilters = filtershape[1] if self.border_mode == 'valid': x = parshape[2] - filtershape[2] + 1 y = parshape[3] - filtershape[3] + 1 else: x = parshape[2] + filtershape[2] - 1 y = parshape[3] + filtershape[3] - 1 self.outshape = (batch_size, nfilters, x, y) else: nfilters = outshape[1] if self.border_mode == 'valid': x = parshape[2] - outshape[2] + 1 y = parshape[3] - outshape[3] + 1 elif self.border_mode == 'full': x = outshape[2] - parshape[2] + 1 y = outshape[3] - parshape[3] + 1 W_shape = (nfilters, nchannels, x, y) self.filtershape = W_shape W_name = 'W_'+parname+'__'+self.name self.alloc(self.init_W.get(self.filtershape, W_name)) b_name = 'b_'+self.name if self.tied_bias: b_shape = nfilters self.alloc(self.init_b.get(b_shape, b_name)) else: b_shape = (nfilters, x, y) self.alloc(self.init_b.get(b_shape, b_name))
def fprop(self, x): x = unpack(x) z = T.zeros((x.shape[0], self.nout)) for x, (parname, parout) in izip(X, self.parent.items()): W = self.params['W_'+parname+'__'+self.name] z += T.dot(x[:, :parout], W) z.name = self.name return z
def fprop(self, x): x = unpack(x) z = T.zeros((x.shape[0], self.nout)) for x, (parname, parout) in izip(X, self.parent.items()): W = self.params['W_' + parname + '__' + self.name] z += T.dot(x[:, :parout], W) z.name = self.name return z
def fprop(self, x): x = unpack(x) x = T.cast(x, 'int32') z = T.zeros((x.shape[0], self.nout)) z = T.set_subtensor( z[T.arange(x.size) % x.shape[0], x.T.flatten()], 1 ) z.name = self.name return z
def convert2matrix(self, x): x = unpack(x) # Assume that axes of x is always ('b', 'c', 'x', 'y') refaxes = ('b', 'c', 'x', 'y') newaxes = () for axis in self.axes: newaxes += (refaxes.index(axis),) x = x.dimshuffle(newaxes) z = x.reshape((x.shape[0], T.prod(x.shape[1:]))) z.name = self.name return z
def fprop(self, x): # Conv layer can have only one parent. # Later, we will extend to generalize this. # For now, we satisfy with fullyconnected layer # that can embed multiple conv layer parents # into same hidden space. x = unpack(x) parname, parshape = unpack(self.parent.items()) z = T.zeros(self.outshape) W = self.params['W_' + parname + '__' + self.name] z += conv2d(x, W, image_shape=parshape, subsample=self.step_size, border_mode=self.border_mode, filter_shape=self.filtershape) if self.tied_bias: z += self.params['b_' + self.name].dimshuffle('x', 0, 'x', 'x') else: z += self.params['b_' + self.name].dimshuffle('x', 0, 1, 2) z = self.nonlin(z) z.name = self.name return z
def fprop(self, x): # Conv layer can have only one parent. # Later, we will extend to generalize this. # For now, we satisfy with fullyconnected layer # that can embed multiple conv layer parents # into same hidden space. x = unpack(x) parname, parshape = unpack(self.parent.items()) z = T.zeros(self.outshape) W = self.params['W_'+parname+'__'+self.name] z += conv2d( x, W, image_shape=parshape, subsample=self.step_size, border_mode=self.border_mode, filter_shape=self.filtershape ) if self.tied_bias: z += self.params['b_'+self.name].dimshuffle('x', 0, 'x', 'x') else: z += self.params['b_'+self.name].dimshuffle('x', 0, 1, 2) z = self.nonlin(z) z.name = self.name return z
init_W=init_W, init_U=init_U, init_b=init_b) h4 = FullyConnectedLayer(name='h4', parent=['h1', 'h2', 'h3'], nout=res, unit='sigmoid', init_W=init_W, init_b=init_b) cost = MSELayer(name='cost', parent=['h4', 'y']) nodes = [h1, h2, h3, h4, cost] rnn = Net(inputs=inputs, inputs_dim=inputs_dim, nodes=nodes) cost = unpack(rnn.build_recurrent_graph(output_args=[cost])) cost = cost.mean() cost.name = 'cost' model.graphs = [rnn] optimizer = Adam( lr=0.001 ) extension = [ GradientClipping(batch_size=batch_size), EpochCount(100), Monitoring(freq=100, ddout=[cost]), Picklize(freq=200, path=save_path) ]
def train_prop(self, z): z = unpack(z) z.name = self.name return z * self.train_scale
def test_prop(self, z): z = unpack(z) z.name = self.name return z * self.test_scale
init_W=init_W, init_U=init_U, init_b=init_b) h4 = FullyConnectedLayer(name='h4', parent=['h1', 'h2', 'h3'], nout=res, unit='sigmoid', init_W=init_W, init_b=init_b) cost = MSELayer(name='cost', parent=['h4', 'y']) nodes = [h1, h2, h3, h4, cost] rnn = Net(inputs=inputs, inputs_dim=inputs_dim, nodes=nodes) cost = unpack(rnn.build_recurrent_graph(output_args=[cost])) cost = cost.mean() cost.name = 'cost' model.graphs = [rnn] optimizer = Adam(lr=0.001) extension = [ GradientClipping(batch_size=batch_size), EpochCount(100), Monitoring(freq=100, ddout=[cost]), Picklize(freq=200, path=save_path) ] mainloop = Training(name='toy_bb_gflstm', data=Iterator(trdata, batch_size),
def train_prop(self, z): z = unpack(z) z = dropout(z, self.p, self.theano_rng) z.name = self.name return z * self.train_scale
def convert2tensor4(self, x): x = unpack(x) z = x.reshape(self.outshape) z.name = self.name return z