本地连接远程服务器上的jupyter notebook
1.服务端安装jupyter notebook
pip install jupyter
conda install notebook
2.服务端启动jupyter notebook
jupyter notebook --no-browser --port=<PORT>
是你指定的端口号,比如9998
3.在本地连接服务器jupyter notebook
打开powershell,输入
#<PORT>就是你在服务器端输入的端口号
#<REMOTE_USER>是你远程服务器的用户名
#<REMOTE_HOST>是你远程服务器的网站ip
ssh -L 8080:localhost:<PORT> <REMOTE_USER>@<REMOTE_HOST>
4.接着在本地浏览器输入http://localhost:8080/就可以连了
5.连接上后可能会无法创建新文件,无法运行代码,报错
Traceback (most recent call last): File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/tornado/web.py", line 1704, in _execute result = await result File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/tornado/gen.py", line 769, in run yielded = self.gen.throw(*exc_info) # type: ignore File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/services/contents/handlers.py", line 207, in post yield self._new_untitled(path, type=type, ext=ext) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/tornado/gen.py", line 762, in run value = future.result() File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/tornado/gen.py", line 234, in wrapper yielded = ctx_run(next, result) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/tornado/gen.py", line 162, in _fake_ctx_run return f(*args, **kw) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/services/contents/handlers.py", line 159, in _new_untitled model = yield maybe_future(self.contents_manager.new_untitled(path=path, type=type, ext=ext)) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/services/contents/manager.py", line 395, in new_untitled name = self.increment_filename(untitled + ext, path, insert=insert) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/services/contents/manager.py", line 347, in increment_filename if not self.exists(u'{}/{}'.format(path, name)): File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/services/contents/filemanager.py", line 240, in exists return exists(os_path) File "/data/anaconda3/envs/pjw1/lib/python3.6/site-packages/notebook/utils.py", line 36, in exists os.lstat(path) UnicodeEncodeError: 'ascii' codec can't encode characters in position 38-40: ordinal not in range(128)
遇到这种情况需是由于处理路径时出现了路径编码问题,尤其是包含了非ASCII字符,这种问题通常出现在Jupyter Notebook尝试创建新文件或检查文件是否存在时出现,解决这个问题,可以尝试以下方法:
# 设置Jupyter Notebook使用UTF-8编码 ,在服务器的终端中执行以下命令
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
重启jupyter后发现可以正常创建新文件
使用过程中报错问题处理
- 1.jupyter notebook中使用argparse模块解析参数报错:
usage: ipykernel_launcher.py [-h] [--flag]
ipykernel_launcher.py: error: unrecognized arguments: -f /home/pengjw/.local/share/jupyter/runtime/kernel-e73d5691-0c3b-4d79-811f-a21a191ec2b5.json
- 在 Jupyter Notebook 中使用 %run 命令执行脚本时,Jupyter 会默认添加一个 -f(或 --file)的参数,用于指定 kernel 的连接文件。这就是你看到的 -f 后面跟着的一长串路径。在解析命令行参数时,argparse 不知道如何处理这个参数,因为它不是你明确定义的一个参数。为了解决这个问题,你可以告诉 argparse 忽略未知的命令行参数。在 argparse 的 parse_args() 方法中,可以使用 parse_known_args() 方法,它将解析已知的参数,并将未知的参数作为列表返回。
# 正确样例
import argparse
# 创建 ArgumentParser 对象
parser = argparse.ArgumentParser(description='Example of boolean command line argument')
# 添加命令行参数
parser.add_argument('--flag', action='store_true', help='Enable the flag')
# 解析已知的参数,将未知的参数返回到 extra_args 列表中
args, extra_args = parser.parse_known_args()
# 获取解析后的参数值
flag_enabled = args.flag
# 打印结果
print(f'Flag is enabled: {flag_enabled}')
# 打印未知的参数
print(f'Extra args: {extra_args}')
- 2.jupyter notebook加载预训练模型报错Unexpected key(s) in state_dict: "model", "optimizer", "lr_scheduler", "epoch", "args".
import torch
# 初始化模型
model = YourModel() # 替换为你的模型类
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 指定模型权重文件路径
model_weights_path = 'path_to_your_model_weights.pth'
# 加载模型权重
model.load_state_dict(torch.load(model_weights_path, map_location=device))
- 错误原因可能是模型是使用cuda训练的,则加载方式需使用cuda,当前模型除了保存模型参数,还保存了其他参数,只需加载模型参数
- 正确加载方式
model.load_state_dict(torch.load(model_weights_path, map_location=device)['model'])