如何在Google Colab中导入TensorFlow

时间:2020-07-26 12:38:48

标签: tensorflow google-colaboratory

几个星期前在正常工作时,Google Colab似乎在尝试导入Tensorflow时抛出以下错误

%tensorflow_version 1.x
import sys
import codecs
import tensorflow as tf
from tqdm import tqdm
from chardet import detect
from keras_radam import RAdam
from keras import backend as K
from keras_bert import load_trained_model_from_checkpoint
import os
import numpy as np
import pandas as pd

/usr/local/lib/python3.6/dist-packages/keras/__init__.py in <module>()
      4 except ImportError:
      5     raise ImportError(
----> 6         'Keras requires TensorFlow 2.2 or higher. '
      7         'Install TensorFlow via `pip install tensorflow`')
      8 

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.

注意:正在TPU上运行,并且已经尝试过!pip install tensorflow,但满足了需求

2 个答案:

答案 0 :(得分:0)

这足以满足我的感觉 %tensorflow_version 2.x 将tensorflow导入为tf

这在Google Colab中一直对我有用。我认为问题在于您将%tensorflow_version设为1.x,请尝试将其更改为2.x

答案 1 :(得分:0)

我不得不像下面这样重新排序我的导入,并使用了keras版本2.3.0

%tensorflow_version 1.x
import sys
import codecs
import tensorflow as tf

!pip install keras-bert
!pip install keras-rectified-adam

!pip install keras==2.3.0

from tqdm import tqdm
from chardet import detect
from keras_radam import RAdam
from keras import backend as K
from keras_bert import load_trained_model_from_checkpoint
import os
import numpy as np
import pandas as pd