浅谈keras中的batch_dot,dot方法和TensorFlow的matmul

作者:huml126 时间:2023-04-08 07:21:28 

概述

在使用keras中的keras.backend.batch_dot和tf.matmul实现功能其实是一样的智能矩阵乘法,比如A,B,C,D,E,F,G,H,I,J,K,L都是二维矩阵,中间点表示矩阵乘法,AG 表示矩阵A 和G 矩阵乘法(A 的列维度等于G 行维度),WX=Z


import keras.backend as K
import tensorflow as tf
import numpy as np

w = K.variable(np.random.randint(10,size=(10,12,4,5)))
k = K.variable(np.random.randint(10,size=(10,12,5,8)))
z = K.batch_dot(w,k)
print(z.shape) #(10, 12, 4, 8)


import keras.backend as K
import tensorflow as tf
import numpy as np

w = tf.Variable(np.random.randint(10,size=(10,12,4,5)),dtype=tf.float32)
k = tf.Variable(np.random.randint(10,size=(10,12,5,8)),dtype=tf.float32)
z = tf.matmul(w,k)
print(z.shape) #(10, 12, 4, 8)

浅谈keras中的batch_dot,dot方法和TensorFlow的matmul

示例


from keras import backend as K
a = K.ones((3,4,5,2))
b = K.ones((2,5,3,7))
c = K.dot(a, b)
print(c.shape)

会输出:

ValueError: Dimensions must be equal, but are 2 and 3 for ‘MatMul' (op: ‘MatMul') with input shapes: [60,2], [3,70].


from keras import backend as K
a = K.ones((3,4))
b = K.ones((4,5))
c = K.dot(a, b)
print(c.shape)#(3,5)

或者


import tensorflow as tf
a = tf.ones((3,4))
b = tf.ones((4,5))
c = tf.matmul(a, b)
print(c.shape)#(3,5)

如果增加维度:


from keras import backend as K
a = K.ones((2,3,4))
b = K.ones((7,4,5))
c = K.dot(a, b)
print(c.shape)#(2, 3, 7, 5)

这个矩阵乘法会沿着两个矩阵最后两个维度进行乘法,不是element-wise矩阵乘法


from keras import backend as K
a = K.ones((1, 2, 3 , 4))
b = K.ones((8, 7, 4, 5))
c = K.dot(a, b)
print(c.shape)#(1, 2, 3, 8, 7, 5)

浅谈keras中的batch_dot,dot方法和TensorFlow的matmul

keras的dot方法是Theano中的复制


from keras import backend as K
a = K.ones((1, 2, 4))
b = K.ones((8, 7, 4, 5))
c = K.dot(a, b)
print(c.shape)# (1, 2, 8, 7, 5).

from keras import backend as K
a = K.ones((9, 8, 7, 4, 2))
b = K.ones((9, 8, 7, 2, 5))
c = K.batch_dot(a, b)
print(c.shape) #(9, 8, 7, 4, 5)

或者


import tensorflow as tf
a = tf.ones((9, 8, 7, 4, 2))
b = tf.ones((9, 8, 7, 2, 5))
c = tf.matmul(a, b)
print(c.shape) #(9, 8, 7, 4, 5)

来源:https://blog.csdn.net/huml126/article/details/88739846

标签:keras,batch,dot,dot,TensorFlow,matmul
0
投稿

猜你喜欢

  • GOLANG版的冒泡排序和快速排序分享

    2023-07-05 05:31:09
  • CSS 的优先规则

    2009-01-08 12:40:00
  • PHP _construct()函数讲解

    2023-06-14 16:56:43
  • 利用global.asa计划执行程序

    2008-03-05 12:49:00
  • Python深度学习albumentations数据增强库

    2023-02-14 20:00:18
  • jfinal与bootstrap的登录跳转实战演习

    2023-07-02 05:20:01
  • python利用Excel读取和存储测试数据完成接口自动化教程

    2022-04-02 05:25:13
  • 详解 PyTorch Lightning模型部署到生产服务中

    2021-10-23 01:17:25
  • 如何使用python写截屏小工具

    2021-04-04 23:51:14
  • js控制输入框获得和失去焦点时状态显示的方法

    2024-04-17 10:24:04
  • 字符编码详解及由来(UNICODE,UTF-8,GBK) 比较详细

    2022-05-21 14:49:29
  • 一些SQL查询语法参考

    2007-10-14 11:56:00
  • 文件上传服务器-jupyter 中python解压及压缩方式

    2021-06-03 22:32:21
  • PHP根据IP判断地区名信息的示例代码

    2023-09-10 14:05:55
  • 用VB编写ActiveX DLL实现ASP编程

    2008-10-21 21:28:00
  • django 2.0更新的10条注意事项总结

    2023-05-22 10:48:35
  • Python 读取有公式cell的结果内容实例方法

    2021-09-06 21:07:11
  • python beautifulsoup4 模块详情

    2021-12-30 07:50:03
  • python格式化字符串实例总结

    2023-09-01 04:36:11
  • Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法

    2021-02-14 00:18:22
  • asp之家 网络编程 m.aspxhome.com