Jupyter在服务器上的使用

应用

可以在浏览器上进行python的编译执行,分步执行,更直观方便。

步骤

安装jupyter

可以选择使用conda安装

conda install jupyter notebook

或使用pip

pip install jupyter

如果你有任何jupyter notebook命令的疑问,可以考虑查看官方帮助文档,命令如下:

jupyter notebook --help

配置jupython文件参考

1、在当前用户的根目录创建一个名为nbserver的配置文件。

$ ipython profile create nbserver

2、这将创建一个文件夹,其中包含一些原始的配置文件。跳转到这个文件夹进行一些配置

$ cd ~/.ipython/profile_nbserver/

3、由于ipython Notebook要求https连接,因此需要创建一个ssl证书。

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

这之后可能需要你填什么country name,province name什么的,自己随便填填无关大雅
4、启动ipython:
ipython其实就是jupyter的底层shell界面,所以我们装的anaconda里面会自带jupyter,同时也有了ipython

$ ipython

创建远程连接密码:

In [1]: from notebook.auth import passwd
In [2]: passwd()

注意:设置密码的时候,比如数字密码,不要用小键盘的输入,建议用字母键上的数字输入,且网页登录的时候,也不要用小键盘输入!!!

然后会输出:
Out [2]: sha1:23e342d43.. #这是我的密码的hash值,后段被删除,你的密码得到的结果应该不同,这个hash值要记住,之后要写入配置文件
5、然后退出ipython,开始编辑配置文件,通过vi编辑文件的命令如下:

vim ipython_notebook_config.py

该文件或许不存在,那将会新建一个文件。文件写入:

c=get_config() 
c.IPKernelApp.pylab='inline'  # 所有matplotlib的图像都通过iline的方式显示
c.NotebookApp.certfile=u'/home/XXX/.ipython/profile_nbserver/mycert.pem'  # 这一行指向我们刚刚创建的ssl证书,这里的路径要给绝对路径(反正我给相对路径报错)
c.NotebookApp.password=u'sha1:23e342d43..'  # 给出刚刚创建的密码的哈希值
c.NotebookApp.ip='*'  #使用默认IP
c.NotebookApp.port=8888  #给出运行的端口,ipython默认为8888
c.NotebookApp.open_browser=False   # 禁止在运行ipython的同时弹出浏览器

保存退出
6、配置完毕以后就可以运行ipython Notebook的服务端了:

$ jupyter notebook --config=~/.ipython/profile_nbserver/ipython_notebook_config.py #刚才创建的nbserver路径

7、在你的本地浏览器地址栏输入:https://192.168.xxx.X:8888 (这里就输入你服务器的IP地址,加上配置的端口号8888.注意前面的https不能省)
8、重新配置时,删除 ~/.jupyter 文件夹即可。

使用conda环境参考

关联Jupyter Notebook和conda的环境和包——“nb_conda”。安装

conda install nb_conda

执行上述命令能够将你conda创建的环境与Jupyter Notebook相关联,便于你在Jupyter Notebook的使用中,在不同的环境下创建笔记本进行工作。

可以在Conda类目下对conda环境和包进行一系列操作。

image.png

选中后,会将选择的环境添加到默认环境pytohn 3中。

gym使用jupyter参考

gym包在服务器使用无法可视化,会大大影响其使用的便捷性。可以在训练时禁止显示,测试时使用jupyter进行可视化,可以大大提高训练效率和结果的可视化。

训练时,屏蔽下面代码将会禁止显示游戏画面。

env.render()

测试时,使用下面方法将使用matplotlib来进行游戏画面的可视化。
1、首先在服务器上安装 xvfb ,并用 xvfb 运行可显示的 jupyter notebook:

$ sudo apt-get install xvfb
$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook --config=~/.ipython/profile_nbserver/ipython_notebook_config.py

然后在 jupyter 中加入代码:

import matplotlib.pyplot as plt
%matplotlib inline
from IPython import display

可视化函数:

def show_state(env, step=0, info=""):
    plt.figure(3)
    plt.clf()
    plt.imshow(env.render(mode='rgb_array'))
    plt.title("Step: %d %s" % (step, info))
    plt.axis('off')
    
    display.clear_output(wait=True)
    display.display(plt.gcf())

整体实例代码如下:

import matplotlib.pyplot as plt
import gym
%matplotlib inline
from IPython import display

def show_state(env, step=0, info=""):
    plt.figure(3)
    plt.clf()
    plt.imshow(env.render(mode='rgb_array'))
    plt.title("Step: %d %s" % (step, info))
    plt.axis('off')
    
    display.clear_output(wait=True)
    display.display(plt.gcf())

env = gym.make("CartPole-v1")
observation = env.reset()
for _ in range(1000):
  # env.render()
    action = env.action_space.sample() # your agent here (this takes random actions)
    observation, reward, done, info = env.step(action)
    show_state(env, action, info)
    
    if done:
        observation = env.reset()
env.close()

参考

https://www.jianshu.com/p/91365f343585/
https://blog.csdn.net/qq_29762941/article/details/80630133
https://www.cnblogs.com/cenariusxz/p/12666938.html
https://stackoverflow.com/questions/40195740/how-to-run-openai-gym-render-over-a-server
https://stackoverflow.com/questions/16726227/xvfb-failed-start-error

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。