Loading... ## 配备K80显卡的Google机器 偶然间发现Google的Colab提供的Jupyter Notebook功能非常好用,而且还提供GPU显卡。 11G显存的K80显卡,12G内存,360G硬盘,至强双核处理器。 ![colab_执行命令.png][1] 真的是良心啊,妈妈再也不用担心我的显卡跑不动了 <img src="https://cdn.jsdelivr.net/gh/ihewro/handsome-static@8.2.0.2/assets/img/emotion/aru/cheer.png" class="emotion-aru"> Update: 目前发现Colab的实例每12小时会回收一次。就需要重新开了~ ## Colab的初步使用 ### 创建Jupyter Notebook 打开这个链接[https://colab.research.google.com/notebooks/welcome.ipynb#recent=true][2]后,点击`New Python 3 Notebook`按钮创建新的Notebook ![创建JupyterNotebook.png][3] 经过几十秒的加载就能看到新的Jupyter Notebook被创建啦,现在我们需要切换到GPU的环境,选择`Runtime`->`Change runtime type` ![选择GPU环境.png][4] 将硬件加速选为`GPU`,然后`Save`即可~ ![选择GPU环境1.png][5] ### 执行Python代码 在Jupyter中可以执行Python代码: ```python a = 1 print(a) print(type(a)) ``` ![执行Python命令.png][6] ### 执行系统命令 在系统命令前面加`!`即可执行 ```bash !nvidia-smi !ls !cat /proc/cpuinfo ``` ![执行系统命令.png][7] ## Colab执行脚本 Colab可以执行各种脚本,然而执行脚本之前通常需要上传和下载文件,所以这一部分会先讲文件的上传和下载,然后就可以执行命令执行脚本了 ### 文件的上传和下载 #### 文件上传 执行脚本,上传文件 ```python from google.colab import files files.upload() ``` ![上传文件.png][8] #### 下载文件 执行脚本,下载文件 ```python from google.colab import files files.download('/path/to/file') ``` ### 执行命令 比如说要执行keras的mnist的demo[https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py][9],先把这个文件传到colab上,然后python执行即可~ ```python # 上传 from google.colab import files files.upload() # 执行 ! python3 mnist_cnn.py ``` 执行结果如下: ![mnist_demo.png][10] 每个EPOCH10s,相当快了。 ## Colab进阶 ### SSH连接Colab实例 Colab的每台机器放在Google机房内,是内网环境,没有公网IP,经过几番Google发现了这个方法。 既然Colab实例可以执行任意命令,那么我们可以在这个实例上安装SSH服务,并将这个机器的22端口反向转发出来,具体如下。 #### 注册ngrok ngrok这个服务可以将内网的端口转发出来,有账户的直接登录[https://dashboard.ngrok.com/user/login][11](支持Github和Google账号登录),没账户的可以注册[https://dashboard.ngrok.com/user/signup][12]注册。 然后访问[https://dashboard.ngrok.com/auth][13]来获得Token。 #### 安装并转发SSH 执行下面的代码后,输入获取的Token ```python #Generate root password import random, string password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20)) #Download ngrok ! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip ! unzip -qq -n ngrok-stable-linux-amd64.zip #Setup sshd ! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null #Set root password ! echo root:$password | chpasswd ! mkdir -p /var/run/sshd ! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config ! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config ! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc ! echo "export LD_LIBRARY_PATH" >> /root/.bashrc #Run sshd get_ipython().system_raw('/usr/sbin/sshd -D &') #Ask token print("Copy authtoken from https://dashboard.ngrok.com/auth") import getpass authtoken = getpass.getpass() #Create tunnel get_ipython().system_raw('./ngrok authtoken {authtoken} && ./ngrok tcp 22 &') #Print root password print("Root password: {}".format(password)) #Get public address ! curl -s http://localhost:4040/api/tunnels | python3 -c \ "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])" ``` 执行完毕后,会输出root密码和端口转发之后的地址,在本机ssh登录即可啦,输出如下: ``` Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service → /lib/systemd/system/getty@.service. Created symlink /etc/systemd/system/multi-user.target.wants/remote-fs.target → /lib/systemd/system/remote-fs.target. Created symlink /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service → /lib/systemd/system/systemd-timesyncd.service. Created symlink /etc/systemd/system/dbus-org.freedesktop.resolve1.service → /lib/systemd/system/systemd-resolved.service. Created symlink /etc/systemd/system/multi-user.target.wants/systemd-resolved.service → /lib/systemd/system/systemd-resolved.service. ln: cannot remove '/etc/resolv.conf': Device or resource busy Created symlink /etc/systemd/system/multi-user.target.wants/ondemand.service → /lib/systemd/system/ondemand.service. Initializing machine ID from random generator. Creating config file /etc/ssh/sshd_config with new version Creating SSH2 RSA key; this may take some time ... 2048 SHA256:4cyZ6SCo73Q6FLm9rKDq3jhrWB3srjhuTySEJqtRg6k root@209bed522146 (RSA) Creating SSH2 ECDSA key; this may take some time ... 256 SHA256:yy/AH2pJWq/QY8QfEod/Dv+kjYm+DcdPPTg8RhwsB5M root@209bed522146 (ECDSA) Creating SSH2 ED25519 key; this may take some time ... 256 SHA256:BLCc19q/QzxEiNtLq7mKNbbtgClQXIfH5BtiXMbEzZU root@209bed522146 (ED25519) Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service. Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service. invoke-rc.d: could not determine current runlevel invoke-rc.d: policy-rc.d denied execution of start. Copy authtoken from https://dashboard.ngrok.com/auth ·········· Root password: 09fcd0efe377 tcp://0.tcp.ngrok.io:17169 ``` 执行`ssh root@0.tcp.ngrok.io -p 17169`就可以登录了~ [1]: https://tba1.sinaimg.com/large/007oxmFJly1fwsv1qrqi1j30o20g642b.jpg [2]: https://colab.research.google.com/notebooks/welcome.ipynb#recent=true [3]: https://tva1.sinaimg.com/large/007oxmFJly1fwsv2mo5l5j30tc0jjac0.jpg [4]: https://tva2.sinaimg.com/large/007oxmFJly1fwsv36gjy3j30mq0d4ac4.jpg [5]: https://tva1.sinaimg.com/large/007oxmFJly1fwsv3gfpoej30mn0ekmz3.jpg [6]: https://tva2.sinaimg.com/large/007oxmFJly1fwsv42ivlaj30nl0bfgn0.jpg [7]: https://tva1.sinaimg.com/large/007oxmFJly1fwsv7f7qnfj30nn0gq0ww.jpg [8]: https://tva2.sinaimg.com/large/007oxmFJly1fwsv7rgidjj30nn08vgn8.jpg [9]: https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py [10]: https://tva4.sinaimg.com/large/007oxmFJly1fwsv885zmpj31090gmk15.jpg [11]: https://dashboard.ngrok.com/user/login [12]: https://dashboard.ngrok.com/user/signup [13]: https://dashboard.ngrok.com/auth Last modification:January 9, 2020 © Allow specification reprint Like If you think my article is useful to you, please feel free to appreciate