我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用theano.tensor.batched_tensordot()。
def weighted_average(inp, weights, axis=None): # n_b x n_s x 4 x n_w_a: inp if axis == 2: # for question weights = weights.flatten(ndim=2) weights /= T.sum(weights, axis=1, keepdims=True) + 0.000001 return T.batched_tensordot(inp, weights, [[inp.ndim - 1], [1]]) elif axis == 3: # for answer inp: (None, 51, 4, 20), output: (None, 4, 20, 1) weights = weights.flatten(ndim=weights.ndim - 1) weights /= T.sum(weights, axis=weights.ndim - 1, keepdims=True) + 0.000001 weights = weights.dimshuffle(0, 'x', 1, 2) return T.sum(inp * weights, axis=3) elif axis == 4: # for inner sliding window weights = weights.flatten(ndim=weights.ndim - 1) weights /= T.sum(weights, axis=weights.ndim - 1, keepdims=True) + 0.000001 weights = weights.dimshuffle(0, 'x', 'x', 1, 2) return T.sum(inp * weights, axis=4) else: raise RuntimeError
def batch_dot(x, y, axes=None): if axes is None: # behaves like tf.batch_matmul as default axes = [(x.ndim-1,), (y.ndim-2,)] return T.batched_tensordot(x, y, axes=axes)
def native_kron(a, b): return T.batched_tensordot(b, a, [[], []]).dimshuffle(0, 1, 3, 2, 4).reshape((a.shape[0], a.shape[1]*b.shape[1], -1)).mean(0, keepdims=True)
def batch_dot(x, y, axes=None): '''Batchwise dot product. batch_dot results in a tensor with less dimensions than the input. If the number of dimensions is reduced to 1, we use `expand_dims` to make sure that ndim is at least 2. # Arguments x, y: tensors with ndim >= 2 axes: list (or single) int with target dimensions # Returns A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1). # Examples Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements. Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If dot_axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape: x.shape[0] : 100 : append to output shape x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1) y.shape[0] : 100 : do not append to output shape, always ignore first dimension of y y.shape[1] : 30 : append to output shape y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30) ''' if type(axes) == int: axes = (axes, axes) if axes is None: # behaves like tf.batch_matmul as default axes = [x.ndim - 1, y.ndim - 2] out = T.batched_tensordot(x, y, axes=axes) if ndim(out) == 1: out = expand_dims(out, 1) return out
def get_output_for(self, inputs, **kwargs): """ :param inputs: inputs: list of theano.TensorType `inputs[0]` should always be the symbolic input variable. When this layer has a mask input (i.e. was instantiated with `mask_input != None`, indicating that the lengths of sequences in each batch vary), `inputs` should have length 2, where `inputs[1]` is the `mask`. The `mask` should be supplied as a Theano variable denoting whether each time step in each sequence in the batch is part of the sequence or not. `mask` should be a matrix of shape ``(n_batch, n_time_steps)`` where ``mask[i, j] = 1`` when ``j <= (length of sequence i)`` and ``mask[i, j] = 0`` when ``j > (length of sequence i)``. :return: theano.TensorType Symbolic output variable. """ input = inputs[0] mask = None if self.mask_incoming_index > 0: mask = inputs[self.mask_incoming_index] # compute the bi-affine part # first via tensor dot ([batch, length, dim] * [dim, dim, num_label]) # output shape = [batch, length, dim, num_label] out = T.tensordot(input, self.U, axes=[[2], [0]]) # second via tensor dot ([batch, length, dim, num_label] * [batch, dim, length) # output shape = [batch, length, length, num_label] out = T.batched_tensordot(out, input.dimshuffle(0, 2, 1), axes=([2], [1])) out = out.dimshuffle(0, 1, 3, 2) # compute head bias part by tensor dot ([batch, length, dim] * [dim, num_label]) # the shape of s_h should be [batch, length, num_label] if self.W_h is not None: s_h = T.tensordot(input, self.W_h, axes=[[2], [0]]) out = out + s_h.dimshuffle(0, 1, 'x', 2) # compute child part by tensor dot ([batch, length, dim] * [dim, num_label] # the shape of s_c should be [batch, length, num_label] if self.W_c is not None: s_c = T.tensordot(input, self.W_c, axes=[[2], [0]]) out = out + s_c.dimshuffle(0, 'x', 1, 2) # add bias part. if self.b is not None: out = out + self.b.dimshuffle('x', 'x', 'x', 0) if mask is not None: mask_shuffled = mask.dimshuffle(0, 1, 'x', 'x') out = out * mask_shuffled mask_shuffled = mask.dimshuffle(0, 'x', 1, 'x') out = out * mask_shuffled return out
def batch_dot(x, y, axes=None): """Batchwise dot product. batch_dot results in a tensor with less dimensions than the input. If the number of dimensions is reduced to 1, we use `expand_dims` to make sure that ndim is at least 2. # Arguments x, y: tensors with ndim >= 2 axes: list (or single) int with target dimensions # Returns A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1). # Examples Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements. Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If dot_axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape: x.shape[0] : 100 : append to output shape x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1) y.shape[0] : 100 : do not append to output shape, always ignore first dimension of y y.shape[1] : 30 : append to output shape y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30) """ # TODO: `keras_shape` inference. if isinstance(axes, int): axes = (axes, axes) if axes is None: # behaves like tf.batch_matmul as default axes = [x.ndim - 1, y.ndim - 2] out = T.batched_tensordot(x, y, axes=axes) if ndim(out) == 1: out = expand_dims(out, 1) return out
def batch_dot(x, y, axes=None): '''Batchwise dot product. batch_dot results in a tensor with less dimensions than the input. If the number of dimensions is reduced to 1, we use `expand_dims` to make sure that ndim is at least 2. # Arguments x, y: tensors with ndim >= 2 axes: list (or single) int with target dimensions # Returns A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1). # Examples Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements. Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If dot_axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape: x.shape[0] : 100 : append to output shape x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1) y.shape[0] : 100 : do not append to output shape, always ignore first dimension of y y.shape[1] : 30 : append to output shape y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30) ''' # TODO: `keras_shape` inference. if isinstance(axes, int): axes = (axes, axes) if axes is None: # behaves like tf.batch_matmul as default axes = [x.ndim - 1, y.ndim - 2] out = T.batched_tensordot(x, y, axes=axes) if ndim(out) == 1: out = expand_dims(out, 1) return out
def batch_dot(x, y, axes=None): """Batchwise dot product. batch_dot results in a tensor with less dimensions than the input. If the number of dimensions is reduced to 1, we use `expand_dims` to make sure that ndim is at least 2. # Arguments x, y: tensors with ndim >= 2 axes: list (or single) int with target dimensions # Returns A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1). # Examples Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements. Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If dot_axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape: x.shape[0] : 100 : append to output shape x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1) y.shape[0] : 100 : do not append to output shape, always ignore first dimension of y y.shape[1] : 30 : append to output shape y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30) """ if isinstance(axes, int): axes = (axes, axes) if axes is None: # behaves like tf.batch_matmul as default axes = [x.ndim - 1, y.ndim - 2] out = T.batched_tensordot(x, y, axes=axes) if ndim(out) == 1: out = expand_dims(out, 1) if hasattr(x, '_keras_shape') and hasattr(y, '_keras_shape'): shape = [] for axis in range(len(x._keras_shape)): if axis != axes[0]: shape.append(x._keras_shape[axis]) for axis in range(1, len(y._keras_shape)): if axis != axes[1]: shape.append(y._keras_shape[axis]) if len(shape) == 1: shape.append(1) # Expand dims if ndim == 1 out._keras_shape = tuple(shape) return out