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

 

(テストコード)
import numpy as np

def main():

    a_1d_2 = np.array([1,2])
    a_2d_1x2 = np.array([[1,2]])
    a_2d_2x1 = np.array([[1],[2]])
    a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
    a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])

    print("①:",np.dot(a_1d_2,a_1d_2))
    print("②:",np.dot(a_1d_2,a_2d_2x1))
    print("③:",np.dot(a_1d_2,a_3d_2x2x1))
    print("④:",np.dot(a_2d_1x2,a_1d_2))
    print("⑤:",np.dot(a_2d_1x2,a_2d_2x1))
    print("⑦:",np.dot(a_3d_2x1x2,a_1d_2))
    print("⑥:",np.dot(a_2d_1x2,a_3d_2x2x1))
    print("⑧:",np.dot(a_3d_2x2x1,a_2d_1x2))
    print("⑨:",np.dot(a_3d_2x2x1,a_3d_2x1x2))

if __name__ == "__main__":
    main()

(実行結果)

①: 5
②: [5]
③: [[5] [5]]
④: [5]
⑤: [[5]]
⑦: [[5] [5]]
⑥: [[[5]  [5]]]
⑧: [
	[[1 2]  [2 4]]
	[[1 2]  [2 4]]
	]
⑨: [
	[
		[[1 2]   [1 2]]
		[[2 4]   [2 4]]
	]
	[
		[[1 2]   [1 2]]
		[[2 4]   [2 4]]
	]
  ]

(図121)

●numpy.matmul

(テストコード)
import numpy as np

def main():

    a_1d_2 = np.array([1,2])
    a_2d_1x2 = np.array([[1,2]])
    a_2d_2x1 = np.array([[1],[2]])
    a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
    a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])

    print("①:",np.matmul(a_1d_2,a_1d_2))
    print("②:",np.matmul(a_1d_2,a_2d_2x1))
    print("③:",np.matmul(a_1d_2,a_3d_2x2x1))
    print("④:",np.matmul(a_2d_1x2,a_1d_2))
    print("⑤:",np.matmul(a_2d_1x2,a_2d_2x1))
    print("⑦:",np.matmul(a_3d_2x1x2,a_1d_2))
    print("⑥:",np.matmul(a_2d_1x2,a_3d_2x2x1))
    print("⑧:",np.matmul(a_3d_2x2x1,a_2d_1x2))
    print("⑨:",np.matmul(a_3d_2x2x1,a_3d_2x1x2))

if __name__ == "__main__":
    main()

(実行結果)

①: 5
②: [5]
③: [[5] [5]]
④: [5]
⑤: [[5]]
⑦: [[5] [5]]
⑥: [[[5]] [[5]]]
⑧: [
	[[1 2]  [2 4]]
	[[1 2]  [2 4]]
	]
⑨: [
	[[1 2]  [2 4]]
	[[1 2]  [2 4]]
	]

(図122)

●tensorflow.matmul

(テストコード)
import tensorflow as tf
import numpy as np

def main():

    a_1d_2 = np.array([1,2])
    a_2d_1x2 = np.array([[1,2]])
    a_2d_2x1 = np.array([[1],[2]])
    a_3d_2x1x2 = np.array([[[1,2]],[[1,2]]])
    a_3d_2x2x1 = np.array([[[1],[2]],[[1],[2]]])

    # print("①:",tf.matmul(a_1d_2,a_1d_2).numpy())
    # print("②:",tf.matmul(a_1d_2,a_2d_2x1).numpy())
    # print("③:",tf.matmul(a_1d_2,a_3d_2x2x1).numpy())
    # print("④:",tf.matmul(a_2d_1x2,a_1d_2).numpy())
    # print("⑦:",tf.matmul(a_3d_2x1x2,a_1d_2).numpy())
    print("⑤:",tf.matmul(a_2d_1x2,a_2d_2x1).numpy())
    print("⑥:",tf.matmul(a_2d_1x2,a_3d_2x2x1).numpy())
    print("⑧:",tf.matmul(a_3d_2x2x1,a_2d_1x2).numpy())
    print("⑨:",tf.matmul(a_3d_2x2x1,a_3d_2x1x2).numpy())

if __name__ == "__main__":
    main()

(実行結果)

①:エラー(In[0] and In[1] ndims must be >= 2)
②:エラー(In[0] and In[1] has different ndims: [2] vs. [2,1])
③:エラー(In[0] ndims must be >= 2)
④:エラー(In[0] and In[1] has different ndims: [1,2] vs. [2])
⑦:エラー(In[1] ndims must be >= 2) 

⑤: [[5]]
⑥: [[[5]] [[5]]]
⑧: [
	[[1 2]  [2 4]]
	[[1 2]  [2 4]]
	]
⑨: [
	[[1 2]  [2 4]]
	[[1 2]  [2 4]]
	]

(図123)

目次にもどる

Adsense審査用広告コード


Adsense審査用広告コード


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

執筆者:


comment

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

関連記事

no image

Azure Cognitive Searchを行うPythonプログラム(ドキュメント検索)

  <目次> Azure Cognitive Searchを行うPythonプログラム(ドキュメント検索)  STEP0:前提条件  STEP1:キーとURLの取得  STEP2:セマンティ …

Pythonのconfigparesrで書き込みや書き換え等の基本的な使い方をご紹介

<目次> (1) Pythonのconfigparesrで書き込みや書き換え等の基本的な使い方をご紹介  (1-1) ConfigParserクラスについて  (1-2) サンプルプログラム  (1- …

PythonのSQLAlchemyで「ArgumentError : Could not parse rfc1738 URL」エラーや「InvalidRequestError: Could not reflect: requested tables(s)」エラーが出た時の対処について

(0)目次&概説 (1) エラー対応1:sqlalchemy.exc.ArgumentError  (1-1) 発生状況・エラーメッセージ   (1-1-1) エラーメッセージ   (1-1-2) エ …

Pythonのvenvの使い方(基礎編)をご紹介

  <目次> (1) Pythonのvenvの使い方(基礎編)をご紹介  (1-1) venvとは?  (1-2) STEP1:仮想環境(venv)の作成  (1-3) STEP2:仮想環境 …

Pythonでcsvを読み込んでデータベース化する方法~pandasのto_sqlを用いたテーブルへのINSERT~

<目次> (1) Pythonでcsvを読み込んでデータベース化する方法  (1-1) 構文(to_sql)  (1-2) 構文(to_sql)の引数  (1-3) サンプルプログラム (1) Pyt …

  • English (United States)
  • 日本語
Top