【问题标题】:Pycharm: Is there a way to run a snippet of code without running the entire file?Pycharm:有没有办法在不运行整个文件的情况下运行一段代码?
【发布时间】:2020-11-06 03:03:37
【问题描述】:

我正在自学卷积神经网络的编码。特别是我正在关注“狗对猫”挑战(https://medium.com/@mrgarg.rajat/kaggle-dogs-vs-cats-challenge-complete-step-by-step-guide-part-2-e9ee4967b9)。我正在使用 PyCharm。

在 PyCharm 中,有没有一种方法可以使用经过训练的模型对测试数据进行预测,而不必每次都运行整个文件(从而每次都重新训练模型)?此外,有没有办法跳过准备数据以输入到 CNN 的脚本部分?以类似的方式,PyCharm 是否存储变量 - 我可以在脚本运行后打印单个变量吗?

如果我使用不同的 IDLE 会更好吗?

【问题讨论】:

  • 我建议在 Pycharm 中尝试使用 Python 调试器 -> jetbrains.com/help/pycharm/part-1-debugging-python-code.html 此外,还有一个“评估表达式”按钮,可让您运行代码的特定部分而无需运行所有内容 -> @987654323 @
  • 我没有使用您所指的库的经验,但是在 Pycharm 中,您可以打开底部的交互式控制台(“Python 控制台”),并在其中运行任意代码。如果将文件加载到其中,则可以运行代码(在文件加载后)。另请注意,“IDLE”是一个特定的 IDE。
  • 你想要的是this answer吗?
  • Jupyter Notebooks 可能是要走的路(或 Jupyter Lab - 更新且相当相似)。它非常适合编写可以分段执行的代码。
  • 这能回答你的问题吗? Pycharm: run only part of my Python file

标签: python pycharm conv-neural-network


【解决方案1】:

您可以使用sklearn joblib 将经过训练的模型保存为pickle,并在以后用于预测。

from sklearn.externals import joblib 

# Save the model as a pickle in a file 
joblib.dump(knn, 'filename.pkl') 

# Load the model from the file 
knn_from_joblib = joblib.load('filename.pkl')  

# Use the loaded model to make predictions 
knn_from_joblib.predict(X_test) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多