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 weight variable. dtype: data type for variables. build_bias: boolean, whether to build a bias variable. bias_initializ
| 12 | _WEIGHTS_VARIABLE_NAME = "kernel" |
| 13 | |
| 14 | class _Linear(object): |
| 15 | """Linear map: sum_i(args[i] * W[i]), where W[i] is a variable. |
| 16 | Args: |
| 17 | args: a 2D Tensor or a list of 2D, batch x n, Tensors. |
| 18 | output_size: int, second dimension of weight variable. |
| 19 | dtype: data type for variables. |
| 20 | build_bias: boolean, whether to build a bias variable. |
| 21 | bias_initializer: starting value to initialize the bias |
| 22 | (default is all zeros). |
| 23 | kernel_initializer: starting value to initialize the weight. |
| 24 | Raises: |
| 25 | ValueError: if inputs_shape is wrong. |
| 26 | """ |
| 27 | |
| 28 | |
| 29 | def __init__(self, |
| 30 | args, |
| 31 | output_size, |
| 32 | build_bias, |
| 33 | bias_initializer=None, |
| 34 | kernel_initializer=None): |
| 35 | self._build_bias = build_bias |
| 36 | |
| 37 | if args is None or (nest.is_sequence(args) and not args): |
| 38 | raise ValueError("`args` must be specified") |
| 39 | if not nest.is_sequence(args): |
| 40 | args = [args] |
| 41 | self._is_sequence = False |
| 42 | else: |
| 43 | self._is_sequence = True |
| 44 | |
| 45 | # Calculate the total size of arguments on dimension 1. |
| 46 | total_arg_size = 0 |
| 47 | shapes = [a.get_shape() for a in args] |
| 48 | for shape in shapes: |
| 49 | if shape.ndims != 2: |
| 50 | raise ValueError("linear is expecting 2D arguments: %s" % shapes) |
| 51 | if shape[1].value is None: |
| 52 | raise ValueError("linear expects shape[1] to be provided for shape %s, " |
| 53 | "but saw %s" % (shape, shape[1])) |
| 54 | else: |
| 55 | total_arg_size += shape[1].value |
| 56 | |
| 57 | dtype = [a.dtype for a in args][0] |
| 58 | |
| 59 | scope = vs.get_variable_scope() |
| 60 | with vs.variable_scope(scope) as outer_scope: |
| 61 | self._weights = vs.get_variable( |
| 62 | _WEIGHTS_VARIABLE_NAME, [total_arg_size, output_size], |
| 63 | dtype=dtype, |
| 64 | initializer=kernel_initializer) |
| 65 | if build_bias: |
| 66 | with vs.variable_scope(outer_scope) as inner_scope: |
| 67 | inner_scope.set_partitioner(None) |
| 68 | if bias_initializer is None: |
| 69 | bias_initializer = init_ops.constant_initializer(0.0, dtype=dtype) |
| 70 | self._biases = vs.get_variable( |
| 71 | _BIAS_VARIABLE_NAME, [output_size], |