- Google Colab 教程
- Google Colab - 首頁
- Google Colab - 簡介
- 什麼是 Google Colab?
- 你的第一個 Colab Notebook
- 程式碼文件
- Google Colab - 儲存你的工作
- Google Colab - 分享 Notebook
- 呼叫系統命令
- 執行外部 Python 檔案
- Google Colab - 圖形輸出
- Google Colab - 程式碼編輯幫助
- Google Colab - 魔法命令
- Google Colab - 新增表單
- Google Colab - 安裝 ML 庫
- Google Colab - 使用免費 GPU
- Google Colab - 結論
- Google Colab 有用資源
- Google Colab - 快速指南
- Google Colab - 有用資源
- Google Colab - 討論
Google Colab - 呼叫系統命令
Jupyter 包含許多常用系統操作的快捷方式。Colab 程式碼單元支援此功能。
簡單命令
在使用系統命令 echo 的程式碼單元格中輸入以下程式碼。
message = 'A Great Tutorial on Colab by Tutorialspoint!' greeting = !echo -e '$message\n$message' greeting
現在,如果您執行單元格,您將看到以下輸出:
['A Great Tutorial on Colab by Tutorialspoint!', 'A Great Tutorial on Colab by Tutorialspoint!']
獲取遠端資料
讓我們來看另一個從遠端伺服器載入資料集的示例。在您的程式碼單元格中輸入以下命令:
!wget http://mlr.cs.umass.edu/ml/machine-learning-databases/adult/adult.data -P "/content/drive/My Drive/app"
如果您執行程式碼,您將看到以下輸出:
--2019-06-20 10:09:53-- http://mlr.cs.umass.edu/ml/machine-learning-databases/adult/adult.data Resolving mlr.cs.umass.edu (mlr.cs.umass.edu)... 128.119.246.96 Connecting to mlr.cs.umass.edu (mlr.cs.umass.edu)|128.119.246.96|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 3974305 (3.8M) [text/plain] Saving to: ‘/content/drive/My Drive/app/adult.data.1’ adult.data.1 100%[===================>] 3.79M 1.74MB/s in 2.2s 2019-06-20 10:09:56 (1.74 MB/s) - ‘/content/drive/My Drive/app/adult.data.1’ saved [3974305/3974305]
如訊息所示,adult.data.1 檔案現已新增到您的驅動器中。您可以透過檢查驅動器的資料夾內容來驗證這一點。或者,在新的程式碼單元格中輸入以下程式碼:
import pandas as pd
data = pd.read_csv("/content/drive/My Drive/app/adult.data.1")
data.head(5)
現在執行程式碼,您將看到以下輸出:
同樣,大多數系統命令可以透過在命令前加上感嘆號 (!) 在您的程式碼單元格中呼叫。在給出您可以呼叫的完整命令列表之前,讓我們再看一個示例。
克隆 Git 倉庫
您可以使用git命令將整個 GitHub 倉庫克隆到 Colab。例如,要克隆 keras 教程,請在程式碼單元格中輸入以下命令:
!git clone https://github.com/wxs/keras-mnist-tutorial.git
命令成功執行後,您將看到以下輸出:
Cloning into 'keras-mnist-tutorial'... remote: Enumerating objects: 26, done. remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 26 Unpacking objects: 100% (26/26), done.
克隆倉庫後,在其中找到一個 Jupyter 專案(例如 keras.ipyab 中的 MINST),右鍵單擊檔名,然後選擇使用 / Colaboratory 開啟選單選項以在 Colab 中開啟該專案。
系統別名
要獲取常用操作的快捷方式列表,請執行以下命令:
!ls /bin
您將在輸出視窗中看到如下所示的列表:
bash* journalctl* sync* bunzip2* kill* systemctl* bzcat* kmod* systemd@ bzcmp@ less* systemd-ask-password* bzdiff* lessecho* systemd-escape* bzegrep@ lessfile@ systemd-hwdb* bzexe* lesskey* systemd-inhibit* bzfgrep@ lesspipe* systemd-machine-id-setup* bzgrep* ln* systemd-notify* bzip2* login* systemd-sysusers* bzip2recover* loginctl* systemd-tmpfiles* bzless@ ls* systemd-tty-ask-password-agent* bzmore* lsblk* tar* cat* lsmod@ tempfile* chgrp* mkdir* touch* chmod* mknod* true* chown* mktemp* udevadm* cp* more* ulockmgr_server* dash* mount* umount* date* mountpoint* uname* dd* mv* uncompress* df* networkctl* vdir* dir* nisdomainname@ wdctl* dmesg* pidof@ which* dnsdomainname@ ps* ypdomainname@ domainname@ pwd* zcat* echo* rbash@ zcmp* egrep* readlink* zdiff* false* rm* zegrep* fgrep* rmdir* zfgrep* findmnt* run-parts* zforce* fusermount* sed* zgrep* grep* sh@ zless* gunzip* sh.distrib@ zmore* gzexe* sleep* znew* gzip* stty* hostname* su*
執行任何這些命令,就像我們對echo和wget所做的那樣。在下一章中,我們將瞭解如何執行您之前建立的 Python 程式碼。
廣告