使用命令行在ipython笔记本中运行python文件 [英] running a python file from the ipython notebook using command line in loop

查看:377
本文介绍了使用命令行在ipython笔记本中运行python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个使用training.py进行训练的模型.我想调整超参数,并通过更改传递的参数从笔记本中循环运行以下脚本.

I have built a model that trains using training.py. I want to tune the hyperparameters and run the following script from the notebook in loop by varying the arguments passed.

python training.py --cuda --emsize 1500 --nhid 1500 --dropout 0.65 --epochs 10

例如:如果超参数丢失,我希望能够通过更改丢失值并绘制图形来循环运行脚本.

For eg: If the hyperparameter is dropout, I want to be able to run the script in loop by varying dropout values and plot the graph.

推荐答案

您可以在ipython环境中使用!运行shell命令,

You can run a shell command using ! in an ipython environment as

!ls -l

如果要与变量一起使用,则可以使用{}.

If you want to use it with variables,then you can use {}.

# Supposing you have epochs in e and dropout size in d
!python training.py --cuda --emsize 1500 --nhid 1500 --dropout {d} --epochs {e}

您还可以使用!!而不是!捕获ipython中shell运行的输出. !! magic命令将命令的输出作为字符串列表返回.因此,您可以执行类似的操作

You can also capture the output of shell runs in ipython using !! instead of !. The !! magic command returns the output of the command as a list of strings. So, you can do something like

files = !! ls - l

这篇关于使用命令行在ipython笔记本中运行python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆