Tensor Berinteraksi dengan NumPy
Konversikan antara tensor torch dan array NumPy
Tensor Berinteraksi dengan NumPy adalah pelajaran Deep Learning Academy gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Deep Learning Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Deep Learning Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.ndarrayThey 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.float64Watch 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.float32GPU 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. 🌉
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Tensor Berinteraksi dengan NumPy” gratis?
Ya — teks lengkap “Tensor Berinteraksi dengan NumPy” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Deep Learning Academy, upgrade ke CoddyKit PRO. Kursus Deep Learning Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Tensor Berinteraksi dengan NumPy”?
Konversikan antara tensor torch dan array NumPy Kamu berlatih Deep Learning Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Deep Learning Academy?
Tidak diperlukan pengalaman sebelumnya. Deep Learning Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Tensor Berinteraksi dengan NumPy” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Deep Learning Academy ini?
Ya. Setiap pelajaran Deep Learning Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Bentuk, Dtype, dan Pengindeksan
- Reshape, View, Squeeze, dan Unsqueeze
- Aturan Broadcasting yang Menghemat Perulangan
- Tensor Berinteraksi dengan NumPy