Linear map: sum_i(args[i] * W[i]), where W[i] is a Variable. Args: args: a 2D Tensor or a list of 2D, batch x n, Tensors. output_size: int, second dimension of W[i]. bias: boolean, whether to add a bias term or not. bias_initializer: starting value to initialize the bias
(self,
args,
output_size,
bias,
bias_initializer=None,
kernel_initializer=None,
layer_norm=False)
| 2571 | return self._output_size |
| 2572 | |
| 2573 | def _linear(self, |
| 2574 | args, |
| 2575 | output_size, |
| 2576 | bias, |
| 2577 | bias_initializer=None, |
| 2578 | kernel_initializer=None, |
| 2579 | layer_norm=False): |
| 2580 | """Linear map: sum_i(args[i] * W[i]), where W[i] is a Variable. |
| 2581 | |
| 2582 | Args: |
| 2583 | args: a 2D Tensor or a list of 2D, batch x n, Tensors. |
| 2584 | output_size: int, second dimension of W[i]. |
| 2585 | bias: boolean, whether to add a bias term or not. |
| 2586 | bias_initializer: starting value to initialize the bias |
| 2587 | (default is all zeros). |
| 2588 | kernel_initializer: starting value to initialize the weight. |
| 2589 | layer_norm: boolean, whether to apply layer normalization. |
| 2590 | |
| 2591 | |
| 2592 | Returns: |
| 2593 | A 2D Tensor with shape [batch x output_size] taking value |
| 2594 | sum_i(args[i] * W[i]), where each W[i] is a newly created Variable. |
| 2595 | |
| 2596 | Raises: |
| 2597 | ValueError: if some of the arguments has unspecified or wrong shape. |
| 2598 | """ |
| 2599 | if args is None or (nest.is_sequence(args) and not args): |
| 2600 | raise ValueError("`args` must be specified") |
| 2601 | if not nest.is_sequence(args): |
| 2602 | args = [args] |
| 2603 | |
| 2604 | # Calculate the total size of arguments on dimension 1. |
| 2605 | total_arg_size = 0 |
| 2606 | shapes = [a.get_shape() for a in args] |
| 2607 | for shape in shapes: |
| 2608 | if shape.ndims != 2: |
| 2609 | raise ValueError("linear is expecting 2D arguments: %s" % shapes) |
| 2610 | if tensor_shape.dimension_value(shape[1]) is None: |
| 2611 | raise ValueError("linear expects shape[1] to be provided for shape %s, " |
| 2612 | "but saw %s" % (shape, shape[1])) |
| 2613 | else: |
| 2614 | total_arg_size += tensor_shape.dimension_value(shape[1]) |
| 2615 | |
| 2616 | dtype = [a.dtype for a in args][0] |
| 2617 | |
| 2618 | # Now the computation. |
| 2619 | scope = vs.get_variable_scope() |
| 2620 | with vs.variable_scope(scope) as outer_scope: |
| 2621 | weights = vs.get_variable( |
| 2622 | "kernel", [total_arg_size, output_size], |
| 2623 | dtype=dtype, |
| 2624 | initializer=kernel_initializer) |
| 2625 | if len(args) == 1: |
| 2626 | res = math_ops.matmul(args[0], weights) |
| 2627 | else: |
| 2628 | res = math_ops.matmul(array_ops.concat(args, 1), weights) |
| 2629 | if not bias: |
| 2630 | return res |
no test coverage detected