【问题标题】:Google Colab: redirect python output to a log fileGoogle Colab:将 python 输出重定向到日志文件
【发布时间】:2018-09-03 02:55:20
【问题描述】:

我正在 Google CoLab 上使用 Tensorflow 训练模型。 训练过程会在屏幕上输出很多行。

有没有办法将所有 python 输出重定向到日志文件并稍后读取?

附: 我试过这个建议: https://colab.research.google.com/drive/1q2mhsj4bwwdQK-KZIxrIIKed8O11MQl0

但它仅适用于系统命令(如“ls -la”等)。使用此代码,python 命令仍在写入标准输出。

!python /root/models/research/object_detection/model_main.py \
    --pipeline_config_path=/drive/data/ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync.config \
    --model_dir=/drive/data/ \
    --num_train_steps=50000 \
    --num_eval_steps=2000 \
    --alsologtostderr

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    可以使用标准 linux 重定向和实用程序“tee”将输出捕获到文件

    !python trainingScripty.py 2>&1 | tee TrainerLog.txt
    

    以上命令应将输出和错误消息捕获到 TrainerLog.txt

    【讨论】:

    • 当我在 colab 中尝试这个命令时: !python test.py 2>&1 | tee "/content/drive/Shared drive/result.txt' 我收到以下错误: tee: '/content/drive/Shared drive/results.txt': 没有这样的文件或目录
    • 很可能是它的输出路径问题。检查所有子路径是否存在且拼写正确。
    【解决方案2】:

    阿米特的回答很好。附带说明:如果您收到类似于以下内容的错误消息:

    tee: output.txt: Transport endpoint is not connected
    

    并且您已使用类似的方式在当前的 colab 笔记本文件中安装了一个驱动器;

    drive.mount('/content/gdrive', force_remount=True)
    

    在您尝试运行的 python 脚本中重新检查您没有尝试再次挂载 gdrive 驱动器可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 2020-10-14
      • 2021-06-27
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2010-09-18
      相关资源
      最近更新 更多