<?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.expand_dims」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/tf-expand_dims/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 11 Jan 2026 06:10:12 +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.expand_dims」タグの記事一覧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-expand-dims/</link>
					<comments>https://tech.nkhn37.net/tensorflow-tensor-expand-dims/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Fri, 14 Oct 2022 21:00:00 +0000</pubDate>
				<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[tf.expand_dims]]></category>
		<category><![CDATA[tf.newaxis]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=5148</guid>

					<description><![CDATA[Googleによって開発されている機械学習ライブラリであるTensorFlowで、Tensorの軸を拡張する方法を解説します。 Tensorの軸を拡張する方法 ディープラーニングに関するプログラミングをしている際の形状変 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Googleによって開発されている機械学習ライブラリであるTensorFlowで、<span class="marker"><strong>Tensorの軸を拡張する方法</strong></span>を解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold">Tensorの軸を拡張する方法</h2>



<p class="wp-block-paragraph">ディープラーニングに関するプログラミングをしている際の形状変更の一種として、軸を拡張するというケースがあります。本記事では、Tensorの軸を拡張する方法を紹介します。</p>



<p class="wp-block-paragraph">なお、Tensorの形状を変更する方法については「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/tensorflow-tensor-reshape/" target="_blank">Tensorの形状を変更する方法</a>」で説明しているのであわせて見ていただけるとよいかと思います。</p>



<h3 class="wp-block-heading jinr-heading d--bold">tf.newaxisを使用して軸を追加する</h3>



<p class="wp-block-paragraph">Tensorの軸を拡張する一つの方法としては、<span class="marker"><strong>tf.newaxis</strong></span>を使用する方法があります。以降でいくつかの例をみてみましょう。</p>



<h4 class="wp-block-heading jinr-heading d--bold">例1：行ベクトルや列ベクトルにする</h4>



<p class="wp-block-paragraph">tf.newaxisを使って行ベクトル(1, n)や列ベクトル(n, 1)を作るような場合には、以下のように使用します。</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], dtype=tf.float16)
print(tensor, "\n")

# ===== Tensorの軸を拡張する tf.newaxis
# 先頭に軸を追加する
expanded_tensor_1 = tensor[tf.newaxis, :]
print(expanded_tensor_1, "\n")

# 末尾に軸を追加する
expanded_tensor_2 = tensor[:, tf.newaxis]
print(expanded_tensor_2)</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.], shape=(5,), dtype=float16) 

tf.Tensor([[1. 2. 3. 4. 5.]], shape=(1, 5), dtype=float16) 

tf.Tensor(
[[1.]
 [2.]
 [3.]
 [4.]
 [5.]], shape=(5, 1), dtype=float16)</pre>



<p class="wp-block-paragraph">tf.newaxisを使用した方法では、要素参照の[]で軸を追加したい部分にtf.newaxisを指定します。例えば、先頭に軸を追加したい場合にはtensor[tf.newaxis, :]といった形です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">例2：2階以上のTensorに軸を追加</h4>



<p class="wp-block-paragraph">tf.newaxisの使い方は、2階以上の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_2d = tf.constant([[1, 2], [3, 4]], dtype=tf.float16)
print(tensor_2d, "\n")

# ===== Tensorの軸を拡張する tf.newaxis
# 先頭に軸を追加する
expanded_tensor_1 = tensor_2d[tf.newaxis, :, :]
print(expanded_tensor_1, "\n")

# 真ん中に軸を追加する
expanded_tensor_2 = tensor_2d[:, tf.newaxis, :]
print(expanded_tensor_2, "\n")

# 末尾に軸を追加する
expanded_tensor_3 = tensor_2d[:, :, tf.newaxis]
print(expanded_tensor_3)</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.]], shape=(2, 2), dtype=float16) 

tf.Tensor(
[[[1. 2.]
  [3. 4.]]], shape=(1, 2, 2), dtype=float16) 

tf.Tensor(
[[[1. 2.]]

 [[3. 4.]]], shape=(2, 1, 2), dtype=float16) 

tf.Tensor(
[[[1.]
  [2.]]

 [[3.]
  [4.]]], shape=(2, 2, 1), dtype=float16)</pre>



<p class="wp-block-paragraph">上記結果のshapeを見るとそれぞれで先頭、真ん中、末尾に軸が追加できていることが分かるかと思います。</p>



<h3 class="wp-block-heading jinr-heading d--bold">tf.expand_dimsを使用して軸を拡張する</h3>



<p class="wp-block-paragraph">Tensorの軸を拡張するもう一つの方法は、<span class="marker"><strong>tf.expand_dims</strong></span>を使用する方法です。以降で、tf.newaxisの場合と同じ例で見てみましょう。</p>



<h4 class="wp-block-heading jinr-heading d--bold">例1：行ベクトルや列ベクトルにする</h4>



<p class="wp-block-paragraph">tf.expand_dimsを使って行ベクトル(1, n)や列ベクトル(n, 1)を作るような場合には、以下のように使用します。</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], dtype=tf.float16)
print(tensor, "\n")

# ===== Tensorの軸を拡張する tf.expand_dims
# 先頭に軸を追加する
expanded_tensor_1 = tf.expand_dims(tensor, axis=0)
print(expanded_tensor_1, "\n")

# 末尾に軸を追加する
expanded_tensor_2 = tf.expand_dims(tensor, axis=-1)
print(expanded_tensor_2)</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.], shape=(5,), dtype=float16) 

tf.Tensor([[1. 2. 3. 4. 5.]], shape=(1, 5), dtype=float16) 

tf.Tensor(
[[1.]
 [2.]
 [3.]
 [4.]
 [5.]], shape=(5, 1), dtype=float16)</pre>



<p class="wp-block-paragraph">tf.expand_dimsを使用する場合は、拡張する元のTensorと拡張したい軸をaxis引数で指定します。例えば、先頭に追加したい場合には「axis=0」とします。</p>



<p class="wp-block-paragraph">上記の例では、末尾に追加する例では「axis=-1」としています。もちろん上記の例で「axis=1」としても同様の結果が得られますが、末尾に追加する場合には「axis=-1」で指定する場合も多いですので覚えておきましょう。</p>



<h4 class="wp-block-heading jinr-heading d--bold">例2：2階以上のTensorに軸を追加</h4>



<p class="wp-block-paragraph">tf.expand_dimsの使い方は、2階以上の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_2d = tf.constant([[1, 2], [3, 4]], dtype=tf.float16)
print(tensor_2d, "\n")

# ===== Tensorの軸を拡張する tf.expand_dims
# 先頭に軸を追加する
expanded_tensor_1 = tf.expand_dims(tensor_2d, axis=0)
print(expanded_tensor_1, "\n")

# 真ん中に軸を追加する
expanded_tensor_2 = tf.expand_dims(tensor_2d, axis=1)
print(expanded_tensor_2, "\n")

# 末尾に軸を追加する
expanded_tensor_3 = tf.expand_dims(tensor_2d, axis=-1)
print(expanded_tensor_3)</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.]], shape=(2, 2), dtype=float16) 

tf.Tensor(
[[[1. 2.]
  [3. 4.]]], shape=(1, 2, 2), dtype=float16) 

tf.Tensor(
[[[1. 2.]]

 [[3. 4.]]], shape=(2, 1, 2), dtype=float16) 

tf.Tensor(
[[[1.]
  [2.]]

 [[3.]
  [4.]]], shape=(2, 2, 1), dtype=float16)</pre>



<p class="wp-block-paragraph">上記結果のshapeを見るとそれぞれで先頭、真ん中、末尾に軸が追加できていることが分かるかと思います。</p>



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



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



<p class="wp-block-paragraph">軸を拡張する方法としては、<span class="marker"><strong>tf.newaxis</strong></span>を使用する方法と<span class="marker"><strong>tf.expand_dims</strong></span>を使用する方法があります。データ操作で形状を揃えるとき等に軸追加をして拡張することがよくありますので、方法をしっかり覚えておきましょう。</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/expand_dims" 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-expand-dims/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:32:37 by W3 Total Cache
-->