Collapse all dimensions of a tensor but the last. This function also returns a function to unflatten any tensors to recover the original shape (aside from the last dimension). This is useful for interfacing with functions that are agnostic to all dimensions but the last. For example, if we
(t)
| 161 | |
| 162 | |
| 163 | def flatten(t): |
| 164 | """Collapse all dimensions of a tensor but the last. |
| 165 | |
| 166 | This function also returns a function to unflatten any tensors to recover |
| 167 | the original shape (aside from the last dimension). This is useful for |
| 168 | interfacing with functions that are agnostic to all dimensions but the last. |
| 169 | For example, if we want to apply a linear projection to a batched sequence |
| 170 | of embeddings: |
| 171 | |
| 172 | t = tf.random_uniform([batch_size, sequence_length, embedding_size]) |
| 173 | w = tf.get_variable("w", [embedding_size, projection_size]) |
| 174 | flat_t, unflatten = flatten(t) |
| 175 | flat_projected = tf.matmul(flat_t, w) |
| 176 | projected = unflatten(flat_projected) |
| 177 | |
| 178 | Args: |
| 179 | t: [dim_1, ..., dim_(n-1), dim_n] |
| 180 | |
| 181 | Returns: |
| 182 | output: [dim_1 * ... * dim_(n-1), dim_n] |
| 183 | _unflatten: A function that when called with a flattened tensor returns the |
| 184 | unflattened version, i.e. reshapes any tensor with shape |
| 185 | [dim_1 * ... * dim_(n-1), dim_new] to [dim_1, ..., dim_(n-1), dim_new]. |
| 186 | """ |
| 187 | input_shape = shape(t) |
| 188 | if len(input_shape) > 2: |
| 189 | t = tf.reshape(t, [-1, input_shape[-1]]) |
| 190 | |
| 191 | def _unflatten(flat_t): |
| 192 | if len(input_shape) > 2: |
| 193 | return tf.reshape(flat_t, input_shape[:-1] + [shape(flat_t, -1)]) |
| 194 | else: |
| 195 | return flat_t |
| 196 | |
| 197 | return t, _unflatten |
| 198 | |
| 199 | |
| 200 | def transpose_batch_time(tensor): |
no test coverage detected
searching dependent graphs…