用 Python 使用 Google Colab?岂止是炫酷
共 6577字,需浏览 14分钟
·
2020-12-06 15:24
阅读本文大概需要 3 分钟。
选自Medium
作者:Towards AI Team
机器之心编译
机器之心编辑部
一篇教你如何使用 Google Colab,更好利用免费资源的文章。
提供了免费的 Jupyter notebook 环境;
带有预安装的软件包;
完全托管在谷歌云上;
用户无需在服务器或工作站上进行设置;
Notebook 会自动保存在用户的 Google Drive 中;
提供基于浏览器的 Jupyter notebook;
完全免费,且提供 GPU 和 TPU(Pro 用户可以使用更多资源,但需要付费);
支持 Python 2 和 Python 3;
提供两种硬件加速器:GPU 和 TPU。
启动 Google Colab
从弹框中选择 GitHub 这一项。
输入 GitHub 项目 URL 并搜索以获取代码
将完整代码一键上传到 Google Colab notebook
打开 Kaggle
转至「我的账户」
向下滚动到「API」部分
如果需要,先单击「Expire API Token」以删除先前的 token
点击「Create New API Token」,生成一个新的 token 并下载一个名为「kaggle.json」的 JSON 文件
「kaggle.json」文件包含用户名和密钥,如下所示:
!pip install -q kaggle
from google.colab import files
files.upload()
!kaggle datasets list
!kaggle competitions download -c competitive-data-science-predict-future-sales
import globimport pandas as pdfrom google.colab import drive
drive.mount('/gdrive')
授权码输入框
单击链接并生成授权码
从 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_lib
tf.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 drive
drive.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
history
time
!uptime
!free -hprint("-"*100)
!lscpuprint("-"*70)
"List all running VM processes."ps -efecho "Done" %shecho
%%html<marquee>Towards AI is a great publication platformmarquee>
#'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") Personal Details# Information.Name =
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 tf
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
print('Exception')
Google colab 实现: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网络爬虫开发实战》作者
隐形字
个人公众号:进击的Coder
长按识别二维码关注
好文和朋友一起看~