def test_infer_shape(self): image = tensor.dtensor4() maxout = tensor.dtensor4() gz = tensor.dtensor4() rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3), (3, 2)) image_val = rng.rand(4, 6, 7, 9) out_shapes = [[[4, 6, 7, 9], [4, 6, 7, 9]], [[4, 6, 3, 4], [4, 6, 4, 5]], [[4, 6, 2, 3], [4, 6, 3, 3]], [[4, 6, 3, 3], [4, 6, 4, 3]], [[4, 6, 2, 4], [4, 6, 3, 5]]] for i, maxpoolshp in enumerate(maxpoolshps): for j, ignore_border in enumerate([True, False]): # checking shapes generated by DownsampleFactorMax self._compile_and_check([image], [DownsampleFactorMax(maxpoolshp, ignore_border=ignore_border)(image)], [image_val], DownsampleFactorMax) # checking shapes generated by DownsampleFactorMaxGrad maxout_val = rng.rand(*out_shapes[i][j]) gz_val = rng.rand(*out_shapes[i][j]) self._compile_and_check([image, maxout, gz], [DownsampleFactorMaxGrad(maxpoolshp, ignore_border=ignore_border)(image, maxout, gz)], [image_val, maxout_val, gz_val], DownsampleFactorMaxGrad)
def test_infer_shape_gradW(self): if theano.config.mode == "FAST_COMPILE": raise SkipTest("CorrMM don't work in FAST_COMPILE") def rand(*shape): r = numpy.asarray(numpy.random.rand(*shape), dtype="float64") return r * 2 - 1 corrMM = corr.CorrMM gradW = corr.CorrMM_gradWeights adtens = T.dtensor4() bdtens = T.dtensor4() aivec_vals = [[1, 5, 6, 3], [8, 2, 7, 3], [1, 6, 9, 4], [9, 6, 8, 5], [9, 1, 6, 8]] bivec_vals = [[7, 5, 3, 1], [4, 2, 5, 3], [12, 6, 3, 2], [5, 6, 1, 3], [11, 1, 3, 3]] modes = ["valid", "full", "half", (1, 1), (2, 1), (1, 2), 1] subsamples = [(1, 1), (2, 1), (1, 2)] for aivec_val, bivec_val in zip(aivec_vals, bivec_vals): adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) for mode in modes: for subsample in subsamples: # CorrMM cdtens = corrMM(border_mode=mode, subsample=subsample)(adtens, bdtens) f = theano.function([adtens, bdtens], cdtens) cdtens_val = f(adtens_val, bdtens_val) # CorrMM_gradWeights shape = (theano.shared(bivec_val[2]), theano.shared(bivec_val[3])) bdtens_g = gradW(border_mode=mode, subsample=subsample)(adtens, cdtens, shape=shape) self._compile_and_check([adtens, cdtens], [bdtens_g], [adtens_val, cdtens_val], gradW, warn=False)
def test_conv_no_bias(self): images = T.dtensor4('input_conv') weights = T.dtensor4('weights') images_internal = U2IConv(imshp=(12, 3, 256, 256), kshp=(12, 3, 3, 3))(images) convOut = Conv2D(imshp=(12, 3, 256, 256), kshp=(12, 3, 3, 3), filter_flip=False)(images_internal, weights) convOut_user = I2U()(convOut) convOutLoss = T.mean(convOut_user) conv_op_di = T.grad(convOutLoss, images) conv_op_dk = T.grad(convOutLoss, weights) convOutBack = [conv_op_di, conv_op_dk] ival = numpy.random.rand(12, 3, 256, 256).astype(numpy.float64) wval = numpy.random.rand(12, 3, 3, 3).astype(numpy.float64) fopt = theano.function(inputs=[images, weights], outputs=convOutBack, mode=mode_with_mkl) new_out = fopt(ival, wval) convOut = conv2d(images, weights, input_shape=(12, 3, 256, 256), filter_shape=(12, 3, 3, 3), filter_flip=False) convOutLoss = T.mean(convOut) conv_op_di = T.grad(convOutLoss, images) conv_op_dk = T.grad(convOutLoss, weights) convOutBack = [conv_op_di, conv_op_dk] fori = theano.function(inputs=[images, weights], outputs=convOutBack, mode=mode_without_mkl) old_out = fori(ival, wval) assert len(fopt.maker.fgraph.toposort()) != len(fori.maker.fgraph.toposort()) assert numpy.allclose(old_out[0], new_out[0]) assert new_out[0].dtype == 'float64'
def test_relu_grad(self): seed = utt.fetch_seed() rng = numpy.random.RandomState(seed) imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8)) n, c = 4, 2 axis = 1 image = T.dtensor4('image') image1 = T.dtensor4('image1') for imgsize in imgsize_list: imval = rng.rand(n, c, imgsize[0], imgsize[1]) out = T.concatenate([image, image1], axis) sum_ref = T.sum(out) gx_ref = T.grad(sum_ref, [image, image1]) f_ref = theano.function([image, image1], outputs=gx_ref, mode=mode_without_mkl) output_ref = f_ref(imval, imval) out_mkl = self.mkl_concatenate_func(axis, image, image1) sum_mkl = T.sum(out_mkl) gx_mkl = T.grad(sum_mkl, [image, image1]) f_mkl = theano.function([image, image1], outputs=gx_mkl) output_mkl = f_mkl(imval, imval) utt.assert_allclose(output_mkl, output_ref)
def test_conv_with_bias(self): images = T.dtensor4('inputs') weights = T.dtensor4('weights') bias = T.dvector('bias') ishape = [(8, 3, 256, 256), (16, 3, 256, 256), (32, 3, 256, 256), (64, 3, 256, 256)] wshape = [(8, 3, 3, 3), (16, 3, 3, 3), (32, 3, 3, 3), (64, 3, 3, 3)] for i, ish in enumerate(ishape): wsh = wshape[i] images_internal = U2IConv(imshp=ish, kshp=wsh)(images) convOutBias_internal = Conv2D(imshp=ish, kshp=wsh, filter_flip=False)(images_internal, weights, bias) convOutBias_user = I2U()(convOutBias_internal) ival = numpy.random.rand(*ish).astype(numpy.float64) wval = numpy.random.rand(*wsh).astype(numpy.float64) bval = numpy.random.rand(wsh[0]).astype(numpy.float64) fopt = theano.function(inputs=[images, weights, bias], outputs=convOutBias_user, mode=mode_with_mkl) new_old = fopt(ival, wval, bval) convOut = conv2d(images, weights, input_shape=ish, filter_shape=wsh, filter_flip=False) convOutBias = convOut + bias.dimshuffle('x', 0, 'x', 'x') fori = theano.function(inputs=[images, weights, bias], outputs=convOutBias, mode=mode_without_mkl) old_out = fori(ival, wval, bval) assert str(fopt.maker.fgraph.toposort()) != str(fori.maker.fgraph.toposort()) assert numpy.allclose(old_out, new_old)
def test_concatenate(self): def ref(*inputs): axis = inputs[0] tensors = inputs[1:] return numpy.concatenate(tensors, axis) seed = utt.fetch_seed() rng = numpy.random.RandomState(seed) imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8)) n, c = 4, 2 axis = 1 image = T.dtensor4('image') image1 = T.dtensor4('image1') for imgsize in imgsize_list: imval = rng.rand(n, c, imgsize[0], imgsize[1]) output_ref = ref(axis, imval, imval) Opout = self.mkl_concatenate_func(axis, image, image1) f = function([image, image1], [Opout, ]) output_mkl = f(imval, imval) utt.assert_allclose(output_mkl, output_ref)
def test_infer_shape_gradI(self): def rand(*shape): r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') return r * 2 - 1 corrMM = corr.CorrMM gradI = corr.CorrMM_gradInputs adtens = T.dtensor4() bdtens = T.dtensor4() aivec_vals = [[1, 5, 6, 3], [8, 2, 7, 3], [1, 6, 9, 4], [9, 6, 8, 5], [9, 1, 6, 8]] bivec_vals = [[7, 5, 3, 1], [4, 2, 5, 3], [12, 6, 3, 2], [5, 6, 1, 3], [7, 1, 3, 4]] modes = ['valid', 'full', 'half', (1, 1), (2, 1), (1, 2), 1] subsamples = [(1, 1), (2, 1), (1, 2)] for aivec_val, bivec_val in zip(aivec_vals, bivec_vals): adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) for mode in modes: for subsample in subsamples: # CorrMM cdtens = corrMM(border_mode=mode, subsample=subsample)(adtens, bdtens) f = theano.function([adtens, bdtens], cdtens) cdtens_val = f(adtens_val, bdtens_val) # CorrMM_gradInputs shape = (theano.shared(aivec_val[2]), theano.shared(aivec_val[3])) adtens_g = gradI(border_mode=mode, subsample=subsample)(bdtens, cdtens, shape=shape) self._compile_and_check([bdtens, cdtens], [adtens_g], [bdtens_val, cdtens_val], gradI, warn=False)
def test_infer_shape_forward(self): if theano.config.mode == "FAST_COMPILE": raise SkipTest("CorrMM don't work in FAST_COMPILE") def rand(*shape): r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') return r * 2 - 1 corrMM = corr.CorrMM adtens = T.dtensor4() bdtens = T.dtensor4() aivec_vals = [[4, 5, 6, 3], [6, 2, 8, 3], [3, 6, 7, 5], [3, 6, 7, 5], [5, 2, 4, 3]] bivec_vals = [[7, 5, 3, 2], [4, 2, 5, 3], [5, 6, 3, 2], [5, 6, 2, 3], [6, 2, 4, 3]] modes = ['valid', 'full', 'half', (1, 1), (2, 1), (1, 2), 1] subsamples = [(1, 1), (2, 1), (1, 2)] for aivec_val, bivec_val in zip(aivec_vals, bivec_vals): adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) for mode in modes: for subsample in subsamples: # CorrMM cdtens = corrMM(border_mode=mode, subsample=subsample)(adtens, bdtens) self._compile_and_check([adtens, bdtens], [cdtens], [adtens_val, bdtens_val], corrMM, warn=False)
def setUp(self): super (TestConv2D, self).setUp() self.input = T.dtensor4('input') self.input.name = 'default_V' self.filters = T.dtensor4('filters') self.filters.name = 'default_filters' if not conv.imported_scipy_signal and theano.config.cxx == "": raise SkipTest("conv2d tests need SciPy or a c++ compiler")
def test_infer_shape(self): image = tensor.dtensor4() maxout = tensor.dtensor4() gz = tensor.dtensor4() rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3), (3, 2)) image_val = rng.rand(4, 6, 7, 9) out_shapes = [[[[4, 6, 7, 9], [4, 6, 7, 9]], [[4, 6, 3, 4], [4, 6, 4, 5]], [[4, 6, 2, 3], [4, 6, 3, 3]], [[4, 6, 3, 3], [4, 6, 4, 3]], [[4, 6, 2, 4], [4, 6, 3, 5]]], [[None, None], [[4, 6, 4, 5], None], [[4, 6, 3, 3], None], [[4, 6, 4, 3], None], [[4, 6, 3, 5], None]], [[None, None], [None, None], [[4, 6, 3, 4], None], [[4, 6, 4, 4], None], [None, None]]] for i, maxpoolshp in enumerate(maxpoolshps): for j, ignore_border in enumerate([True, False]): for k, padding in enumerate([(0, 0), (1, 1), (1, 2)]): if out_shapes[k][i][j] is None: continue # checking shapes generated by DownsampleFactorMax self._compile_and_check([image], [DownsampleFactorMax(maxpoolshp, ignore_border=ignore_border, padding=padding)(image)], [image_val], DownsampleFactorMax) # checking shapes generated by MaxPoolGrad maxout_val = rng.rand(*out_shapes[k][i][j]) gz_val = rng.rand(*out_shapes[k][i][j]) self._compile_and_check([image, maxout, gz], [MaxPoolGrad(maxpoolshp, ignore_border=ignore_border, padding=padding) (image, maxout, gz)], [image_val, maxout_val, gz_val], MaxPoolGrad, warn=False) # checking with broadcastable input image = tensor.tensor(dtype='float64', broadcastable=(False, False, True, True)) image_val = rng.rand(4, 6, 1, 1) self._compile_and_check( [image], [DownsampleFactorMax((2, 2), ignore_border=True, padding=(0, 0))(image)], [image_val], DownsampleFactorMax)
def test_no_shape(self): images = T.dtensor4('inputs') weights = T.dtensor4('weights') convOut = conv2d(images, weights, filter_shape=(12, 3, 3, 3), filter_flip=False) fopt = theano.function(inputs=[images, weights], outputs=convOut, mode=mode_with_mkl) fori = theano.function(inputs=[images, weights], outputs=convOut, mode=mode_without_mkl) # No optimization for the case image shape is None assert all([not isinstance(n, (Conv2D, U2IConv, I2U)) for n in fopt.maker.fgraph.toposort()]) assert str(fopt.maker.fgraph.toposort()) == str(fori.maker.fgraph.toposort())
def cnn(input): input.shape = (1, 1, 28, 28) x = T.dtensor4('x') classifer = CNN(input=x) get_p_y = theano.function(inputs=[x], outputs=classifer.outputs) pred_y = theano.function(inputs=[x], outputs=classifer.pred) return (get_p_y(input), pred_y(input))
def __init__(self, minibatch_size=128, epochs=1, learn_rate=1e-3, bottleneck_width=10, **kwargs): '''Initialize a ready-to-train convolutional autoencoder.''' super(IBDPairConvAe, self).__init__(self) # Shapes are given as (batch, depth, height, width) nchannels = kwargs.get('nchannels', 4) weighted = kwargs.get('weighted_cost', False) self.minibatch_shape = (minibatch_size, nchannels, 8, 24) self.minibatch_size = minibatch_size self.image_shape = self.minibatch_shape[1:-1] self.num_features = reduce(mul, self.image_shape) self.epochs = epochs self.learn_rate = learn_rate self.bottleneck_width = bottleneck_width self.input_var = T.dtensor4('input') self.network = self._setup_network() self.train_prediction = self._setup_prediction(deterministic=False) self.test_prediction = self._setup_prediction(deterministic=True) self.train_cost = self._setup_cost(deterministic=False, weighted=weighted) self.test_cost = self._setup_cost(deterministic=True, array=True) self.optimizer = self._setup_optimizer() self.train_once = theano.function([self.input_var], [self.train_cost], updates=self.optimizer) self.predict_fn = theano.function([self.input_var], [self.test_cost, self.test_prediction])
def test_DownsampleFactorMaxStride(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) # generate random images imval = rng.rand(4, 10, 16, 16) outputshps = ((4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 14, 14), (4, 10, 5, 5), (4, 10, 3, 2), (4, 10, 14, 14), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 12, 14), (4, 10, 4, 5), (4, 10, 3, 2), (4, 10, 12, 14), (4, 10, 5, 6), (4, 10, 4, 3)) images = tensor.dtensor4() indx = 0 for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: for stride in stridesizes: outputshp = outputshps[indx] indx += 1 #DownsampleFactorMax op numpy_output_val = \ self.numpy_max_pool_2d_stride(imval, maxpoolshp, ignore_border, stride) assert numpy_output_val.shape == outputshp, ( "outshape is %s, calculated shape is %s" % (outputshp, numpy_output_val.shape)) maxpool_op = \ DownsampleFactorMax(maxpoolshp, ignore_border=ignore_border, st=stride)(images) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"] ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([images], [output]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_mkl_lrn_forward(): if theano.config.floatX == 'float32': x = tensor.ftensor4() else: x = tensor.dtensor4() y = lrn(x) f = theano.function([x], y, mode=mode_with_mkl) topo = f.maker.fgraph.toposort() inputs = f.maker.fgraph.inputs outputs = f.maker.fgraph.outputs assert len(inputs) == 1 assert len(outputs) == 1 assert len(topo) == 3 assert isinstance(topo[0].op, U2ILRN) assert isinstance(topo[1].op, mkl.mkl_lrn.LRN) assert isinstance(topo[2].op, I2U) assert outputs[0].owner == topo[2] imval = numpy.random.rand(4, 2, 4, 4).astype(theano.config.floatX) f(imval) print('test_mkl_lrn_forward() pass..')
def test_mkl_relu_backward(): predefineOps = [U2IRelu, mkl.mkl_relu.Relu, I2U, Shape_i, Shape_i, Shape_i, Shape_i, Alloc, I2UGrad, mkl.mkl_relu.ReluGrad, U2IGrad] if theano.config.floatX == 'float32': x = tensor.ftensor4('x') else: x = tensor.dtensor4('x') y = tensor.nnet.relu(x) s = tensor.sum(y) z = tensor.grad(s, [x]) f = theano.function([x], z, mode=mode_with_mkl) topo = f.maker.fgraph.toposort() inputs = f.maker.fgraph.inputs outputs = f.maker.fgraph.outputs assert len(inputs) == 1 assert len(outputs) == 1 assert len(topo) == 11 for i, node in enumerate(topo): assert isinstance(node.op, predefineOps[i]) imval = numpy.random.rand(4, 2, 4, 4).astype(theano.config.floatX) f(imval) print('test_mkl_relu_backward() pass..')
def test_pool_stride_padding(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images ds_list = ((3, 3), (4, 4), (3, 4), (5, 5)) st_list = ((1, 1), (2, 2), (3, 3), (1, 2)) pad_list = ((1, 1), (0, 0), (1, 1), (1, 1)) imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8)) n = 4 c = 2 images = T.dtensor4() for idx, ignore_border, mode in product(numpy.arange(len(ds_list)), [False], ['max', 'average_exc_pad']): imgsize = imgsize_list[idx] imval = rng.rand(n, c, imgsize[0], imgsize[1]) ds = ds_list[idx] st = st_list[idx] pad = pad_list[idx] # Pure Numpy computation numpy_output_val = self.numpy_pool_2d_stride_padding(imval, ds, ignore_border, st, pad, mode) # MKL Ops output = self.mkl_pool_func(images, ignore_border, mode, ds, st, pad) f = function([images, ], [output, ]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 10, 64, 64) images = tensor.dtensor4() for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: #print 'maxpoolshp =', maxpoolshp #print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border) output = max_pool_2d(images, maxpoolshp, ignore_border) f = function([images, ], [output, ]) output_val = f(imval) assert numpy.all(output_val == numpy_output_val) #DownsampleFactorMax op maxpool_op = DownsampleFactorMax(maxpoolshp, ignore_border=ignore_border)(images) f = function([images], maxpool_op) output_val = f(imval) assert (numpy.abs(output_val - numpy_output_val) < 1e-5).all()
def cnn(train_x, train_y, learning_rate=0.05, batch=100, epochs=100): train_x = train_x.reshape(-1, 1, 100, 100) X = T.dtensor4() Y = T.fmatrix() w1 = init_weights((8, 1, 3, 3)) w2 = init_weights((4, 8, 3, 3)) w = init_weights((196, 62)) b = init_bias(62) l = model(X, w1, w2, w, b) y_pred = T.argmax(l, axis=1) cost = T.mean(T.nnet.categorical_crossentropy(l, Y)) params = [w1, w2, w, b] update = sgd(cost, params, learning_rate) train = theano.function(inputs=[X, Y], outputs=cost, updates=update, allow_input_downcast=True) predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True) now = time.strftime("%X", time.localtime()) print "[%s] Start training" % (now) for epoch in range(epochs): hit = 0.0 for start, end in zip(range(0, train_x.shape[0], batch), range(batch, train_x.shape[0], batch)): cost = train(train_x[start:end, :], train_y[start:end, :]) hit = hit + np.sum(np.argmax(train_y[start:end, :], axis=1) == predict(train_x[start:end, :])) accuracy = hit / train_x.shape[0] now = time.strftime("%X", time.localtime()) print "[%s] epoch %d, accuracy = %.4f" % (now, epoch + 1, accuracy) if accuracy > 0.9950: break f = open("model.txt", "w") lists1 = w1.get_value(borrow=True) for i in lists1: for j in i: for k in j: for l in k: f.write((str)(l) + "\t") f.write("\n") lists2 = w2.get_value(borrow=True) for i in lists2: for j in i: for k in j: for l in k: f.write((str)(l) + "\t") f.write("\n") lists3 = w.get_value(borrow=True) for i in lists3: for l in i: f.write((str)(l) + "\t") f.write("\n") lists4 = b.get_value(borrow=True) for i in lists4: f.write((str)(i) + "\t") f.write("\n") f.close()
def _make_graph(self): if not self.twod_inputs: self._inputs = TT.dtensor4("inputs") layer_outputs = self._inputs else: self._inputs = TT.matrix("inputs") layer_outputs = self._inputs.reshape(self.img_shape) for i in range(0, len(self.conv_weights)): # Perform the convolution. conv_out = conv.conv2d(layer_outputs, self.conv_weights[i], filter_shape = self.filter_shapes[i], image_shape = self.layer_shapes[i]) # Downsample the feature maps. pooled_out = downsample.max_pool_2d(conv_out, self.pool_sizes[i], ignore_border = True) # Account for the bias. Since it is a vector, we first need to reshape it # to (1, n_filters, 1, 1). layer_outputs = self.activation_func(pooled_out + \ self.conv_biases[i].dimshuffle("x", 0, "x", "x")) # Concatenate output maps into one big matrix where each row is the # concatenation of all the feature maps from one item in the batch. next_shape = self.layer_shapes[i + 1] new_shape = (next_shape[0], reduce(mul, next_shape[1:], 1)) print "New Shape: " + str(new_shape) self.x = layer_outputs.reshape(new_shape)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product(maxpoolshps, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = max_pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([images, ], [output, ]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # DownsampleFactorMax op maxpool_op = DownsampleFactorMax(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_gauntlet(self): maxpoolshps = ((1, 1), (3, 3), (5, 3),) stridesizes = ((1, 1), (3, 3), (5, 7),) # generate random images imval = self.rng.rand(4, 10, 16, 16) images = T.dtensor4() for index_type, index_scope, maxpoolshp, stride, ignore_border in product(['flattened', 'array'], ['local', 'global'], maxpoolshps, stridesizes, [True, False]): # Pool op max_pool_op = Pool(ds=maxpoolshp, ignore_border=ignore_border, st=stride, mode='max')(images) max_pool_f = theano.function([images], max_pool_op) maxpool_output_val = max_pool_f(imval) maxpoolswitch_op = MaxPoolSwitch(ds = maxpoolshp, ignore_border=ignore_border, st=stride, index_type=index_type, index_scope=index_scope)(images) f = theano.function([images], maxpoolswitch_op, mode='DebugMode') output_val = f(imval) self.check_max_and_switches(imval, output_val, maxpool_output_val, maxpoolshp, ignore_border, stride, None, index_type, index_scope)
def test_DownsampleFactorMaxPaddingStride(self): ignore_border = True # padding does not support ignore_border=False rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolsizes = [(3, 3), (4, 4), (3, 4), (4, 3), (2, 2)] stridesizes = [(2, 2), (2, 2), (1, 1), (1, 2), (2, 2)] paddingsizes = [(2, 2), (1, 2), (2, 1), (0, 0), (1, 1)] imgsizes = [(5, 5), (5, 5), (5, 6), (6, 5), (5, 5)] m = 4 # minibatch c = 2 # channel size images = tensor.dtensor4() for indx, mode in product( numpy.arange(len(maxpoolsizes)), ["max", "sum", "average_inc_pad", "average_exc_pad"] ): imgsize = imgsizes[indx] imval = rng.rand(m, c, imgsize[0], imgsize[1]) - 0.5 stridesize = stridesizes[indx] maxpoolsize = maxpoolsizes[indx] paddingsize = paddingsizes[indx] numpy_output_val = self.numpy_max_pool_2d_stride_padding( imval, maxpoolsize, ignore_border, stridesize, paddingsize, mode ) maxpool_op = DownsampleFactorMax( maxpoolsize, ignore_border=ignore_border, st=stridesize, padding=paddingsize, mode=mode )(images) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_c_code(self): x = T.dtensor4() gz = T.tensor4() f = theano.function([x, gz], self.op(x, gz), mode='DebugMode') inp = self.rng.rand(1, 6, 4, 4) inp_gz = self.rng.rand(1, 3, 8, 8) out = f(inp, inp_gz)
def conv2d_func(image_shape, filter_shape, mode): import theano import theano.tensor as T if FLAGS.floatX == 'float32': inputs = T.ftensor4() filters = T.ftensor4() else: inputs = T.dtensor4() filters = T.dtensor4() conv_out = theano.tensor.nnet.conv2d( input=inputs, filters=filters, filter_shape=filter_shape, image_shape=image_shape, border_mode=mode ) func = theano.function([inputs, filters], conv_out) return func
def test_neibs_ignore_border(): shape = (2, 3, 5, 5) images = T.dtensor4() images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) def fn(images): return T.sum(T.sqr(images2neibs(images, (2, 2), mode='ignore_borders')), axis=[0, 1])
def test_neibs_ignore_border(): shape = (2,3,5,5) images = T.dtensor4() images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) def fn(images): return T.sum(T.sqr(images2neibs(images, (2,2), mode='ignore_borders')), axis=[0,1]) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
def test_conv_U2I(self): images = T.dtensor4('inputs') a_internal = U2IConv(imshp=(12, 3, 256, 256), kshp=(12, 3, 3, 3))(images) out = I2U()(a_internal) fopt = theano.function([images], out, mode=mode_with_mkl) ival = numpy.random.rand(12, 3, 256, 256).astype(numpy.float64) assert numpy.allclose(fopt(ival), ival)
def test_conv_with_bias(self): images = T.dtensor4('input_conv') weights = T.dtensor4('weights') bias = T.dvector('bias') ishape = [(8, 3, 256, 256), (16, 3, 256, 256), (32, 3, 256, 256), (64, 3, 256, 256)] wshape = [(8, 3, 3, 3), (16, 3, 3, 3), (32, 3, 3, 3), (64, 3, 3, 3)] for i, ish in enumerate(ishape): wsh = wshape[i] images_internal = U2IConv(imshp=ish, kshp=wsh)(images) convOut = Conv2D(imshp=ish, kshp=wsh, filter_flip=False)(images_internal, weights, bias) convOut_user = I2U()(convOut) convOutLoss = T.mean(convOut_user) conv_op_di = theano.grad(convOutLoss, images) conv_op_dk = theano.grad(convOutLoss, weights) conv_op_db = theano.grad(convOutLoss, bias) convOutBack = [conv_op_di, conv_op_dk, conv_op_db] ival = numpy.random.rand(*ish).astype(numpy.float64) wval = numpy.random.rand(*wsh).astype(numpy.float64) bval = numpy.random.rand(wsh[0]).astype(numpy.float64) - numpy.random.rand(wsh[0]).astype(numpy.float64) fopt = theano.function(inputs=[images, weights, bias], outputs=convOutBack, mode=mode_with_mkl) new_out = fopt(ival, wval, bval) convOut = conv2d(images, weights, input_shape=ish, filter_shape=wsh, filter_flip=False) convOutLoss = T.mean(convOut + bias.dimshuffle('x', 0, 'x', 'x')) conv_op_di = theano.grad(convOutLoss, images) conv_op_dk = theano.grad(convOutLoss, weights) conv_op_db = theano.grad(convOutLoss, bias) convOutBack = [conv_op_di, conv_op_dk, conv_op_db] fori = theano.function(inputs=[images, weights, bias], outputs=convOutBack, mode=mode_without_mkl) old_out = fori(ival, wval, bval) assert len(fopt.maker.fgraph.toposort()) != len(fori.maker.fgraph.toposort()) assert numpy.allclose(old_out[0], new_out[0]) # assert numpy.allclose(old_out[1], new_out[1]) assert numpy.allclose(old_out[2], new_out[2]) assert new_out[0].dtype == 'float64' assert new_out[2].dtype == 'float64'
x, x0, alfa, rtV, cI = inputs_storage y, cM = output_storage y[0], cM[0] = projection(x, x0, alfa, rtV, cI) # optional: check_input = True # %% pruebo si esta bien como OP x = T.dtensor3('x') x0 = T.dvector('x0') alfa = T.dscalar('alfa') rtV = T.dtensor3('rtV') cI = T.dtensor4('cI') projT = ProjectionT() projTfunction = theano.function([x, x0, alfa, rtV, cI], projT(x, x0, alfa, rtV, cI)) out = projTfunction(xTrue, x0True, alfaTrue, rtVTrue, cITrue) print(out) # %% modelo niter = 100 try: basic_model
# constructors with fixed data type. (examples with tensor4) # b: byte, w: word(16bit), l: int64, i: int32 # d:float64, f: float32, c: complex64, z: complex128 v = T.btensor4(name='v') report(v) v = T.wtensor4(name='v') report(v) v = T.itensor4(name='v') report(v) v = T.ltensor4(name='v') report(v) v = T.dtensor4(name='v') report(v) v = T.ftensor4(name='v') report(v) v = T.ctensor4(name='v') report(v) v = T.ztensor4(name='v') report(v) # you can of course define custom data type out of Theano tensors. dtensor5 = T.TensorType('float64', (False, ) * 5) x = dtensor5() z = dtensor5('z')
def __init__(self, content_path, style_path, image_w=500, image_h=500, style_weight=2e5, content_weight=0.001): self.image_w = image_w self.image_h = image_h # mean pixel values for VGG-19 self.mean_pixels = np.array([104, 117, 123]).reshape((3, 1, 1)) content_image = imread(content_path) style_image = imread(style_path) content = self.preprocess_image(content_image) style = self.preprocess_image(style_image) self.content = content self.style = style self.vgg19 = VGG19(input_image_shape=(1, 3, image_h, image_w), pool_method='average_exc_pad') self.get_content_layer = theano.function( inputs=[self.vgg19.input], outputs=self.vgg19.conv4_2.output) self.get_style_layers = theano.function(inputs=[self.vgg19.input], outputs=[ self.vgg19.conv1_1.output, self.vgg19.conv2_1.output, self.vgg19.conv3_1.output, self.vgg19.conv4_1.output, self.vgg19.conv5_1.output ]) self.sty_out = self.get_style_layers(style) self.cont_lay = self.get_content_layer(content) white_noise = np.random.uniform(low=-128.0, high=128.0, size=(1, 3, image_h, image_w)).astype( theano.config.floatX) self.wn = theano.shared(value=white_noise, name='wn', borrow=False) self.cont = T.dtensor4('cont') self.sty1 = T.dtensor4('sty1') self.sty2 = T.dtensor4('sty2') self.sty3 = T.dtensor4('sty3') self.sty4 = T.dtensor4('sty4') self.sty5 = T.dtensor4('sty5') # alpha/beta should be around 0.01 to achieve good results self.cont_loss = content_weight * 0.5 * T.sum( T.sqr(self.vgg19.conv4_2.output - self.cont)) self.style_loss = style_weight * ( self.calc_style_loss(self.sty1, self.vgg19.conv1_1.output) + self.calc_style_loss(self.sty2, self.vgg19.conv2_1.output) + self.calc_style_loss(self.sty3, self.vgg19.conv3_1.output) + self.calc_style_loss(self.sty4, self.vgg19.conv4_1.output) + self.calc_style_loss(self.sty5, self.vgg19.conv5_1.output)) self.cost = self.cont_loss + self.style_loss self.img_grad = T.grad(self.cost, self.vgg19.input) # Theano functions to evaluate loss and gradient self.f_loss = theano.function([ self.vgg19.input, self.cont, self.sty1, self.sty2, self.sty3, self.sty4, self.sty5 ], self.cost) self.f_grad = theano.function([ self.vgg19.input, self.cont, self.sty1, self.sty2, self.sty3, self.sty4, self.sty5 ], self.img_grad) self.losses = []
# # 1×60 # finalSRep = T.sum(newSRep, axis=0) # # 1×120 # finSRepAfNon = T.dot(finalSRep, linearW) # # finSRepAfNon = finSRepAfNon + T.dot(LRConnect, WForEP) + BForEP # # return [finSRepAfNon, newSRep] # # [finalSRep, myob], _ = theano.scan(AtLayerData, outputs_info=[LRConnect, None], n_steps=NUMBER_DATA) # return [finalSRep[-1], myob[-1]] return originSentence input2 = T.dtensor4() left2 = T.ivector() right2 = T.ivector() Slen2 = T.ivector() input1 = T.dtensor3() left1 = T.iscalar() right1 = T.iscalar() Slen1 = T.iscalar() # ok = atData(input1, left1, right1, Slen1) ok, _ = theano.scan(atData, sequences=[input2, left2, right2, Slen2]) myfunc = theano.function([input2, left2, right2, Slen2], ok, on_unused_input='ignore')
def test_DownsampleFactorMaxStride(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3), (16, 16)) stridesizes = ( (1, 1), (3, 3), (5, 7), ) # generate random images imval = rng.rand(4, 10, 16, 16) # The same for each mode outputshps = ( (4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 14, 14), (4, 10, 5, 5), (4, 10, 3, 2), (4, 10, 14, 14), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 12, 14), (4, 10, 4, 5), (4, 10, 3, 2), (4, 10, 12, 14), (4, 10, 5, 6), (4, 10, 4, 3), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), ) images = tensor.dtensor4() indx = 0 for mode, maxpoolshp, ignore_border in product( ['max', 'sum', 'average_inc_pad', 'average_exc_pad'], maxpoolshps, [True, False]): for stride in stridesizes: outputshp = outputshps[indx % len(outputshps)] indx += 1 # Pool op numpy_output_val = \ self.numpy_max_pool_2d_stride(imval, maxpoolshp, ignore_border, stride, mode) assert numpy_output_val.shape == outputshp, ( "outshape is %s, calculated shape is %s" % (outputshp, numpy_output_val.shape)) maxpool_op = \ Pool(maxpoolshp, ignore_border=ignore_border, st=stride, mode=mode)(images) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
# test data test_x_indexes_extended = dataUtils.pad_to_batch_size(test_x_indexes, hyperparas['batch_size']) test_y_extended = dataUtils.pad_to_batch_size(test_y, hyperparas['batch_size']) n_test_batches = test_x_indexes_extended.shape[0] / hyperparas['batch_size'] n_test_samples = len(test_y) dataUtils.extend_lenghts(test_lengths, hyperparas['batch_size']) ###################### # BUILD ACTUAL MODEL # ###################### print('Building the model') # allocate symbolic variables for the data X_batch = T.dtensor4('x') y_batch = T.lvector('y') rng = numpy.random.RandomState(23455) # define/load the network output_layer = networks.build1DDCNN_dynamic( nlayers=hyperparas['nlayers'], batch_size=hyperparas['batch_size'], channels_size=hyperparas['channels_size'], vocab_size=hyperparas['vocab_size'], filter_sizes=hyperparas['filter_size_conv_layers'], nr_of_filters=hyperparas['nr_of_filters_conv_layers'], activations=hyperparas['activations'], ktop=hyperparas['ktop'], dropout=hyperparas["dropout_value"], output_classes=hyperparas['output_classes'],
params = layer1.params + [E, O] return (mental_images.dimshuffle(1, 0, 3, 2, 4), \ output.dimshuffle(1, 0, 2), \ outputs.dimshuffle(2, 0, 1, 3), \ accuracy, cost, params) if __name__ == '__main__': argparser = argparse.ArgumentParser() argparser.add_argument('--load', help='load help', type=file) argparser.add_argument('--batch_size', help='batch size help', \ default=128, type=int) args = argparser.parse_args() input_var = T.dtensor4('input') target_var = T.dtensor3('target') states, output, outputs, accuracy, cost, params = model(input_var, \ target_var, num_symbols=3, width=4, embedding_dim=24, \ filter_size=(3, 3)) generator = Duplicate(batch_size=args.batch_size, max_iter=1000, \ proba_curriculum=0.2, max_length=10) if args.load is None: updates = lasagne.updates.adam(cost, params, learning_rate=1e-3) train_fn = theano.function([input_var, target_var], cost, \ updates=updates) accuracy_fn = theano.function([input_var, target_var], accuracy)
def test_infer_shape(self): # Note: infer_shape is incomplete and thus input and filter shapes # must be provided explicitly def rand(*shape): r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') return r * 2 - 1 adtens = T.dtensor4() bdtens = T.dtensor4() aivec_val = [4, 5, 6, 3] bivec_val = [7, 5, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='valid') ], [adtens_val, bdtens_val], conv.ConvOp) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='full') ], [adtens_val, bdtens_val], conv.ConvOp) aivec_val = [6, 2, 8, 3] bivec_val = [4, 2, 5, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='valid') ], [adtens_val, bdtens_val], conv.ConvOp) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='full') ], [adtens_val, bdtens_val], conv.ConvOp) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='valid') ], [adtens_val, bdtens_val], conv.ConvOp) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='full') ], [adtens_val, bdtens_val], conv.ConvOp) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 2, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='valid') ], [adtens_val, bdtens_val], conv.ConvOp) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='full') ], [adtens_val, bdtens_val], conv.ConvOp) aivec_val = [5, 2, 4, 3] bivec_val = [6, 2, 4, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='valid') ], [adtens_val, bdtens_val], conv.ConvOp) self._compile_and_check([adtens, bdtens], [ conv.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode='full') ], [adtens_val, bdtens_val], conv.ConvOp)
def __init__(self, n_words=110, n_embedding=4800, lr=0.01, momentum=0.9, word_to_id=None, null_word_id=-1, max_stmts=110, max_words=4800, load_from_file=None): if load_from_file: self.load_model(load_from_file) else: self.regularization = 0.000001 self.n_embedding = n_embedding self.lr = lr self.momentum = momentum self.n_words = n_words self.batch_size = 4 self.max_stmts = max_stmts self.max_words = max_words self.word_to_id = word_to_id self.id_to_word = dict((v, k) for k, v in word_to_id.iteritems()) self.null_word_id = null_word_id # Question embedding # self.B = init_shared_normal(self.n_words, self.n_embedding, 0.1) # Statement input, output embeddings self.weights = init_shared_normal_tensor(110, 80, 4800, 0.1) # Linear mapping between layers self.H = init_shared_normal(self.n_embedding, self.n_embedding, 0.1) # Final outut weight matrix # self.W = init_shared_normal(self.n_embedding, self.n_words, 0.1) zero_vector = T.vector('zv', dtype=theano.config.floatX) # Statement x = T.dtensor3('x') #1 w3 #x = T.dtensor4('x') #3 w3 xbatch = T.dtensor4('xb') # Positional encoding matrix pe = T.tensor3('pe') # Question q = T.dvector('q') qbatch = T.dmatrix('qb') # True word r = T.iscalar('r') rbatch = T.ivector('rb') memory_cost = self.memnn_cost(x, q, pe) # memory_loss = -T.log(memory_cost[r]) # cross entropy on softmax memory_loss = self.memnn_batch_cost(xbatch, qbatch, rbatch, pe) params = [ self.weights, # self.B, # self.W, self.H ] regularization_cost = reduce( lambda x, y: x + y, map(lambda x: self.regularization * T.sum(x**2), params)) cost = memory_loss + regularization_cost grads = T.grad(cost, params) l_rate = T.scalar('l_rate') # Parameter updates updates = get_param_updates(params, grads, lr=l_rate, method='adadelta', momentum=0.9, constraint=self._constrain_embedding( self.null_word_id, zero_vector)) self.train_function = theano.function( inputs=[ xbatch, qbatch, rbatch, pe, theano.Param(l_rate, default=self.lr), theano.Param(zero_vector, default=np.zeros((self.n_embedding, ), theano.config.floatX)) ], outputs=cost, updates=updates, allow_input_downcast=True, # mode='FAST_COMPILE', #mode='DebugMode' #mode=theano.compile.MonitorMode(pre_func=detect_nan, post_func=detect_nan).excluding('local_elemwise_fusion', 'inplace') #mode=NanGuardMode(nan_is_error=True, inf_is_error=True, big_is_error=True) on_unused_input='warn') self.predict_function = theano.function( inputs=[x, q, pe], outputs=memory_cost, allow_input_downcast=True, # mode='FAST_COMPILE', on_unused_input='warn')
def test_infer_shape(self): # Note: infer_shape is incomplete and thus input and filter shapes # must be provided explicitly def rand(*shape): r = np.asarray(np.random.rand(*shape), dtype="float64") return r * 2 - 1 adtens = T.dtensor4() bdtens = T.dtensor4() aivec_val = [4, 5, 6, 3] bivec_val = [7, 5, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [6, 2, 8, 3] bivec_val = [4, 2, 5, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 2, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [5, 2, 4, 3] bivec_val = [6, 2, 4, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], )
img_ = img.transpose(2, 0, 1).reshape(1, 3, 183 , 275) filtered_img = f(img_) # plot original image and first and second components of output pylab.subplot(1, 3, 1); pylab.axis('off'); pylab.imshow(img) pylab.gray(); # recall that the convOp output (filtered image) is actually a "minibatch", # of size 1 here, so we take index 0 in the first dimension: pylab.subplot(1, 3, 2); pylab.axis('off'); pylab.imshow(filtered_img[0, 0, :, :]) pylab.subplot(1, 3, 3); pylab.axis('off'); pylab.imshow(filtered_img[0, 1, :, :]) pylab.show() ### ----### part 3 from theano.tensor.signal import pool input = tensor.dtensor4('input') maxpool_shape = (2, 2) pool_out = pool.pool_2d(input, maxpool_shape, ignore_border=True) f = theano.function([input],pool_out) invals = numpy.random.RandomState(1).rand(3, 2, 5, 5) print 'With ignore_border set to True:' print 'invals[0, 0, :, :] =\n', invals[0, 0, :, :] print 'output[0, 0, :, :] =\n', f(invals)[0, 0, :, :] pool_out = pool.pool_2d(input, maxpool_shape, ignore_border=False) f = theano.function([input],pool_out) print 'With ignore_border set to False:' print 'invals[1, 0, :, :] =\n ', invals[1, 0, :, :] print 'output[1, 0, :, :] =\n ', f(invals)[1, 0, :, :]
#coding=utf-8 ''' Created on 2016年11月30日 @author: Administrator ''' import numpy import theano.tensor as T import theano from theano.tensor.signal import pool input = T.dtensor4('input') maxpool_shape = (2, 2) #pool_out=pool.pool_2d(input=input, ds=maxpool_shape, ignore_border=True) pool_out = pool.pool_2d(input=input, ds=maxpool_shape, ignore_border=False) f = theano.function([input], pool_out) invals = numpy.random.RandomState(1).rand(3, 2, 5, 5) print 'With ignore_border set to True:' print 'invals[0, 0, :, :] =\n', invals[0, 0, :, :] print 'output[0, 0, :, :] =\n', f(invals)[0, 0, :, :]
def __init__(self, train_set_x, train_set_y, valid_set_x, valid_set_y, batch_size, nkerns=[20, 50], nb_neurons=[225, 100]): if sys.version_info < (3,0): super(self.__class__, self).__init__(train_set_x, train_set_y, valid_set_x, valid_set_y, batch_size) else: super().__init__(train_set_x, train_set_y, valid_set_x, valid_set_y, batch_size) self.x = T.dtensor4('x') nb_channel = train_set_x.shape[1] height = train_set_x.shape[2] width = train_set_x.shape[3] self.layers = [] layer0 = ConvLayer( self.rng, inputs=self.x, image_shape=(batch_size, nb_channel, height, width), filter_shape=(nkerns[0], nb_channel, 6, 6), # stride=2, # pad=2 ) layer1 = PoolLayer( inputs=layer0.output, input_shape=layer0.output_shape ) self.layers.append(layer0) self.layers.append(layer1) # nkerns.pop(0) for i, layer_param in enumerate(nkerns): if i != 0: layer = ConvLayer( self.rng, inputs=self.layers[-1].output, image_shape=self.layers[-1].output_shape, filter_shape=(nkerns[i], nkerns[i-1], 6, 6), # stride=2, # pad=2 ) self.layers.append(layer) layer = PoolLayer( inputs=self.layers[-1].output, input_shape=self.layers[-1].output_shape ) self.layers.append(layer) layer4_input = self.layers[-1].output.flatten(2) n_in = self.layers[-1].output_shape[1] * self.layers[-1].output_shape[2] * self.layers[-1].output_shape[3] # n_out = int(n_in/2) layer = FullyConnectedLayer(layer4_input, n_in, nb_neurons[0], self.rng) self.layers.append(layer) n_in = nb_neurons[0] nb_neurons.pop(0) for n_out in nb_neurons: inputs = self.layers[-1].outputs layer = FullyConnectedLayer(inputs, n_in, n_out, self.rng) n_in = n_out self.layers.append(layer) nb_outputs = len(np.unique(train_set_y)) layer = SoftmaxLayer(inputs=self.layers[-1].outputs, n_in=n_out, n_out=nb_outputs, rng=self.rng) self.layers.append(layer)
def test_fail(self): """ Test that conv2d fails for dimensions other than 2 or 3. """ self.assertRaises(Exception, conv.conv2d, T.dtensor4(), T.dtensor3()) self.assertRaises(Exception, conv.conv2d, T.dtensor3(), T.dvector())