运行时重新启动后,Colab的TensorFlow 2更改回Tensorflow 1

时间:2019-11-01 21:42:24

标签: tensorflow google-colaboratory

我使用以下命令将TF从1.15重新启动到TF2:

!pip install tensorflow==2.0
import tensorflow as tf
print(tf.__version__)

但是,当我重新启动colab笔记本时(在单元格上方的第一行注释掉),

说我的TF版本是1.15?

我应该怎么做才能使tensorflow保留在版本2中?

谢谢

CS

3 个答案:

答案 0 :(得分:2)

魔术命令也是一个选项:

try:
  # Use the %tensorflow_version magic if in colab.
  %tensorflow_version 2.x
except Exception:
  pass

import tensorflow as tf

从Udacity示例中找到了代码:https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l02c01_celsius_to_fahrenheit.ipynb#scrollTo=-ZMgCvSRFqxE

答案 1 :(得分:1)

将安装命令! pip install tensorflow==2.0放置在其自己的代码单元中,并将导入命令安装在安装单元之后的单独单元中。

如果您希望保存安装,请将其保存到Google驱动器,然后执行以下操作:
运行! pip install tensorflow==2.0后,在其下面的单元格中运行;

from google.colab import drive
drive.mount('/content/drive')
! pip freeze --local > /content/drive/My\ Drive/installed.txt

#`then in another cell run` (this is what you'll need to run always to restore your installations)

from google.colab import drive
drive.mount('/content/drive')
! pip install --upgrade --force-reinstall `cat/content/drive/My\ Drive/installed

然后点击链接以获取用于安装驱动器的访问令牌。

答案 2 :(得分:0)

未来导入import absolute_import,division,print_function,unicode_literals

try:
  # Use the %tensorflow_version magic if in colab.
  %tensorflow_version 2.x
except Exception:
  pass

import tensorflow as tf

import numpy as np
import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)