<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「tf.matmul」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/tf-matmul/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 11 Jan 2026 06:05:44 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「tf.matmul」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【TensorFlow】Tensorの行列積を計算する方法</title>
		<link>https://tech.nkhn37.net/tensorflow-tensor-matrix-multiplication/</link>
					<comments>https://tech.nkhn37.net/tensorflow-tensor-matrix-multiplication/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Sun, 16 Oct 2022 20:00:00 +0000</pubDate>
				<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[tf.matmul]]></category>
		<category><![CDATA[行列積]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=5170</guid>

					<description><![CDATA[Googleによって開発されている機械学習ライブラリであるTensorFlowで、Tenosorの行列積を計算する方法を解説します。 Tensorの行列積 ディープラーニング等の機械学習の実装ではよく行列積を計算します。 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Googleによって開発されている機械学習ライブラリであるTensorFlowで、<span class="marker"><strong>Tenosorの行列積を計算する方法</strong></span>を解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold">Tensorの行列積</h2>



<p class="wp-block-paragraph">ディープラーニング等の機械学習の実装ではよく行列積を計算します。本記事では、Tensorの行列積を計算する方法を紹介します。</p>



<h3 class="wp-block-heading jinr-heading d--bold">tf.matmulによる行列積の計算</h3>



<h4 class="wp-block-heading jinr-heading d--bold">tf.matmulの使用方法</h4>



<p class="wp-block-paragraph">Tensorの行列積を計算するには、以下のように<span class="marker"><strong>tf.matmul</strong></span>を使用します。以降の例は簡単な例として同じTensor同士をかけていますが、もちろん異なるTensor同士でも問題ありません。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import tensorflow as tf

tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float16)
print(tensor, "\n")

# Tensor同士の行列積を計算する tf.matmul
result = tf.matmul(tensor, tensor)
print(result)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]], shape=(3, 3), dtype=float16) 

tf.Tensor(
[[ 30.  36.  42.]
 [ 66.  81.  96.]
 [102. 126. 150.]], shape=(3, 3), dtype=float16)</pre>



<h4 class="wp-block-heading jinr-heading d--bold">行列積ではTensorの形状に注意</h4>



<p class="wp-block-paragraph">行列積では、形状に注意しましょう。例えば、以下のように形状が(3, 2)×(3, 2)のような掛け算をしてしまうとエラーとなります。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import tensorflow as tf

tensor = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float16)
print(tensor, "\n")

# Tensorの形状に注意　以下はエラー
result = tf.matmul(tensor, tensor)
print(result)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [3,2], In[1]: [3,2] [Op:MatMul]</pre>



<p class="wp-block-paragraph">行列積では、形状が(3, <strong>2</strong>)×(<strong>2</strong>, 3)や(2, <strong>3</strong>)×(<strong>3</strong>, 2)のように内側の数字が一致している必要があります。また、計算結果は(<strong>3</strong>, 2)×(2, <strong>3</strong>)→(3, 3)のように外側の数字が形状となるような計算結果となります。</p>



<h4 class="wp-block-heading jinr-heading d--bold">必要に応じて転置して計算</h4>



<p class="wp-block-paragraph">必要に応じて以下のように行列を転置して行列積を計算するようにしましょう。転置では、tf.transposeを使用します。tf.transposeについては「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/tensorflow-tensor-transpose/" target="_blank">Tensorを転置する方法</a>」で紹介していますので興味があれば参考にしてください。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import tensorflow as tf

tensor = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float16)
print(tensor, "\n")

# Tensorの形状に注意　必要に応じて転置をして計算
result = tf.matmul(tensor, tf.transpose(tensor))
print(result, "\n")

result = tf.matmul(tf.transpose(tensor), tensor)
print(result)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
tf.Tensor(
[[1. 2.]
 [3. 4.]
 [5. 6.]], shape=(3, 2), dtype=float16) 

tf.Tensor(
[[ 5. 11. 17.]
 [11. 25. 39.]
 [17. 39. 61.]], shape=(3, 3), dtype=float16) 

tf.Tensor(
[[35. 44.]
 [44. 56.]], shape=(2, 2), dtype=float16)</pre>



<h4 class="wp-block-heading jinr-heading d--bold">＠演算子によるtf.matmulの実行　</h4>



<p class="wp-block-paragraph">Python3.5から<span class="marker"><strong>@演算子</strong></span>(<a rel="noreferrer noopener" href="https://peps.python.org/pep-0465/" target="_blank">PEP 465</a>)がサポートされていますが、TensorFlowのtf.matmulでも以下のように@演算子を用いて計算することができます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import tensorflow as tf

tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float16)
print(tensor, "\n")

# Tensor同士の行列積を計算する @演算子
result = tensor @ tensor
print(result)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]], shape=(3, 3), dtype=float16) 

tf.Tensor(
[[ 30.  36.  42.]
 [ 66.  81.  96.]
 [102. 126. 150.]], shape=(3, 3), dtype=float16)</pre>



<p class="wp-block-paragraph">以上のように@演算子でもtf.matmulを呼び出した場合と同じ結果となることが分かるかと思います。</p>



<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p class="wp-block-paragraph">Googleによって開発されている機械学習ライブラリであるTensorFlowで、<span class="marker"><strong>Tenosorの行列積を計算する方法</strong></span>を解説しました。</p>



<p class="wp-block-paragraph">Tensor同士の行列積を計算する際には<span class="marker"><strong>tf.matmul</strong></span>を使用します。また、Python3.5以降で使えるようになった<span class="marker"><strong>@演算子</strong></span>でも計算することが可能です。</p>



<p class="wp-block-paragraph">ディープラーニングをはじめとした機械学習では行列積は色々なところで出てきますので、使い方をしっかり覚えておくとよいかと思います。</p>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p class="wp-block-paragraph">上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-data-analysis/tensorflow/fundamentals/matrix-multiplication" target="_blank" rel="noreferrer noopener">GitHub</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>

]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/tensorflow-tensor-matrix-multiplication/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-07-04 09:41:42 by W3 Total Cache
-->