aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/sparse_concat_op_test.py
blob: 0f5650b89cf21c130d4ccd921c666a601e1029a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""Tests for SparseConcat."""

import tensorflow.python.platform

import numpy as np
import tensorflow as tf


class SparseConcatTest(tf.test.TestCase):

  def _SparseTensor_UnknownShape(self, ind_shape=None, val_shape=None,
                                 shape_shape=None):
    return tf.SparseTensor(
        tf.placeholder(tf.int64, shape=ind_shape),
        tf.placeholder(tf.float32, shape=val_shape),
        tf.placeholder(tf.int64, shape=shape_shape))

  def _SparseTensor_3x3(self):
    # [    1]
    # [2    ]
    # [3   4]
    ind = np.array([[0, 2], [1, 0], [2, 0], [2, 2]])
    val = np.array([1, 2, 3, 4])
    shape = np.array([3, 3])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.float32),
        tf.constant(shape, tf.int64))

  def _SparseTensor_3x5(self):
    # [         ]
    # [  1      ]
    # [2     1 0]
    ind = np.array([[1, 1], [2, 0], [2, 3], [2, 4]])
    val = np.array([1, 2, 1, 0])
    shape = np.array([3, 5])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.float32),
        tf.constant(shape, tf.int64))

  def _SparseTensor_3x2(self):
    # [   ]
    # [1  ]
    # [2  ]
    ind = np.array([[1, 0], [2, 0]])
    val = np.array([1, 2])
    shape = np.array([3, 2])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.float32),
        tf.constant(shape, tf.int64))

  def _SparseTensor_2x3(self):
    # [  1  ]
    # [1   2]
    ind = np.array([[0, 1], [1, 0], [1, 2]])
    val = np.array([1, 1, 2])
    shape = np.array([2, 3])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.float32),
        tf.constant(shape, tf.int64))

  def _SparseTensor_2x3x4(self):
    ind = np.array([
        [0, 0, 1],
        [0, 1, 0], [0, 1, 2],
        [1, 0, 3],
        [1, 1, 1], [1, 1, 3],
        [1, 2, 2]])
    val = np.array([1, 10, 12, 103, 111, 113, 122])
    shape = np.array([2, 3, 4])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.float32),
        tf.constant(shape, tf.int64))

  def _SparseTensor_String3x3(self):
    # [    a]
    # [b    ]
    # [c   d]
    ind = np.array([[0, 2], [1, 0], [2, 0], [2, 2]])
    val = np.array(["a", "b", "c", "d"])
    shape = np.array([3, 3])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.string),
        tf.constant(shape, tf.int64))

  def _SparseTensor_String3x5(self):
    # [         ]
    # [  e      ]
    # [f     g h]
    ind = np.array([[1, 1], [2, 0], [2, 3], [2, 4]])
    val = np.array(["e", "f", "g", "h"])
    shape = np.array([3, 5])
    return tf.SparseTensor(
        tf.constant(ind, tf.int64),
        tf.constant(val, tf.string),
        tf.constant(shape, tf.int64))

  def testConcat1(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A):
      # [    1]
      # [2    ]
      # [3   4]
      sp_a = self._SparseTensor_3x3()

      sp_concat = tf.sparse_concat(1, [sp_a])

      self.assertEqual(sp_concat.indices.get_shape(), [4, 2])
      self.assertEqual(sp_concat.values.get_shape(), [4])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices, [[0, 2], [1, 0], [2, 0], [2, 2]])
      self.assertAllEqual(concat_out.values, [1, 2, 3, 4])
      self.assertAllEqual(concat_out.shape, [3, 3])

  def testConcat2(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A, B):
      # [    1          ]
      # [2       1      ]
      # [3   4 2     1 0]
      sp_a = self._SparseTensor_3x3()
      sp_b = self._SparseTensor_3x5()

      sp_concat = tf.sparse_concat(1, [sp_a, sp_b])

      self.assertEqual(sp_concat.indices.get_shape(), [8, 2])
      self.assertEqual(sp_concat.values.get_shape(), [8])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices,
          [[0, 2], [1, 0], [1, 4], [2, 0], [2, 2], [2, 3], [2, 6], [2, 7]])
      self.assertAllEqual(concat_out.values, [1, 2, 1, 3, 4, 2, 1, 0])
      self.assertAllEqual(concat_out.shape, [3, 8])

  def testConcatDim0(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A, D):
      # [    1]
      # [2    ]
      # [3   4]
      # [  1  ]
      # [1   2]
      sp_a = self._SparseTensor_3x3()
      sp_d = self._SparseTensor_2x3()

      sp_concat = tf.sparse_concat(0, [sp_a, sp_d])

      self.assertEqual(sp_concat.indices.get_shape(), [7, 2])
      self.assertEqual(sp_concat.values.get_shape(), [7])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices,
          [[0, 2], [1, 0], [2, 0], [2, 2], [3, 1], [4, 0], [4, 2]])
      self.assertAllEqual(
          concat_out.values, np.array([1, 2, 3, 4, 1, 1, 2]))
      self.assertAllEqual(
          concat_out.shape, np.array([5, 3]))

  def testConcat3(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A, B, C):
      # [    1              ]
      # [2       1       1  ]
      # [3   4 2     1 0 2  ]
      sp_a = self._SparseTensor_3x3()
      sp_b = self._SparseTensor_3x5()
      sp_c = self._SparseTensor_3x2()

      sp_concat = tf.sparse_concat(1, [sp_a, sp_b, sp_c])

      self.assertEqual(sp_concat.indices.get_shape(), [10, 2])
      self.assertEqual(sp_concat.values.get_shape(), [10])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices,
          [[0, 2], [1, 0], [1, 4], [1, 8], [2, 0], [2, 2], [2, 3], [2, 6],
           [2, 7], [2, 8]])
      self.assertAllEqual(concat_out.values, [1, 2, 1, 1, 3, 4, 2, 1, 0, 2])
      self.assertAllEqual(concat_out.shape, [3, 10])

  def testConcatNonNumeric(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A, B):
      # [    a          ]
      # [b       e      ]
      # [c   d f     g h]
      sp_a = self._SparseTensor_String3x3()
      sp_b = self._SparseTensor_String3x5()

      sp_concat = tf.sparse_concat(1, [sp_a, sp_b])

      self.assertEqual(sp_concat.indices.get_shape(), [8, 2])
      self.assertEqual(sp_concat.values.get_shape(), [8])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices,
          [[0, 2], [1, 0], [1, 4], [2, 0], [2, 2], [2, 3], [2, 6], [2, 7]])
      self.assertAllEqual(
          concat_out.values, ["a", "b", "e", "c", "d", "f", "g", "h"])
      self.assertAllEqual(concat_out.shape, [3, 8])

  def testMismatchedRank(self):
    with self.test_session(use_gpu=False):
      sp_a = self._SparseTensor_3x3()
      sp_e = self._SparseTensor_2x3x4()

      # Rank mismatches can be caught at shape-inference time
      with self.assertRaises(ValueError):
        tf.sparse_concat(1, [sp_a, sp_e])

  def testMismatchedShapes(self):
    with self.test_session(use_gpu=False) as sess:
      sp_a = self._SparseTensor_3x3()
      sp_b = self._SparseTensor_3x5()
      sp_c = self._SparseTensor_3x2()
      sp_d = self._SparseTensor_2x3()
      sp_concat = tf.sparse_concat(1, [sp_a, sp_b, sp_c, sp_d])

      # Shape mismatches can only be caught when the op is run
      with self.assertRaisesOpError("Input shapes must match"):
        sess.run(sp_concat)

  def testShapeInferenceUnknownShapes(self):
    with self.test_session(use_gpu=False):
      sp_inputs = [
          self._SparseTensor_UnknownShape(),
          self._SparseTensor_UnknownShape(val_shape=[3]),
          self._SparseTensor_UnknownShape(ind_shape=[1, 3]),
          self._SparseTensor_UnknownShape(shape_shape=[3])]

      sp_concat = tf.sparse_concat(0, sp_inputs)

      self.assertEqual(sp_concat.indices.get_shape().as_list(), [None, 3])
      self.assertEqual(sp_concat.values.get_shape().as_list(), [None])
      self.assertEqual(sp_concat.shape.get_shape(), [3])


if __name__ == "__main__":
  tf.test.main()