用 Python 使用 Google Colab?豈止是炫酷
↑↑↑點(diǎn)擊上方藍(lán)字,回復(fù)資料,10個(gè)G的驚喜
選自Medium?作者:Towards AI Team?機(jī)器之心編譯
這篇文章教你如何使用 Google Colab,更好地利用免費(fèi)資源。
提供了免費(fèi)的 Jupyter notebook 環(huán)境;
帶有預(yù)安裝的軟件包;
完全托管在谷歌云上;
用戶無(wú)需在服務(wù)器或工作站上進(jìn)行設(shè)置;
Notebook 會(huì)自動(dòng)保存在用戶的 Google Drive 中;
提供基于瀏覽器的 Jupyter notebook;
完全免費(fèi),且提供 GPU 和 TPU(Pro 用戶可以使用更多資源,但需要付費(fèi));
支持 Python 2 和 Python 3;
提供兩種硬件加速器:GPU 和 TPU。

啟動(dòng) Google Colab
從彈框中選擇 GitHub 這一項(xiàng)。

輸入 GitHub 項(xiàng)目 URL 并搜索以獲取代碼

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


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

如果需要,先單擊「Expire API Token」以刪除先前的 token
點(diǎn)擊「Create New API Token」,生成一個(gè)新的 token 并下載一個(gè)名為「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 實(shí)現(xiàn):https://colab.research.google.com/drive/1ymIYzFg4Q7iBjnTut31aBqPHgOpkjHYD?usp=sharing
Github repository:https://github.com/towardsai/tutorials/tree/master/google_colab_tutorial
看到這還沒(méi)來(lái)得及跑的同學(xué),給個(gè)三連好嗎?
機(jī)器學(xué)習(xí)從 入 門 到 精 通 路 線 圖


