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, n, Tensors. output_size: int, second dimension of weight variable. dtype: data type for variables. build_bias: boolean, whether to build a bias variable. bias_initializ
| 46 | |
| 47 | |
| 48 | class _Linear(object): |
| 49 | """Linear map: sum_i(args[i] * W[i]), where W[i] is a variable. |
| 50 | |
| 51 | Args: |
| 52 | args: a 2D Tensor or a list of 2D, batch, n, Tensors. |
| 53 | output_size: int, second dimension of weight variable. |
| 54 | dtype: data type for variables. |
| 55 | build_bias: boolean, whether to build a bias variable. |
| 56 | bias_initializer: starting value to initialize the bias |
| 57 | (default is all zeros). |
| 58 | kernel_initializer: starting value to initialize the weight. |
| 59 | |
| 60 | Raises: |
| 61 | ValueError: if inputs_shape is wrong. |
| 62 | """ |
| 63 | |
| 64 | def __init__(self, |
| 65 | args, |
| 66 | output_size, |
| 67 | build_bias, |
| 68 | bias_initializer=None, |
| 69 | kernel_initializer=None): |
| 70 | self._build_bias = build_bias |
| 71 | |
| 72 | if args is None or (nest.is_sequence(args) and not args): |
| 73 | raise ValueError("`args` must be specified") |
| 74 | if not nest.is_sequence(args): |
| 75 | args = [args] |
| 76 | self._is_sequence = False |
| 77 | else: |
| 78 | self._is_sequence = True |
| 79 | |
| 80 | # Calculate the total size of arguments on dimension 1. |
| 81 | total_arg_size = 0 |
| 82 | shapes = [a.get_shape() for a in args] |
| 83 | for shape in shapes: |
| 84 | if shape.ndims != 2: |
| 85 | raise ValueError("linear is expecting 2D arguments: %s" % shapes) |
| 86 | if shape.dims[1].value is None: |
| 87 | raise ValueError("linear expects shape[1] to be provided for shape %s, " |
| 88 | "but saw %s" % (shape, shape[1])) |
| 89 | else: |
| 90 | total_arg_size += shape.dims[1].value |
| 91 | |
| 92 | dtype = [a.dtype for a in args][0] |
| 93 | |
| 94 | scope = vs.get_variable_scope() |
| 95 | with vs.variable_scope(scope) as outer_scope: |
| 96 | self._weights = vs.get_variable( |
| 97 | _WEIGHTS_VARIABLE_NAME, [total_arg_size, output_size], |
| 98 | dtype=dtype, |
| 99 | initializer=kernel_initializer) |
| 100 | if build_bias: |
| 101 | with vs.variable_scope(outer_scope) as inner_scope: |
| 102 | inner_scope.set_partitioner(None) |
| 103 | if bias_initializer is None: |
| 104 | bias_initializer = init_ops.constant_initializer(0.0, dtype=dtype) |
| 105 | self._biases = vs.get_variable( |