def test_sparse_cross_dense(self): """Tests sparse and dense inputs.""" sp_inp = self._sparse_tensor([['batch1-FC1-F1'], ['batch2-FC1-F1', 'batch2-FC1-F2']]) dense_inp = constant_op.constant([['batch1-FC2-F1', 'batch1-FC2-F2'], ['batch2-FC2-F1', 'batch2-FC2-F2']], dtypes.string) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp.indices], values=[sp_inp.values], shapes=[sp_inp.dense_shape], dense_inputs=[dense_inp], sep='_X_') expected_out = self._sparse_tensor([ ['batch1-FC1-F1_X_batch1-FC2-F1', 'batch1-FC1-F1_X_batch1-FC2-F2'], [ 'batch2-FC1-F1_X_batch2-FC2-F1', 'batch2-FC1-F1_X_batch2-FC2-F2', 'batch2-FC1-F2_X_batch2-FC2-F1', 'batch2-FC1-F2_X_batch2-FC2-F2' ] ]) out = sparse_tensor.SparseTensor(inds, vals, shapes) with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_some_columns_empty(self): """Tests when more than one columns are empty. Cross for the corresponding batch should be empty. """ sp_inp_1 = self._sparse_tensor([['batch1-FC1-F1', 'batch1-FC1-F2']], 2) sp_inp_2 = self._sparse_tensor([['batch1-FC2-F1'], ['batch2-FC2-F1']], 2) sp_inp_3 = self._sparse_tensor([['batch1-FC3-F1', 'batch1-FC3-F2']], 2) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices, sp_inp_3.indices], values=[sp_inp_1.values, sp_inp_2.values, sp_inp_3.values], shapes=[ sp_inp_1.dense_shape, sp_inp_2.dense_shape, sp_inp_3.dense_shape ], dense_inputs=[], sep='_X_') expected_out = self._sparse_tensor([[ 'batch1-FC1-F1_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F1_X_batch1-FC2-F1_X_batch1-FC3-F2', 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F2' ]], 2) out = sparse_tensor.SparseTensor(inds, vals, shapes) with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_permutation_3x1x2(self): """Tests 3x1x2 permutation.""" sp_inp_1 = self._sparse_tensor( [['batch1-FC1-F1', 'batch1-FC1-F2', 'batch1-FC1-F3']]) sp_inp_2 = self._sparse_tensor([['batch1-FC2-F1']]) sp_inp_3 = self._sparse_tensor([['batch1-FC3-F1', 'batch1-FC3-F2']]) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices, sp_inp_3.indices], values=[sp_inp_1.values, sp_inp_2.values, sp_inp_3.values], shapes=[ sp_inp_1.dense_shape, sp_inp_2.dense_shape, sp_inp_3.dense_shape ], dense_inputs=[], sep='_X_') expected_out = self._sparse_tensor([[ 'batch1-FC1-F1_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F1_X_batch1-FC2-F1_X_batch1-FC3-F2', 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F2', 'batch1-FC1-F3_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F3_X_batch1-FC2-F1_X_batch1-FC3-F2' ]]) out = sparse_tensor.SparseTensor(inds, vals, shapes) with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_integer_mixed_string_dense(self): """Tests mixed dense inputs.""" dense_inp_1 = constant_op.constant([[11, 333], [55555, 999999]], dtypes.int64) dense_inp_2 = constant_op.constant( [['batch1-FC2-F1', 'batch1-FC2-F2'], ['batch2-FC2-F1', 'batch2-FC2-F2']], dtypes.string) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[], values=[], shapes=[], dense_inputs=[dense_inp_1, dense_inp_2], sep='_X_') out = sparse_tensor.SparseTensor(inds, vals, shapes) # pyformat: disable expected_out = self._sparse_tensor([[ '11_X_batch1-FC2-F1', '11_X_batch1-FC2-F2', '333_X_batch1-FC2-F1', '333_X_batch1-FC2-F2' ], [ '55555_X_batch2-FC2-F1', '55555_X_batch2-FC2-F2', '999999_X_batch2-FC2-F1', '999999_X_batch2-FC2-F2' ]]) # pyformat: enable with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_dense_sep(self): """Tests only dense inputs.""" dense_inp_1 = constant_op.constant( [['batch1-FC1-F1', 'batch1-FC1-F2'], ['batch2-FC1-F1', 'batch2-FC1-F2']], dtypes.string) dense_inp_2 = constant_op.constant( [['batch1-FC2-F1', 'batch1-FC2-F2'], ['batch2-FC2-F1', 'batch2-FC2-F2']], dtypes.string) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[], values=[], shapes=[], dense_inputs=[dense_inp_1, dense_inp_2], sep='_') out = sparse_tensor.SparseTensor(inds, vals, shapes) # pyformat: disable expected_out = self._sparse_tensor([[ 'batch1-FC1-F1_batch1-FC2-F1', 'batch1-FC1-F1_batch1-FC2-F2', 'batch1-FC1-F2_batch1-FC2-F1', 'batch1-FC1-F2_batch1-FC2-F2' ], [ 'batch2-FC1-F1_batch2-FC2-F1', 'batch2-FC1-F1_batch2-FC2-F2', 'batch2-FC1-F2_batch2-FC2-F1', 'batch2-FC1-F2_batch2-FC2-F2' ]]) # pyformat: enable with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_sparse_sep(self): """Tests a simple scenario.""" sp_inp_1 = self._sparse_tensor([['batch1-FC1-F1'], ['batch2-FC1-F1', 'batch2-FC1-F2']]) sp_inp_2 = self._sparse_tensor([['batch1-FC2-F1'], ['batch2-FC2-F1', 'batch2-FC2-F2']]) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices], values=[sp_inp_1.values, sp_inp_2.values], shapes=[sp_inp_1.dense_shape, sp_inp_2.dense_shape], dense_inputs=[], sep='_Y_') out = sparse_tensor.SparseTensor(inds, vals, shapes) # pyformat: disable expected_out = self._sparse_tensor( [['batch1-FC1-F1_Y_batch1-FC2-F1'], [ 'batch2-FC1-F1_Y_batch2-FC2-F1', 'batch2-FC1-F1_Y_batch2-FC2-F2', 'batch2-FC1-F2_Y_batch2-FC2-F1', 'batch2-FC1-F2_Y_batch2-FC2-F2' ]]) # pyformat: enable with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_large_batch(self): """Tests with large batch size to force multithreading.""" batch_size = 5000 col1 = [] col2 = [] col3 = [] for b in range(batch_size): col1.append([ 'batch%d-FC1-F1' % b, 'batch%d-FC1-F2' % b, 'batch%d-FC1-F3' % b ]) col2.append(['batch%d-FC2-F1' % b]) col3.append(['batch%d-FC3-F1' % b, 'batch%d-FC3-F2' % b]) sp_inp_1 = self._sparse_tensor(col1) sp_inp_2 = self._sparse_tensor(col2) sp_inp_3 = self._sparse_tensor(col3) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices, sp_inp_3.indices], values=[sp_inp_1.values, sp_inp_2.values, sp_inp_3.values], shapes=[ sp_inp_1.dense_shape, sp_inp_2.dense_shape, sp_inp_3.dense_shape ], dense_inputs=[], sep='_X_') col_out = [] for b in range(batch_size): col_out.append([ 'batch%d-FC1-F1_X_batch%d-FC2-F1_X_batch%d-FC3-F1' % (b, b, b), 'batch%d-FC1-F1_X_batch%d-FC2-F1_X_batch%d-FC3-F2' % (b, b, b), 'batch%d-FC1-F2_X_batch%d-FC2-F1_X_batch%d-FC3-F1' % (b, b, b), 'batch%d-FC1-F2_X_batch%d-FC2-F1_X_batch%d-FC3-F2' % (b, b, b), 'batch%d-FC1-F3_X_batch%d-FC2-F1_X_batch%d-FC3-F1' % (b, b, b), 'batch%d-FC1-F3_X_batch%d-FC2-F1_X_batch%d-FC3-F2' % (b, b, b) ]) expected_out = self._sparse_tensor(col_out) out = sparse_tensor.SparseTensor(inds, vals, shapes) with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))
def test_all_columns_empty(self): """Tests when all columns are empty. The crossed tensor should be empty. """ sp_inp_1 = self._sparse_tensor([]) sp_inp_2 = self._sparse_tensor([]) sp_inp_3 = self._sparse_tensor([]) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices, sp_inp_3.indices], values=[sp_inp_1.values, sp_inp_2.values, sp_inp_3.values], shapes=[ sp_inp_1.dense_shape, sp_inp_2.dense_shape, sp_inp_3.dense_shape ], dense_inputs=[], sep='_X_') out = sparse_tensor.SparseTensor(inds, vals, shapes) with self.cached_session(): self._assert_sparse_tensor_empty(self.evaluate(out))
def test_integer_mixed_string_sparse(self): """Tests mixed type.""" sp_inp_1 = self._sparse_tensor([[11], [333, 55555]]) sp_inp_2 = self._sparse_tensor([['batch1-FC2-F1'], ['batch2-FC2-F1', 'batch2-FC2-F2']]) inds, vals, shapes = gen_sparse_ops.sparse_cross_v2( indices=[sp_inp_1.indices, sp_inp_2.indices], values=[sp_inp_1.values, sp_inp_2.values], shapes=[sp_inp_1.dense_shape, sp_inp_2.dense_shape], dense_inputs=[], sep='_X_') out = sparse_tensor.SparseTensor(inds, vals, shapes) # pyformat: disable expected_out = self._sparse_tensor([ ['11_X_batch1-FC2-F1'], ['333_X_batch2-FC2-F1', '333_X_batch2-FC2-F2', '55555_X_batch2-FC2-F1', '55555_X_batch2-FC2-F2' ]]) # pyformat: enable with self.cached_session(): self._assert_sparse_tensor_equals(expected_out, self.evaluate(out))