tf计算矩阵维度_tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)
@tf_export("matmul")
def matmul(a,
b,
transpose_a=False,
transpose_b=False,
adjoint_a=False,
adjoint_b=False,
a_is_sparse=False,
b_is_sparse=False,
name=None):
"""Multiplies matrix `a` by matrix `b`, producing `a` * `b`.
将矩阵a与矩阵b相乘,得出a * b。
The inputs must, following any transpositions, be tensors of rank >= 2
where the inner 2 dimensions specify valid matrix multiplication arguments,
and any further outer dimensions match.
在进行任何换位后,输入必须为(秩?)> = 2的张量,其中内部2维指定有效的矩阵乘法自变量,
并且任何其他外部维匹配。
Both matrices must be of the same type. The supported types are:
`float16`, `float32`, `float64`, `int32`, `complex64`, `complex128`.
两种矩阵必须属于同一类型。 支持的类型有:`float16`,`float32`,`float64`,`int32`,`complex64`,`complex128`。
Either matrix can be transposed or adjointed (conjugated and transposed) on
the fly by setting one of the corresponding flag to `True`. These are `False`
by default.
通过将相应标志之一设置为“ True”,可以即时对矩阵进行转置或连接(共轭和转置)。 这些默认为False。
If one or both of the matrices contain a lot of zeros, a more efficient
multiplication algorithm can be used by setting the corresponding
`a_is_sparse` or `b_is_sparse` flag to `True`. These are `False` by default.
This optimization is only available for plain matrices (rank-2 tensors) with
datatypes `bfloat16` or `float32`.
如果一个或两个矩阵都包含大量零,则可以通过将相应的“ a_is_sparse”或“ b_is_sparse”标志,
设置为“ True”来使用更有效的乘法算法。 这些默认为False。
此优化仅适用于数据类型为bfloat16或float32的普通矩阵(秩2张量)。
For example:
```python
# 2-D tensor `a`
# [[1, 2, 3],
# [4, 5, 6]]
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
# 2-D tensor `b`
# [[ 7, 8],
# [ 9, 10],
# [11, 12]]
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
# `a` * `b`
# [[ 58, 64],
# [139, 154]]
c = tf.matmul(a, b)
# 3-D tensor `a`
# [[[ 1, 2, 3],
# [ 4, 5, 6]],
# [[ 7, 8, 9],
# [10, 11, 12]]]
a = tf.constant(np.arange(1, 13, dtype=np.int32),
shape=[2, 2, 3])
# 3-D tensor `b`
# [[[13, 14],
# [15, 16],
# [17, 18]],
# [[19, 20],
# [21, 22],
# [23, 24]]]
b = tf.constant(np.arange(13, 25, dtype=np.int32),
shape=[2, 3, 2])
# `a` * `b`
# [[[ 94, 100],
# [229, 244]],
# [[508, 532],
# [697, 730]]]
c = tf.matmul(a, b)
# Since python >= 3.5 the @ operator is supported (see PEP 465).
# In TensorFlow, it simply calls the `tf.matmul()` function, so the
# following lines are equivalent:
由于python> = 3.5,因此支持@运算符(请参阅PEP 465)。
在TensorFlow中,它仅调用`tf.matmul()`函数,因此以下几行是等效的:
d = a @ b @ [[10.], [11.]]
d = tf.matmul(tf.matmul(a, b), [[10.], [11.]])
Args:
a: `Tensor` of type `float16`, `float32`, `float64`, `int32`, `complex64`,
`complex128` and rank > 1.
类型为`float16`,`float32`,`float64`,`int32,`complex64`,`complex128`和秩> 1的`Tensor`。
b: `Tensor` with same type and rank as `a`.
具有与a相同类型和秩的Tensor。
transpose_a: If `True`, `a` is transposed before multiplication.
如果为True,则在相乘之前对a进行转置。
transpose_b: If `True`, `b` is transposed before multiplication.
如果为True,则在相乘之前将b换位。
adjoint_a: If `True`, `a` is conjugated and transposed before
multiplication.
如果为True,则在相乘之前对a进行共轭和转置。
adjoint_b: If `True`, `b` is conjugated and transposed before
multiplication.
如果为True,则在相乘之前对b进行共轭和转置。
a_is_sparse: If `True`, `a` is treated as a sparse matrix.
如果为True,则将a视为稀疏矩阵。
b_is_sparse: If `True`, `b` is treated as a sparse matrix.
如果为True,则将b视为稀疏矩阵。
name: Name for the operation (optional).
操作名称(可选)。
Returns:
A `Tensor` of the same type as `a` and `b` where each inner-most matrix is
the product of the corresponding matrices in `a` and `b`, e.g. if all
transpose or adjoint attributes are `False`:
与`a`和`b`具有相同类型的`张量`,其中每个最里面的矩阵是`a`和`b`中对应矩阵的乘积,
例如 如果所有转置或伴随属性均为False:
`output`[..., i, j] = sum_k (`a`[..., i, k] * `b`[..., k, j]),
for all indices i, j.
Note: This is matrix product, not element-wise product.
这是矩阵乘积,而不是元素乘积。
Raises:
ValueError: If transpose_a and adjoint_a, or transpose_b and adjoint_b
are both set to True.
"""
with ops.name_scope(name, "MatMul", [a, b]) as name:
if transpose_a and adjoint_a:
raise ValueError("Only one of transpose_a and adjoint_a can be True.")
if transpose_b and adjoint_b:
raise ValueError("Only one of transpose_b and adjoint_b can be True.")
if context.executing_eagerly():
if not isinstance(a, (ops.EagerTensor, _resource_variable_type)):
a = ops.convert_to_tensor(a, name="a")
if not isinstance(b, (ops.EagerTensor, _resource_variable_type)):
b = ops.convert_to_tensor(b, name="b")
else:
a = ops.convert_to_tensor(a, name="a")
b = ops.convert_to_tensor(b, name="b")
# TODO(apassos) remove _shape_tuple here when it is not needed.
a_shape = a._shape_tuple() # pylint: disable=protected-access
b_shape = b._shape_tuple() # pylint: disable=protected-access
if (not a_is_sparse and
not b_is_sparse) and ((a_shape is None or len(a_shape) > 2) and
(b_shape is None or len(b_shape) > 2)):
# BatchMatmul does not support transpose, so we conjugate the matrix and
# use adjoint instead. Conj() is a noop for real matrices.
if transpose_a:
a = conj(a)
adjoint_a = True
if transpose_b:
b = conj(b)
adjoint_b = True
return gen_math_ops.batch_mat_mul(
a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)
# Neither matmul nor sparse_matmul support adjoint, so we conjugate
# the matrix and use transpose instead. Conj() is a noop for real
# matrices.
if adjoint_a:
a = conj(a)
transpose_a = True
if adjoint_b:
b = conj(b)
transpose_b = True
use_sparse_matmul = False
if a_is_sparse or b_is_sparse:
sparse_matmul_types = [dtypes.bfloat16, dtypes.float32]
use_sparse_matmul = (
a.dtype in sparse_matmul_types and b.dtype in sparse_matmul_types)
if ((a.dtype == dtypes.bfloat16 or b.dtype == dtypes.bfloat16) and
a.dtype != b.dtype):
# matmul currently doesn't handle mixed-precision inputs.
use_sparse_matmul = True
if use_sparse_matmul:
ret = sparse_matmul(
a,
b,
transpose_a=transpose_a,
transpose_b=transpose_b,
a_is_sparse=a_is_sparse,
b_is_sparse=b_is_sparse,
name=name)
# sparse_matmul always returns float32, even with
# bfloat16 inputs. This prevents us from configuring bfloat16 training.
# casting to bfloat16 also matches non-sparse matmul behavior better.
if a.dtype == dtypes.bfloat16 and b.dtype == dtypes.bfloat16:
ret = cast(ret, dtypes.bfloat16)
return ret
else:
return gen_math_ops.mat_mul(
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
tf计算矩阵维度_tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)相关推荐
- MATLAB“内部矩阵维度必须一致”(及要注意数组的乘法运算是要带点)
举出这一题为例子 第一遍输出报错 修改后 图中可见连接exp与sin函数之间的乘号*前面如果没有"."则会报错内部矩阵维度必须一致 明显MATLAB把输入的式子当作矩阵处理了,而其 ...
- tf计算矩阵维度_tf.matmul() 和tf.multiply() 的区别
1.tf.multiply()两个矩阵中对应元素各自相乘 格式: tf.multiply(x, y, name=None) 参数: x: 一个类型为:half, float32, float64, u ...
- TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add
1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...
- python 增加维度_Python3 Tensorlfow:增加或者减小矩阵维度的实现
1.增加维度 下面给出两个样例 样例1: [1, 2, 3] ==> [[1],[2],[3]] import tensorflow as tf a = tf.constant([1, 2, 3 ...
- python维度扩展_在TensorFlow中实现矩阵维度扩展
一般TensorFlow中扩展维度可以使用tf.expand_dims().近来发现另一种可以直接运用取数据操作符[]就能扩展维度的方法. 用法很简单,在要扩展的维度上加上tf.newaxis就行了. ...
- TF之LiR:利用TF自定义一个线性分类器LiR对乳腺癌肿瘤数据集进行二分类预测(良/恶性)
TF之LiR:利用TF自定义一个线性分类器LiR对乳腺癌肿瘤数据集进行二分类预测(良/恶性) 目录 输出结果 设计思路 核心代码 输出结果 设计思路 核心代码 X_train = np.float32 ...
- TensorFlow tf.data 导入数据(tf.data官方教程) * * * * *
原文链接:https://blog.csdn.net/u014061630/article/details/80728694 TensorFlow版本:1.10.0 > Guide > I ...
- 学习笔记:几种矩阵乘法(matmul product普通乘积、hadamard product矩阵点乘、kronecker product克罗内克积、斯特拉森矩阵乘法)
1.普通矩阵乘法(matmul product) 假设矩阵A大小是M*N,矩阵B大小是N*P,C=AB 这里选取一个例子 这里的矩阵乘法要求相乘的两个矩阵一个的行数得等于另一个的列数,否则,无法进行乘 ...
- matlab中除法的使用,错误使用 / 矩阵维度必须一致
错误使用 / 矩阵维度必须一致. 当出现这个错误,说明除数符号写错了,需要从/变成./就可以了 matlab中的乘除法: 1.数字之间相乘/相除 >> a=3; >> b= ...
最新文章
- 演示:思科设备基于物理接口帧中继(fame-relay)的配置
- jdk是什么?jdk1.8安装配置方法
- 矩形变弧度角_在上海做下颌角整形这些医生错过后悔都来不及,案例分享
- 小憩,味一二 ——08年3月编程手札
- C++使用Merge Sort排序计数反转的实现算法(附完整源码)
- android 对话框白色样式,Android 对话框(Dialog)样式大全以及简单实现
- vue-resource安装
- Jmeter JDBC Request执行多条SQL语句
- Python Imaging Library: ImageColor Module(图像颜色模块)
- js平滑滚动到顶部,底部,指定地方 animate()
- 详解机器学习之the Learning Problem
- [verilog] 八位比较器
- apt cyg 安装php,Windows下安装Cygwin及apt-cyg
- OCSP 在SSL证书中起什么作用
- 手把手教你反编译小程序
- jquery.fn jquery.extend jquery.fn.extend
- 梨视频中的旅行短视频怎么批量下载到电脑中
- linux之CLUSTER(集群)一
- 通过R语言实现平稳时间序列的建模--基础(ARMA模型)
- 如何设计安全可靠的开放接口---之AppId、AppSecret