0Pricing
Deep Learning Academy · レッスン

テンソルとNumPyの連携

torchテンソルとNumPy配列を相互に変換します

「テンソルとNumPyの連携」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Two Worlds, One Bridge

Lots of data starts life as a NumPy array. PyTorch makes it easy to move between arrays and tensors in both directions.

NumPy Arrays Are Like Tensors

A NumPy array also stores numbers in a shape, just without GPU support or autograd. Tensors add those superpowers.

import numpy as np
a = np.array([1, 2, 3])
print(a.shape)  # (3,)

From NumPy with from_numpy

Turn an array into a tensor with torch.from_numpy. It is the standard entry point for existing NumPy data.

a = np.array([1, 2, 3])
t = torch.from_numpy(a)
print(t)  # tensor([1, 2, 3])

Back to NumPy with .numpy

Going the other way, call .numpy on a tensor. Great for plotting or handing data to other libraries.

t = torch.tensor([1, 2, 3])
a = t.numpy()
print(type(a))  # numpy.ndarray

They Share the Same Memory

By default the array and tensor share one block of memory. Changing one quietly changes the other.

a = np.array([1, 2, 3])
t = torch.from_numpy(a)
a[0] = 99
print(t[0])  # tensor(99)

Copy to Break the Link

Want independent data? Call .clone on the tensor so edits stay separate from the original array.

a = np.array([1, 2, 3])
t = torch.from_numpy(a).clone()
a[0] = 99
print(t[0])  # tensor(1)

Dtypes Carry Across

The dtype follows the data over the bridge. A float64 array becomes a float64 tensor unless you cast it.

a = np.array([1.0, 2.0])
t = torch.from_numpy(a)
print(t.dtype)  # torch.float64

Watch the Float64 Trap

NumPy defaults to float64, but models want float32. Cast after conversion to avoid mismatched dtype errors.

a = np.array([1.0, 2.0])
t = torch.from_numpy(a).float()
print(t.dtype)  # torch.float32

GPU Tensors Need a Trip Home

You can't call .numpy on a GPU tensor directly. Move it back with .cpu() first, then convert.

t = torch.tensor([1, 2, 3])
a = t.cpu().numpy()
print(a)  # [1 2 3]

Detach Before Converting Grads

If a tensor tracks gradients, call .detach before .numpy so you grab the values without the autograd graph.

t = torch.tensor([1.0], requires_grad=True)
a = t.detach().numpy()
print(a)  # [1.]

A Clean Conversion Recipe

The safe pattern for any tensor is detach then cpu then numpy. It works whether or not grads or GPUs are involved.

t = torch.tensor([1.0, 2.0])
a = t.detach().cpu().numpy()
print(a)  # [1. 2.]

Quick Check

One question about the array and tensor connection.

Recap: Tensors Talk to NumPy

You can bridge both ways with from_numpy and .numpy, watch the shared-memory trap, and use detach-cpu-numpy for a safe export. 🌉

よくある質問

「テンソルとNumPyの連携」レッスンは無料ですか?

はい。「テンソルとNumPyの連携」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「テンソルとNumPyの連携」で何を学びますか?

torchテンソルとNumPy配列を相互に変換します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「テンソルとNumPyの連携」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. shape、dtype、インデックス指定
  2. reshape、view、squeeze、unsqueeze
  3. ループを減らすブロードキャストのルール
  4. テンソルとNumPyの連携
← Deep Learning Academyに戻る