gpt4 book ai didi

python - 常规 Python 中的 %%writefile 魔术命令

转载 作者:行者123 更新时间:2023-12-02 16:26:44 26 4
gpt4 key购买 nike

我正在从 Jupyter Notebook 复制 Python 代码到 .py 文件以从终端运行它。我找不到应该如何实现这条线的方法:

%%writefile -a submission.py

常规 Python 中与 %%writefile 魔术命令相同的代码是什么?

最佳答案

替换为:

%%writefile -a submission.py

model = Net()
model = model.float()
model.load_state_dict(state_dict)
model = model.to('cpu')
model = model.eval()
obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()
obs = obs / 2
action = model(obs)
return int(action)

通过这个:

submission_ending = '''    model = Net()
model = model.float()
model.load_state_dict(state_dict)
model = model.to('cpu')
model = model.eval()
obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()
obs = obs / 2
action = model(obs)
return int(action)'''

with open('submission.py', mode='a') as file:
file.write(submission_ending)

关于python - 常规 Python 中的 %%writefile 魔术命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64297543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com