MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _linear

Function _linear

tensorflow/contrib/rnn/python/ops/core_rnn_cell.py:126–188  ·  view source on GitHub ↗

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 W[i]. bias: boolean, whether to add a bias term or not. bias_initializer: starting value to initialize the bias (def

(args,
            output_size,
            bias,
            bias_initializer=None,
            kernel_initializer=None)

Source from the content-addressed store, hash-verified

124
125# TODO(xpan): Remove this function in a follow up.
126def _linear(args,
127 output_size,
128 bias,
129 bias_initializer=None,
130 kernel_initializer=None):
131 """Linear map: sum_i(args[i] * W[i]), where W[i] is a variable.
132
133 Args:
134 args: a 2D Tensor or a list of 2D, batch, n, Tensors.
135 output_size: int, second dimension of W[i].
136 bias: boolean, whether to add a bias term or not.
137 bias_initializer: starting value to initialize the bias
138 (default is all zeros).
139 kernel_initializer: starting value to initialize the weight.
140
141 Returns:
142 A 2D Tensor with shape `[batch, output_size]` equal to
143 sum_i(args[i] * W[i]), where W[i]s are newly created matrices.
144
145 Raises:
146 ValueError: if some of the arguments has unspecified or wrong shape.
147 """
148 if args is None or (nest.is_sequence(args) and not args):
149 raise ValueError("`args` must be specified")
150 if not nest.is_sequence(args):
151 args = [args]
152
153 # Calculate the total size of arguments on dimension 1.
154 total_arg_size = 0
155 shapes = [a.get_shape() for a in args]
156 for shape in shapes:
157 if shape.ndims != 2:
158 raise ValueError("linear is expecting 2D arguments: %s" % shapes)
159 if shape.dims[1].value is None:
160 raise ValueError("linear expects shape[1] to be provided for shape %s, "
161 "but saw %s" % (shape, shape[1]))
162 else:
163 total_arg_size += shape.dims[1].value
164
165 dtype = [a.dtype for a in args][0]
166
167 # Now the computation.
168 scope = vs.get_variable_scope()
169 with vs.variable_scope(scope) as outer_scope:
170 weights = vs.get_variable(
171 _WEIGHTS_VARIABLE_NAME, [total_arg_size, output_size],
172 dtype=dtype,
173 initializer=kernel_initializer)
174 if len(args) == 1:
175 res = math_ops.matmul(args[0], weights)
176 else:
177 res = math_ops.matmul(array_ops.concat(args, 1), weights)
178 if not bias:
179 return res
180 with vs.variable_scope(outer_scope) as inner_scope:
181 inner_scope.set_partitioner(None)
182 if bias_initializer is None:
183 bias_initializer = init_ops.constant_initializer(0.0, dtype=dtype)

Callers

nothing calls this directly

Calls 6

variable_scopeMethod · 0.80
set_partitionerMethod · 0.80
get_shapeMethod · 0.45
get_variableMethod · 0.45
matmulMethod · 0.45
concatMethod · 0.45

Tested by

no test coverage detected