class MLP: def __init__(self, input, label, n_in, n_hidden, n_out, rng=None): self.x = input self.y = label if rng is None: rng = numpy.random.RandomState(1234) # construct hidden_layer self.hidden_layer = HiddenLayer(input=self.x, n_in=n_in, n_out=n_hidden, rng=rng, activation=tanh) # construct log_layer self.log_layer = LogisticRegression(input=self.hidden_layer.output, label=self.y, n_in=n_hidden, n_out=n_out) def train(self): # forward hidden_layer layer_input = self.hidden_layer.forward() self.log_layer.train(input=layer_input) # backward hidden_layer self.hidden_layer.backward(prev_layer=self.log_layer) def predict(self, x): x = self.hidden_layer.output(input=x) return self.log_layer.predict(x)
def __init__(self, N, label, n_hidden, n_out, image_size, channel, n_kernels, kernel_sizes, pool_sizes, rng=None, activation=ReLU): if rng is None: rng = numpy.random.RandomState(1234) self.N = N self.n_hidden = n_hidden self.n_kernels = n_kernels self.pool_sizes = pool_sizes self.conv_layers = [] self.conv_sizes = [] # construct 1st conv_layer conv_layer0 = ConvPoolLayer(N, image_size, channel, n_kernels[0], kernel_sizes[0], pool_sizes[0], rng, activation) self.conv_layers.append(conv_layer0) conv_size = [ (image_size[0] - kernel_sizes[0][0] + 1) / pool_sizes[0][0], (image_size[1] - kernel_sizes[0][1] + 1) / pool_sizes[0][1] ] self.conv_sizes.append(conv_size) # construct 2nd conv_layer conv_layer1 = ConvPoolLayer(N, conv_size, n_kernels[0], n_kernels[1], kernel_sizes[1], pool_sizes[1], rng, activation) self.conv_layers.append(conv_layer1) conv_size = [ (conv_size[0] - kernel_sizes[1][0] + 1) / pool_sizes[1][0], (conv_size[1] - kernel_sizes[1][0] + 1) / pool_sizes[1][1] ] self.conv_sizes.append(conv_size) # construct hidden_layer self.hidden_layer = HiddenLayer( None, n_kernels[-1] * conv_size[0] * conv_size[1], n_hidden, None, None, rng, activation) # construct log_layer self.log_layer = LogisticRegression(None, label, n_hidden, n_out)
def __init__(self, input=None, label=None, n_ins=2, hidden_layer_sizes=[3, 3], n_outs=2, rng=None): self.x = input self.y = label self.sigmoid_layers = [] self.dA_layers = [] self.n_layers = len(hidden_layer_sizes) # = len(self.rbm_layers) if rng is None: rng = numpy.random.RandomState(1234) assert self.n_layers > 0 # construct multi-layer for i in xrange(self.n_layers): # layer_size if i == 0: input_size = n_ins else: input_size = hidden_layer_sizes[i - 1] # layer_input if i == 0: layer_input = self.x else: layer_input = self.sigmoid_layers[-1].sample_h_given_v() # construct sigmoid_layer sigmoid_layer = HiddenLayer(input=layer_input, n_in=input_size, n_out=hidden_layer_sizes[i], rng=rng, activation=sigmoid) self.sigmoid_layers.append(sigmoid_layer) # construct dA_layers dA_layer = dA(input=layer_input, n_visible=input_size, n_hidden=hidden_layer_sizes[i], W=sigmoid_layer.W, hbias=sigmoid_layer.b) self.dA_layers.append(dA_layer) # layer for output using Logistic Regression self.log_layer = LogisticRegression( input=self.sigmoid_layers[-1].sample_h_given_v(), label=self.y, n_in=hidden_layer_sizes[-1], n_out=n_outs) # finetune cost: the negative log likelihood of the logistic regression layer self.finetune_cost = self.log_layer.negative_log_likelihood()
def __init__(self, input, label, n_in, n_hidden, n_out, rng=None): self.x = input self.y = label if rng is None: rng = numpy.random.RandomState(1234) # construct hidden_layer self.hidden_layer = HiddenLayer(input=self.x, n_in=n_in, n_out=n_hidden, rng=rng, activation=tanh) # construct log_layer self.log_layer = LogisticRegression(input=self.hidden_layer.output, label=self.y, n_in=n_hidden, n_out=n_out)
class CNN: def __init__(self, N, label, n_hidden, n_out, image_size, channel, n_kernels, kernel_sizes, pool_sizes, rng=None, activation=ReLU): if rng is None: rng = numpy.random.RandomState(1234) self.N = N self.n_hidden = n_hidden self.n_kernels = n_kernels self.pool_sizes = pool_sizes self.conv_layers = [] self.conv_sizes = [] # construct 1st conv_layer conv_layer0 = ConvPoolLayer(N, image_size, channel, n_kernels[0], kernel_sizes[0], pool_sizes[0], rng, activation) self.conv_layers.append(conv_layer0) conv_size = [ (image_size[0] - kernel_sizes[0][0] + 1) / pool_sizes[0][0], (image_size[1] - kernel_sizes[0][1] + 1) / pool_sizes[0][1] ] self.conv_sizes.append(conv_size) # construct 2nd conv_layer conv_layer1 = ConvPoolLayer(N, conv_size, n_kernels[0], n_kernels[1], kernel_sizes[1], pool_sizes[1], rng, activation) self.conv_layers.append(conv_layer1) conv_size = [ (conv_size[0] - kernel_sizes[1][0] + 1) / pool_sizes[1][0], (conv_size[1] - kernel_sizes[1][0] + 1) / pool_sizes[1][1] ] self.conv_sizes.append(conv_size) # construct hidden_layer self.hidden_layer = HiddenLayer( None, n_kernels[-1] * conv_size[0] * conv_size[1], n_hidden, None, None, rng, activation) # construct log_layer self.log_layer = LogisticRegression(None, label, n_hidden, n_out) # def train(self, epochs, learning_rate, input=None): def train(self, epochs, learning_rate, input, test_input=None): for epoch in xrange(epochs): if (epoch + 1) % 5 == 0: print 'iter = %d/%d' % (epoch + 1, epochs) print print '------------------' print 'TEST PROCESSING...' print self.predict(test_input) print '------------------' print # forward first conv layer pooled_X = self.conv_layers[0].forward(input=input) # forward second conv layer pooled_X = self.conv_layers[1].forward(input=pooled_X) # flatten input layer_input = self.flatten(pooled_X) # forward hidden layer layer_input = self.hidden_layer.forward(input=layer_input) # forward & backward logistic layer self.log_layer.train(lr=learning_rate, input=layer_input) # backward hidden layer self.hidden_layer.backward(prev_layer=self.log_layer, lr=learning_rate) flatten_size = self.n_kernels[-1] * self.conv_sizes[-1][ 0] * self.conv_sizes[-1][1] delta_flatten = numpy.zeros((self.N, flatten_size)) for n in xrange(self.N): for i in xrange(flatten_size): for j in xrange(self.n_hidden): delta_flatten[n][i] += self.hidden_layer.W[i][ j] * self.hidden_layer.d_y[n][j] # unflatten delta delta = numpy.zeros( (len(delta_flatten), self.n_kernels[-1], self.conv_sizes[-1][0], self.conv_sizes[-1][1])) for n in xrange(len(delta)): index = 0 for k in xrange(self.n_kernels[-1]): for i in xrange(self.conv_sizes[-1][0]): for j in xrange(self.conv_sizes[-1][1]): delta[n][k][i][j] = delta_flatten[n][index] index += 1 # backward second conv layer delta = self.conv_layers[1].backward(delta, self.conv_sizes[1], learning_rate) # backward first conv layer self.conv_layers[0].backward(delta, self.conv_sizes[0], learning_rate) def flatten(self, input): flatten_size = self.n_kernels[-1] * self.conv_sizes[-1][ 0] * self.conv_sizes[-1][1] flattened_input = numpy.zeros((len(input), flatten_size)) for n in xrange(len(flattened_input)): index = 0 for k in xrange(self.n_kernels[-1]): for i in xrange(self.conv_sizes[-1][0]): for j in xrange(self.conv_sizes[-1][1]): flattened_input[n][index] = input[n][k][i][j] index += 1 # print flattened_input return flattened_input def predict(self, x): pooled_X = self.conv_layers[0].forward(input=x) pooled_X = self.conv_layers[1].forward(input=pooled_X) layer_input = self.flatten(pooled_X) x = self.hidden_layer.output(input=layer_input) return self.log_layer.predict(x)