tensorflow 实现 前一个tensor 的最后一维 矩阵相乘 后一个tensor 的第一维
2017-11-21 16:01
477 查看
import tensorflow as tf def multiply_tensors(tensor1, tensor2): """Multiplies two tensors in a matrix-like multiplication based on the last dimension of the first tensor and first dimension of the second tensor. Inputs: tensor1: A tensor of shape [a, b, c, .., x] tensor2: A tensor of shape [x, d, e, f, ...] Outputs: A tensor of shape [a, b, c, ..., d, e, f, ...] """ sh1 = tf.shape(tensor1) sh2 = tf.shape(tensor2) len_sh1 = len(tensor1.get_shape()) len_sh2 = len(tensor2.get_shape()) prod1 = tf.constant(1, dtype=tf.int32) sh1_list = [] for z in range(len_sh1 - 1): sh1_z = sh1[z] prod1 *= sh1_z sh1_list.append(sh1_z) prod2 = tf.constant(1, dtype=tf.int32) sh2_list = [] for z in range(len_sh2 - 1): sh2_z = sh2[len_sh2 - 1 - z] prod2 *= sh2_z sh2_list.append(sh2_z) reshape_1 = tf.reshape(tensor1, [prod1, sh1[len_sh1 - 1]]) reshape_2 = tf.reshape(tensor2, [sh2[0], prod2]) result = tf.reshape(tf.matmul(reshape_1, reshape_2), sh1_list + sh2_list) assert len(result.get_shape()) == len_sh1 + len_sh2 - 2 return result
相关文章推荐
- opencv 矩阵与一个常数相乘的 两种实现方法
- opencv 矩阵与一个常数相乘的 两种实现方法
- Tensorflow 入门学习3 Hello TensorFlow 建立一个隐藏层的神经网络实现回归分析
- opencv 矩阵与一个常数相乘的 两种实现方法
- tensorflow实现简单的矩阵相乘
- 使用TensorFlow实现一个文本分类的卷积神经网络Implementing a CNN for Text Classification in TensorFlow
- opencv 矩阵与一个常数相乘的 两种实现方法
- opencv 矩阵与一个常数相乘的 两种实现方法
- python实现:使用二分查找,查找有序数组中,一个数字最后出现的下标
- java实现矩阵乘法public class MatrixMultiply { //A=A*B 将相乘的结果赋给A public static void Multiply(double[][
- 将对称矩阵压缩存储,实现矩阵相乘,输出相乘后结果(用二维数组)
- MapReduce实现矩阵相乘
- Erlang实现的矩阵相乘C=A*B单线程与并行多线程性能对比
- 编写一个函数实现矩阵的转置运算
- 加农算法的MPI实现 【矩阵相乘】
- TensorFLow 基础 矩阵相乘相加 线性回归 手写字体逻辑回归 神经网络 tensorboard 显示 网络结构 和 优化记录
- tensorflow实现对tensor中各个元素求逻辑‘与’
- MapReduce框架中矩阵相乘的算法思路及其实现
- 矩阵相乘strassen-c++代码实现及运行实例结果
- 一个简单的矩阵类的实现(参照《数据结构C++语言描述》第五章)