用 Python 使用 Google Colab?豈止是炫酷
閱讀本文大概需要 3 分鐘。
選自Medium
作者:Towards AI Team
機器之心編譯
機器之心編輯部
一篇教你如何使用 Google Colab,更好利用免費資源的文章。
提供了免費的 Jupyter notebook 環(huán)境;
帶有預(yù)安裝的軟件包;
完全托管在谷歌云上;
用戶無需在服務(wù)器或工作站上進行設(shè)置;
Notebook 會自動保存在用戶的 Google Drive 中;
提供基于瀏覽器的 Jupyter notebook;
完全免費,且提供 GPU 和 TPU(Pro 用戶可以使用更多資源,但需要付費);
支持 Python 2 和 Python 3;
提供兩種硬件加速器:GPU 和 TPU。

啟動 Google Colab
從彈框中選擇 GitHub 這一項。

輸入 GitHub 項目 URL 并搜索以獲取代碼

將完整代碼一鍵上傳到 Google Colab notebook


打開 Kaggle
轉(zhuǎn)至「我的賬戶」
向下滾動到「API」部分

如果需要,先單擊「Expire API Token」以刪除先前的 token
點擊「Create New API Token」,生成一個新的 token 并下載一個名為「kaggle.json」的 JSON 文件
「kaggle.json」文件包含用戶名和密鑰,如下所示:

!pip install -q kagglefrom google.colab import filesfiles.upload()
!kaggle datasets list
!kaggle competitions download -c competitive-data-science-predict-future-sales

import globimport pandas as pdfrom google.colab import drivedrive.mount('/gdrive')
授權(quán)碼輸入框
單擊鏈接并生成授權(quán)碼
從 Google Drive 讀取 CSV 文件
file_path = glob.glob("/gdrive/My Drive/***.csv")for file in file_path:df = pd.read_csv(file)print(df)


選擇 Runtime → Change runtime type
在彈出窗口中選擇「GPU」

import tensorflow as tffrom tensorflow.python.client import device_libtf.test.gpu_device_name()
device_lib.list_local_devices()

import tensorflow as tfno_of_gpu = len(tf.config.experimental.list_physical_devices('GPU'))print("Total GPUS:", no_of_gpu)

import tensorflow as tfno_of_gpu =len(tf.config.experimental.list_physical_devices('GPU'))print("Total GPUS:", no_of_gpu)
try:with tf.device('/device:GPU:1'):tensor1 = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])tensor2 = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])result = tf.matmul(tensor1, tensor2)print(result)except?RuntimeError?as?exception:????print(exception)

from?google.colab?import?drivedrive.mount('/content/gdrive')

cd gdrive/My Drive/mkdir project%cd project/
!git clone https://github.com/saniyaparveez/youtube_video_type_prediction.git
!ls
lsmagic
ldir
historytime
!uptime
!free -hprint("-"*100)
!lscpuprint("-"*70)
%shecho "List all running VM processes."ps -efecho "Done"
%%html<marquee>Towards AI is a great publication platformmarquee># Personal Details# Information.Name = 'Peter' # {type: "string"}Age = 25 # {type: "slider", min: 1, max: 100}zip = 1234 # {type: "number"}Date = '2020-01-26' # {type: "date"}Gender = "Male" # ['Male', 'Female', 'Other']# ---print("Submitting the form")print(string_type, slider_value, number, date, pick_me)print("Submitted")

x = np.arange(-10,10)y = np.power(x,3)y1 = np.power(x,3) + np.power(x,2) + xplt.scatter(x,y1,c="red")plt.scatter(x,y)
import matplotlib.pyplot as pltimport numpy as npimport seaborn as snslength = 10data = 5 + np.random.randn(length, length)data += np.arange(length)data += np.reshape(np.arange(length), (length, 1))sns.heatmap(data)plt.show()

import tensorflow as tftry:tpu = tf.distribute.cluster_resolver.TPUClusterResolver()print('Running on TPU', tpu.cluster_spec().as_dict()['worker'])except?ValueError:????print('Exception')

Google colab 實現(xiàn):https://colab.research.google.com/drive/1ymIYzFg4Q7iBjnTut31aBqPHgOpkjHYD?usp=sharing
Github repository:https://github.com/towardsai/tutorials/tree/master/google_colab_tutorial
推薦閱讀
1
2
3
4
崔慶才
靜覓博客博主,《Python3網(wǎng)絡(luò)爬蟲開發(fā)實戰(zhàn)》作者
隱形字
個人公眾號:進擊的Coder
長按識別二維碼關(guān)注
好文和朋友一起看~


