def test_constant(self): a = xdl.convert_to_tensor(1) b = xdl.convert_to_tensor([10, 20]) c = xdl.convert_to_tensor(np.array([30, 40])) a, b, c = xdl.execute([a, b, c]) self.assertTrue(a == 1) self.assertTrue((b == np.array([10, 20])).all()) self.assertTrue((c == np.array([30, 40])).all())
def test_all(self): dense = xdl.mock_dense_op(shape=[1, 16], value=0.01, name_="dense") labels = xdl.mock_dense_op(shape=[1, 1], value=1.0, name_="label") ids = xdl.convert_to_tensor( np.array([[0, 0], [0, 1], [0, 2]], dtype=np.int64)) values = xdl.convert_to_tensor( np.array([1.0, 2.0, 3.0], dtype=np.float32)) segments = xdl.convert_to_tensor(np.array([3], dtype=np.int32)) sparse = xdl.SparseTensor(ids, values, segments) emb = xdl.embedding("sparse", sparse, xdl.Ones(), 1, 16, 'sum', vtype='hash') loss = model(dense, emb, labels) train_op = xdl.SGD(0.5).optimize() sess = xdl.TrainSession() _, l, g = sess.run( [train_op, loss, xdl.get_sparse_grads('sparse').grad]) self.assertTrue((l == np.array(0.0024364376, dtype=np.float32)).all()) self.assertTrue( (g == np.array([[-0.002433472], [-0.004866944], [-0.007300416]], dtype=np.float32)).all()) sparse_var = xdl.get_variable_by_name('sparse') weights = sess.run( sparse_var.gather( np.array([[0, 0], [0, 1], [0, 2]], dtype=np.int64))) self.assertTrue( (weights == np.array([[1.0012168], [1.0024334], [1.0036502]], dtype=np.float32)).all()) _, l, g = sess.run( [train_op, loss, xdl.get_sparse_grads('sparse').grad]) self.assertTrue((l == np.array(0.002395329, dtype=np.float32)).all()) self.assertTrue( (g == np.array([[-0.0023924622], [-0.0047849244], [-0.0071773864]], dtype=np.float32)).all()) weights = sess.run( sparse_var.gather( np.array([[0, 0], [0, 1], [0, 2]], dtype=np.int64))) self.assertTrue( (weights == np.array([[1.002413], [1.0048258], [1.0072389]], dtype=np.float32)).all())
def main(): dense = xdl.mock_dense_op(shape=[1, 16], value=0.01, name_="dense") labels = xdl.mock_dense_op(shape=[1, 1], value=1.0, name_="label") ids = xdl.convert_to_tensor( np.array([[0, 0], [0, 1], [0, 2]], dtype=np.int64)) values = xdl.convert_to_tensor(np.array([1.0, 2.0, 3.0], dtype=np.float32)) segments = xdl.convert_to_tensor(np.array([3], dtype=np.int32)) sparse = xdl.SparseTensor(ids, values, segments) emb = xdl.embedding("sparse", sparse, xdl.Ones(), 1, 16, 'sum', vtype='hash') loss = model(dense, emb, labels) train_op = xdl.SGD(0.5).optimize() sess = xdl.TrainSession() loss, gradients = sess.run([loss, xdl.get_sparse_grads('sparse').grad]) return loss, gradients
def mock_embedding(name1, name2): ids = xdl.convert_to_tensor( np.array([[0, 0], [0, 1], [0, 2]], dtype=np.int64)) values = xdl.convert_to_tensor(np.array([1.0, 2.0, 3.0], dtype=np.float32)) segments = xdl.convert_to_tensor(np.array([3], dtype=np.int32)) sparse = xdl.SparseTensor(ids, values, segments) emb = xdl.embedding(name1, sparse, xdl.Ones(), embed_dim, 16, 'sum', vtype='hash') emb.set_shape((1, 3)) ids2 = xdl.convert_to_tensor( np.array([[0, 1], [0, 2], [1, 1]], dtype=np.int64)) values2 = xdl.convert_to_tensor(np.array([1.0, 2.0, 3.0], dtype=np.float32)) segments2 = xdl.convert_to_tensor(np.array([3], dtype=np.int32)) sparse2 = xdl.SparseTensor(ids2, values2, segments2) emb2 = xdl.embedding(name2, sparse2, xdl.Ones(), embed_dim, 16, 'sum', vtype='hash') emb2.set_shape((1, 3)) return [emb, emb2]