Loading [MathJax]/extensions/tex2jax.js

Rainbow Engine

IT技術を分かりやすく簡潔にまとめることによる学習の効率化、また日常の気付きを記録に残すことを目指します。

Python Tensorflow 機械学習 (Machine Learning)

matmulとdotの違いについて(Pythonのnumpy・tensorflow)

投稿日:2022年10月12日 更新日:

 

<目次>

matmulとdotの違いについて(Pythonのnumpy・tensorflow)
 (1-1) 両者の違い
 (1-2) 両者の違い(実機確認)

matmulとdotの違いについて(Pythonのnumpy・tensorflow)

ベクトルや行列の計算で使用する関数である「matmul」と「dot」の違いについて紹介します。はじめに、matmulについてはnumpyライブラリのものと、tensorflowライブラリのものと2種類あります。

(概要)
⇒主に内積で使用。
⇒主に行列計算で使用。3次元以の行列計算にも対応。
⇒主に行列計算で使用。3次元以の行列計算にも対応。1次元を含むベクトルの内積はエラー

(1-1) 両者の違い

●numpy.matmul vs numpy.dot差異
(表)
次元
(行列1\行列2)
n=1 n=2 n>2
n=1
(例:[1])

差異なし

差異なし

差異なし
n=2
(例:[[1],[1]])

差異なし

差異なし

▲差異あり
⇒詳細は「numpyの記事(★)」と「dotの記事(★)」を参照
n>2
(例:[[[1],[1]],[[1],[1]]])

差異なし

▲差異あり
⇒詳細は「numpyの記事(★)」と「dotの記事(★)」を参照

▲差異あり
⇒詳細は「numpyの記事(★)」と「dotの記事(★)」を参照
●numpy.matmul vs tensorflow.matmul差異
(表)
次元
(行列1\行列2)
n=1 n=2 n>2
n=1
(例:[1])

▲差異あり
⇒tensorflowはエラー。numpyはベクトル内積計算。

▲差異あり
⇒tensorflowはエラー。numpyはベクトル内積計算。

▲差異あり
⇒tensorflowはエラー。numpyはベクトル内積計算。
n=2
(例:[[1],[1]])

▲差異あり
⇒tensorflowはエラー。numpyはベクトル内積計算。

差異なし

差異なし
n>2
(例:[[[1],[1]],[[1],[1]]])

▲差異あり
⇒tensorflowはエラー。numpyはベクトル内積計算。

差異なし

差異なし

目次にもどる

(1-2) 両者の違い(実機確認)

●numpy.dot

 

(テストコード)
  1. import numpy as np
  2.  
  3. def main():
  4.  
  5. a_1d_2 = np.array([1,2])
  6. a_2d_1x2 = np.array([[1,2]])
  7. a_2d_2x1 = np.array([[1],[2]])
  8. a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
  9. a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])
  10.  
  11. print("①:",np.dot(a_1d_2,a_1d_2))
  12. print("②:",np.dot(a_1d_2,a_2d_2x1))
  13. print("③:",np.dot(a_1d_2,a_3d_2x2x1))
  14. print("④:",np.dot(a_2d_1x2,a_1d_2))
  15. print("⑤:",np.dot(a_2d_1x2,a_2d_2x1))
  16. print("⑦:",np.dot(a_3d_2x1x2,a_1d_2))
  17. print("⑥:",np.dot(a_2d_1x2,a_3d_2x2x1))
  18. print("⑧:",np.dot(a_3d_2x2x1,a_2d_1x2))
  19. print("⑨:",np.dot(a_3d_2x2x1,a_3d_2x1x2))
  20.  
  21. if __name__ == "__main__":
  22. main()

(実行結果)

  1. ①: 5
  2. ②: [5]
  3. ③: [[5] [5]]
  4. ④: [5]
  5. ⑤: [[5]]
  6. ⑦: [[5] [5]]
  7. ⑥: [[[5] [5]]]
  8. ⑧: [
  9. [[1 2] [2 4]]
  10. [[1 2] [2 4]]
  11. ]
  12. ⑨: [
  13. [
  14. [[1 2] [1 2]]
  15. [[2 4] [2 4]]
  16. ]
  17. [
  18. [[1 2] [1 2]]
  19. [[2 4] [2 4]]
  20. ]
  21. ]

(図121)

●numpy.matmul

(テストコード)
  1. import numpy as np
  2.  
  3. def main():
  4.  
  5. a_1d_2 = np.array([1,2])
  6. a_2d_1x2 = np.array([[1,2]])
  7. a_2d_2x1 = np.array([[1],[2]])
  8. a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
  9. a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])
  10.  
  11. print("①:",np.matmul(a_1d_2,a_1d_2))
  12. print("②:",np.matmul(a_1d_2,a_2d_2x1))
  13. print("③:",np.matmul(a_1d_2,a_3d_2x2x1))
  14. print("④:",np.matmul(a_2d_1x2,a_1d_2))
  15. print("⑤:",np.matmul(a_2d_1x2,a_2d_2x1))
  16. print("⑦:",np.matmul(a_3d_2x1x2,a_1d_2))
  17. print("⑥:",np.matmul(a_2d_1x2,a_3d_2x2x1))
  18. print("⑧:",np.matmul(a_3d_2x2x1,a_2d_1x2))
  19. print("⑨:",np.matmul(a_3d_2x2x1,a_3d_2x1x2))
  20.  
  21. if __name__ == "__main__":
  22. main()

(実行結果)

  1. ①: 5
  2. ②: [5]
  3. ③: [[5] [5]]
  4. ④: [5]
  5. ⑤: [[5]]
  6. ⑦: [[5] [5]]
  7. ⑥: [[[5]] [[5]]]
  8. ⑧: [
  9. [[1 2] [2 4]]
  10. [[1 2] [2 4]]
  11. ]
  12. ⑨: [
  13. [[1 2] [2 4]]
  14. [[1 2] [2 4]]
  15. ]

(図122)

●tensorflow.matmul

(テストコード)
  1. import tensorflow as tf
  2. import numpy as np
  3.  
  4. def main():
  5.  
  6. a_1d_2 = np.array([1,2])
  7. a_2d_1x2 = np.array([[1,2]])
  8. a_2d_2x1 = np.array([[1],[2]])
  9. a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
  10. a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])
  11.  
  12. # print("①:",tf.matmul(a_1d_2,a_1d_2).numpy())
  13. # print("②:",tf.matmul(a_1d_2,a_2d_2x1).numpy())
  14. # print("③:",tf.matmul(a_1d_2,a_3d_2x2x1).numpy())
  15. # print("④:",tf.matmul(a_2d_1x2,a_1d_2).numpy())
  16. # print("⑦:",tf.matmul(a_3d_2x1x2,a_1d_2).numpy())
  17. print("⑤:",tf.matmul(a_2d_1x2,a_2d_2x1).numpy())
  18. print("⑥:",tf.matmul(a_2d_1x2,a_3d_2x2x1).numpy())
  19. print("⑧:",tf.matmul(a_3d_2x2x1,a_2d_1x2).numpy())
  20. print("⑨:",tf.matmul(a_3d_2x2x1,a_3d_2x1x2).numpy())
  21.  
  22. if __name__ == "__main__":
  23. main()

(実行結果)

  1. ①:エラー(In[0] and In[1] ndims must be >= 2
  2. ②:エラー(In[0] and In[1] has different ndims: [2] vs. [2,1])
  3. ③:エラー(In[0] ndims must be >= 2
  4. ④:エラー(In[0] and In[1] has different ndims: [1,2] vs. [2])
  5. ⑦:エラー(In[1] ndims must be >= 2
  6.  
  7. ⑤: [[5]]
  8. ⑥: [[[5]] [[5]]]
  9. ⑧: [
  10. [[1 2] [2 4]]
  11. [[1 2] [2 4]]
  12. ]
  13. ⑨: [
  14. [[1 2] [2 4]]
  15. [[1 2] [2 4]]
  16. ]

(図123)

目次にもどる

Adsense審査用広告コード


Adsense審査用広告コード


-Python, Tensorflow, 機械学習 (Machine Learning)
-

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

no image

ディープラーニングのモデルの評価とは?実際のPythonプログラムもご紹介

<目次> ディープラーニングのモデルの評価とは?実際のPythonプログラムもご紹介  (1-1) モデルの評価の概要  (1-2) モデルの評価の指標  (1-3) モデルの評価の実装例①  (1- …

Pythonのdatapackage学習中に遭遇したエラー「StopIteration」と「AttributeError」の対応

(0)目次&概説 (1) 記事の目的 (2) エラー1:AttributeError: ‘generator’ object has no attribute ‘n …

ロジスティック回帰の式変形(多クラス版)のご紹介(ディープラーニング)

  <目次> ロジスティック回帰の式変形(多クラス版)のご紹介(ディープラーニング)  【前提】多クラスロジスティック回帰とは?  【本編】多クラスロジスティック回帰の流れを整理   STE …

PythonのdatapackageとSQLAlchemy、SQLiteを使ってcsvデータをSELECTする

(0)目次&概説 (1) 今回の目的  (1-1) 目的  (1-2) 前提条件 (2) 実施手順  (2-0) 事前作業  (2-1) データ(csv)のロード  (2-2) エンジンの作成  (2 …

Pythonの「Import “requests” could not be resolved from source Pylance」エラーの原因と対処について

  <目次> (1) Pythonの「Import “requests” could not be resolved from source Pylance」エラーの …

  • English (United States)
  • 日本語
S