Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Tutorial ini menunjukkan cara melatih model ResNet-50
di perangkat Cloud TPU dengan PyTorch. Anda dapat menerapkan pola yang sama ke model klasifikasi gambar yang dioptimalkan untuk TPU lainnya yang menggunakan PyTorch dan set data ImageNet.
Model dalam tutorial ini didasarkan pada Deep Residual Learning for Image Recognition, yang pertama kali memperkenalkan arsitektur jaringan residual (ResNet). Tutorial ini menggunakan varian 50 lapisan, ResNet-50, dan menunjukkan cara melatih model menggunakan PyTorch/XLA.
Tujuan
Siapkan set data.
Jalankan tugas pelatihan.
Verifikasi hasil output.
Biaya
Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:
Compute Engine
Cloud TPU
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda,
gunakan kalkulator harga.
Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.
Sebelum memulai
Sebelum memulai tutorial ini, periksa apakah project Google Cloud Anda telah disiapkan dengan benar.
Sign in to your Google Cloud account. If you're new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Panduan ini menggunakan komponen Google Cloudyang dapat ditagih. Lihat
halaman harga Cloud TPU untuk
memperkirakan biaya Anda. Pastikan untuk membersihkan resource yang Anda buat setelah Anda
selesai menggunakannya untuk menghindari tagihan yang tidak perlu.
Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam
tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan
hapus setiap resource.
Putuskan koneksi dari VM TPU:
(vm)$exit
Sekarang perintah Anda akan menjadi username@projectname, yang menunjukkan Anda
berada dalam Cloud Shell.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-18 UTC."],[],[],null,["# Training Resnet50 on Cloud TPU with PyTorch\n\n*** ** * ** ***\n\nThis tutorial shows you how to train the ResNet-50 model\non a Cloud TPU device with PyTorch. You can apply the same pattern to\nother TPU-optimised image classification models that use PyTorch and the\nImageNet dataset.\n\nThe model in this tutorial is based on [Deep Residual Learning for Image\nRecognition](https://arxiv.org/pdf/1512.03385.pdf), which first introduces\nthe residual network (ResNet) architecture. The tutorial uses the 50-layer\nvariant, ResNet-50, and demonstrates training the model using\n[PyTorch/XLA](https://github.com/pytorch/xla).\n| **Warning:** This tutorial uses a third-party dataset. Google provides no representation, warranty, or other guarantees about the validity, or any other aspects of this dataset.\n\n\nObjectives\n----------\n\n- Prepare the dataset.\n- Run the training job.\n- Verify the output results.\n\n\nCosts\n-----\n\n\nIn this document, you use the following billable components of Google Cloud:\n\n\n- Compute Engine\n- Cloud TPU\n\n\nTo generate a cost estimate based on your projected usage,\nuse the [pricing calculator](/products/calculator). \nNew Google Cloud users might be eligible for a [free trial](/free). \n\n\u003cbr /\u003e\n\n\nBefore you begin\n----------------\n\nBefore starting this tutorial, check that your Google Cloud project is correctly\nset up.\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n1. This walkthrough uses billable components of Google Cloud. Check the [Cloud TPU pricing page](/tpu/docs/pricing) to estimate your costs. Be sure to clean up resources you created when you've finished with them to avoid unnecessary charges.\n\n\nCreate a TPU VM\n---------------\n\n1. Open a Cloud Shell window.\n\n [Open Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n2. Create a TPU VM\n\n ```bash\n gcloud compute tpus tpu-vm create your-tpu-name \\\n --accelerator-type=v3-8 \\\n --version=tpu-ubuntu2204-base \\\n --zone=us-central1-a \\\n --project=your-project\n ```\n | **Note:** The first time you run a command in a new Cloud Shell VM, an `Authorize Cloud Shell` page is displayed. Click `Authorize` at the bottom of the page to allow `gcloud` to make Google Cloud API calls with your credentials.\n3. Connect to your TPU VM using SSH:\n\n ```bash\n gcloud compute tpus tpu-vm ssh your-tpu-name --zone=us-central1-a\n ```\n4. Install PyTorch/XLA on your TPU VM:\n\n ```bash\n (vm)$ pip install torch torch_xla[tpu] torchvision -f https://storage.googleapis.com/libtpu-releases/index.html -f https://storage.googleapis.com/libtpu-wheels/index.html\n ```\n5. Clone the [PyTorch/XLA GitHub repo](https://github.com/pytorch/xla)\n\n ```bash\n (vm)$ git clone --depth=1 https://github.com/pytorch/xla.git\n ```\n6. Run the training script with fake data\n\n ```bash\n (vm) $ PJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py --fake_data --batch_size=256 --num_epochs=1\n ```\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for the resources used in this\ntutorial, either delete the project that contains the resources, or keep the project and\ndelete the individual resources.\n\n1. Disconnect from the TPU VM:\n\n ```bash\n (vm) $ exit\n ```\n\n Your prompt should now be `username@projectname`, showing you\n are in the Cloud Shell.\n2. Delete your TPU VM.\n\n ```bash\n $ gcloud compute tpus tpu-vm delete your-tpu-name \\\n --zone=us-central1-a\n ```\n\n\nWhat's next\n-----------\n\n- [Training diffusion models with Pytorch](/tpu/docs/tutorials/diffusion-pytorch)\n- [Troubleshooting Pytorch on TPUs](/tpu/docs/troubleshooting/trouble-pytorch)\n- [Pytorch/XLA documentation](https://pytorch.org/xla/)"]]