(self, dtype)
| 77 | self.assertAllEqual(result[2:, :], p2) |
| 78 | |
| 79 | def _testRandom(self, dtype): |
| 80 | # Random dims of rank 5 |
| 81 | shape = np.random.randint(1, 5, size=5) |
| 82 | # Random number of tensors, but always > 1. |
| 83 | num_tensors = np.random.randint(2, 10) |
| 84 | # Random dim to concat on |
| 85 | concat_dim = np.random.randint(5) |
| 86 | params = {} |
| 87 | if dtype == dtypes.bfloat16: |
| 88 | dtype_feed = dtypes.float32 |
| 89 | else: |
| 90 | dtype_feed = dtype |
| 91 | with self.session(): |
| 92 | p = [] |
| 93 | for i in np.arange(num_tensors): |
| 94 | input_shape = shape |
| 95 | input_shape[concat_dim] = np.random.randint(1, 5) |
| 96 | placeholder = array_ops.placeholder(dtype_feed, shape=input_shape) |
| 97 | p.append(placeholder) |
| 98 | |
| 99 | t = dtype_feed.as_numpy_dtype |
| 100 | params[placeholder] = np.random.rand(*input_shape).astype(t) |
| 101 | |
| 102 | if dtype != dtype_feed: |
| 103 | concat_inputs = [math_ops.cast(p_i, dtype) for p_i in p] |
| 104 | else: |
| 105 | concat_inputs = p |
| 106 | with self.test_scope(): |
| 107 | c = array_ops.concat(concat_inputs, concat_dim) |
| 108 | if dtype != dtype_feed: |
| 109 | c = math_ops.cast(c, dtype_feed) |
| 110 | result = c.eval(feed_dict=params) |
| 111 | |
| 112 | self.assertEqual(result.shape, c.get_shape()) |
| 113 | cur_offset = 0 |
| 114 | |
| 115 | for i in np.arange(num_tensors): |
| 116 | # The index into the result is the ':' along all dimensions |
| 117 | # except the concat_dim. slice(0, size) is used for ':', and |
| 118 | # a list of slices is used to index into result. |
| 119 | ind = [slice(0, params[p[i]].shape[j]) for j in np.arange(5)] |
| 120 | ind[concat_dim] = slice(cur_offset, |
| 121 | cur_offset + params[p[i]].shape[concat_dim]) |
| 122 | cur_offset += params[p[i]].shape[concat_dim] |
| 123 | if dtype == dtype_feed: |
| 124 | self.assertAllEqual(result[ind], params[p[i]]) |
| 125 | else: |
| 126 | self.assertAllClose(result[ind], params[p[i]], 0.01) |
| 127 | |
| 128 | def testRandom(self): |
| 129 | self._testRandom(dtypes.float32) |
no test coverage detected