def __init__(self, label, rng, input, nin, nout, W=None, b=None, activation_fn=op.Sigmoid): q = pl.qs[0] self.input = input if W is None: nw = np.asarray(rng.uniform(low=-np.sqrt(6. / (nin + nout)), high=np.sqrt(6. / (nin + nout)), size=(nin, nout)), dtype=np.float32) if activation_fn == op.Sigmoid: nw *= 4 W = clarray.to_device(q, nw) if b is None: b = clarray.zeros(q, (nout, ), np.float32) self.W = W self.b = b vW = expr.Variable('W' + label) vb = expr.Variable('b' + label) lin_out = op.Add(op.Dot(self.input, vW), vb) self.output = lin_out if activation_fn is None else activation_fn( lin_out) self.params = [(vW.name, self.W), (vb.name, self.b)]
def test_dot_revgrad(self): x = expr.Variable('x') y = expr.Variable('y') dotprod = op.Dot(x, y) valuation = pl.valuation() nx = np.random.uniform(0, 1, (5, 10)).astype(np.float32) ny = np.random.uniform(0, 1, (10, 3)).astype(np.float32) D = nx.dot(ny) dD = np.ones_like(D) # print >>sys.stderr, '\nDDshape:', dD.shape dX = dD.dot(ny.T) dY = nx.T.dot(dD) # print >>sys.stderr, '\ndX.shape:', dX.shape, 'dY.shape:', dY.shape valuation['x'] = nx valuation['y'] = ny grad = dotprod.rev_grad(valuation) dx = grad['x'].get() dy = grad['y'].get() # print >>sys.stderr, '\ndx:', dx.shape # print >>sys.stderr, 'dy:', dy.shape self.assertTrue(np.allclose(dx, dX)) self.assertTrue(np.allclose(dy, dY))
def test_dot_eval(self): x = expr.Variable('x') y = expr.Variable('y') dotprod = op.Dot(x, y) m, k, n = 5, 10, 3 valuation = pl.valuation() nx = np.random.uniform(0, 1, (5, 10)).astype(np.float32) ny = np.random.uniform(0, 1, (10, 3)).astype(np.float32) valuation['x'] = nx valuation['y'] = ny gd = dotprod.evaluate(valuation) d = gd.get() self.assertTrue(np.allclose(d, np.dot(nx, ny))) # TODO tests for different dimensions cases... # batch cases batch_size = 10 nx = np.random.uniform(0, 1, (batch_size, m, k)).astype(np.float32) ny = np.random.uniform(0, 1, (batch_size, k, n)).astype(np.float32) val2 = pl.valuation() val2['x'] = nx val2['y'] = ny gdotp = dotprod.evaluate(val2) expected = np.array([np.dot(nx[i], ny[i]) for i in range(batch_size)]) d = gdotp.get() # print >>sys.stderr, '\nexpected:\n', expected # print >>sys.stderr, 'got:\n', d self.assertTrue(np.allclose(d, expected))
def __init__(self, rng, nin, nhid, nout): X = expr.Variable('X') Y = expr.Variable('Y') self.layer1 = FullyConnectedLayer(rng, '1', X, nin, nhid) self.layer2 = FullyConnectedLayer(rng, '2', self.layer1.output, nhid, nout) # self.layer2 = FullyConnectedLayer(rng, '2', X, nin, nout, activation_fn=None) self.y_pred = op.Argmax(self.layer2.output, -1) # TODO test this axis self.cost = op.MeanSquaredErr(self.layer2.output, Y) # op.Sub(self.layer2.output, Y) self.errors = op.Mean(op.NotEq(self.y_pred, Y)) self.params = self.layer1.params + self.layer2.params
def test_dot_fwdgrad(self): x = expr.Variable('x') y = expr.Variable('y') dotprod = op.Dot(x, y) valuation = pl.valuation() nx = np.random.uniform(0, 1, (10, )).astype(np.float32) ny = np.random.uniform(0, 1, (10, )).astype(np.float32) valuation['x'] = nx valuation['y'] = ny xw = clarray.zeros(pl.qs[0], (10, ), dtype=np.float32) + 1.0 yw = clarray.zeros(pl.qs[0], (10, ), dtype=np.float32) gddot = dotprod.fwd_grad({'x': xw, 'y': yw}, valuation) ddot = gddot.get()
def test_conv2d_eval(self): img = expr.Variable('img') k = expr.Variable('k') b = expr.Variable('b') convolution = op.Conv2d(img, k, b, strides=(2, 2), zero_padding=(0, 0)) valuation = pl.valuation() nimg = np.asarray([[[[0., 0., 0., 0., 0., 0., 0.], [0., 2., 1., 0., 2., 2., 0.], [0., 2., 2., 1., 1., 2., 0.], [0., 2., 0., 0., 2., 1., 0.], [0., 0., 2., 0., 1., 0., 0.], [0., 1., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0., 0., 0.], [0., 2., 0., 2., 0., 1., 0.], [0., 2., 2., 1., 0., 2., 0.], [0., 2., 1., 0., 0., 1., 0.], [0., 2., 0., 1., 0., 0., 0.], [0., 1., 1., 0., 2., 2., 0.], [0., 0., 0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0., 0., 0.], [0., 2., 0., 0., 2., 1., 0.], [0., 2., 2., 2., 2., 0., 0.], [0., 1., 1., 2., 1., 0., 0.], [0., 2., 1., 2., 1., 0., 0.], [0., 1., 0., 0., 0., 2., 0.], [0., 0., 0., 0., 0., 0., 0.]]]]).astype(np.float32) nk = np.asarray([[[[-1, 1, 1], [1, 0, 0], [1, 0, 0]], [[1, -1, 1], [1, -1, -1], [1, 1, 1]], [[0, 0, 0], [0, -1, 1], [0, 1, -1]]], [[[0, 0, -1], [0, 1, 0], [0, 0, -1]], [[-1, -1, 1], [-1, 1, -1], [1, 1, 0]], [[0, 1, -1], [1, 0, 0], [0, -1, 0]]]]).astype(np.float32) nb = np.asarray([1, 0]).astype(np.float32) expected = np.asarray([[[[1, 7, 4], [5, 6, 2], [-2, -1, -2]], [[2, 2, 7], [-1, -6, 1], [-2, -4, 0]]]]).astype(np.float32) valuation['img'] = nimg valuation['k'] = nk valuation['b'] = nb ret = convolution.evaluate(valuation) nret = ret.get() self.assertTrue(np.allclose(expected, nret))
def test_neq_eval(self): n = 10000 X = np.zeros((n, ), dtype=np.float32) Y = np.ones((n, ), dtype=np.float32) varX = expr.Variable('X') varY = expr.Variable('Y') neq = op.NotEq(varX, varY) eq = op.NotEq(varX, varX) val = pl.valuation() val['X'] = X val['Y'] = Y gres = neq.evaluate(val) gres2 = eq.evaluate(val) self.assertTrue(gres.all()) self.assertFalse(gres2.any())
def __init__(self, label, rng, img, filter_shape, fstrides=(0, 0), zero_pad=(0, 0), poolsize=(0, 0), W=None, b=None, activation_fn=None): q = pl.qs[0] self.img = img # init W and b if W is None: fan_in = np.prod(filter_shape[1:]) fan_out = filter_shape[0] * np.prod(filter_shape[2:]) W_bound = np.sqrt(6. / (fan_in + fan_out)) nW = np.asarray(rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), dtype=np.float32) self.W = clarray.to_device(q, nW) else: self.W = W if b is None: self.b = clarray.zeros(q, (filter_shape[0], ), dtype=np.float32) else: self.b = b pf, ps = poolsize vW = expr.Variable('W' + label) vb = expr.Variable('b' + label) _out = op.Conv2d(self.img, vW, vb, strides=fstrides, zero_padding=zero_pad) if pf > 0 and ps > 0: _out = op.MaxPool(_out, pf, ps) self.output = activation_fn(_out) self.params = [(vW.name, self.W), (vb.name, self.b)]
def __init__(self, label, rng, input, n_in, n_out, W=None, b=None): q = pl.qs[0] self.input = input if W is None: self.W = clarray.zeros(q, (n_in, n_out), dtype=np.float32) else: self.W = W if b is None: self.b = clarray.zeros(q, (n_out, ), dtype=np.float32) else: self.b = b vW = expr.Variable('W' + label) vb = expr.Variable('b' + label) self.p_y_given_x = op.Softmax(op.Add(op.Dot(self.input, vW), vb)) self.y_pred = op.Argmax(self.p_y_given_x, axis=-1) self.params = [(vW.name, self.W), (vb.name, self.b)]
def test_sigmoid_eval(self): X = np.random.uniform(-1, 1, (10, 10)).astype(np.float32) vX = expr.Variable('X') sig = op.Sigmoid(vX) expected = 1.0 / (1.0 + np.exp(-X)) val = pl.valuation() val['X'] = X gs = sig.evaluate(val) s = gs.get() self.assertTrue(np.allclose(s, expected))
def test_addition(self): x = expr.Variable("x") y = expr.Constant(2) valuation = plat.valuation() valuation['x'] = np.eye(2, dtype=np.float32) z = op.Add(x,y).evaluate(valuation) zz = z.get() self.assertEqual(zz[0,0], 3.0) self.assertEqual(zz[0,1], 2.0) self.assertEqual(zz[1,0], 2.0) self.assertEqual(zz[1,1], 3.0)
def __init__(self, rng, n_out, params=None): X = expr.Variable('X') Y = expr.Variable('Y') self.conv1 = ConvLayer('_C1', rng, X, (32, 3, 5, 5), fstrides=(1, 1), zero_pad=(2, 2), poolsize=(2, 2), activation_fn=op.ReLU, W=None if params is None else params['W_C1'], b=None if params is None else params['b_C1']) self.conv2 = ConvLayer('_C2', rng, self.conv1.output, (32, 32, 5, 5), fstrides=(1, 1), zero_pad=(2, 2), poolsize=(2, 2), activation_fn=op.ReLU, W=None if params is None else params['W_C2'], b=None if params is None else params['b_C2']) self.conv3 = ConvLayer('_C3', rng, self.conv2.output, (64, 32, 5, 5), fstrides=(1, 1), zero_pad=(2, 2), poolsize=(2, 2), activation_fn=op.ReLU, W=None if params is None else params['W_C3'], b=None if params is None else params['b_C3']) nin = 64 * 4 * 4 reshaped_conv_out = op.Reshape(self.conv3.output, (-1, nin)) self.fc64 = FullyConnectedLayer( '_F1', rng, reshaped_conv_out, nin, 64, activation_fn=op.ReLU, W=None if params is None else params['W_F1'], b=None if params is None else params['b_F1']) self.fc10 = FullyConnectedLayer( '_F2', rng, self.fc64.output, 64, 10, activation_fn=op.ReLU, W=None if params is None else params['W_F2'], b=None if params is None else params['b_F2']) # self.do1 = op.Dropout(self.fc10.output) self.layer3 = SoftmaxLayer( '_S1', rng, self.fc10.output, 10, n_out, W=None if params is None else params['W_S1'], b=None if params is None else params['b_S1']) self.cost = op.MeanSquaredErr(self.layer3.p_y_given_x, Y) self.error = op.Mean(op.NotEq(self.layer3.y_pred, Y)) self.params = self.conv1.params + self.conv2.params + \ self.conv3.params + self.fc64.params + self.fc10.params + \ self.layer3.params self.prev_grad = None