小编Bob*_*ith的帖子

如何在谷歌colab中使用TPU

Google colab在运行时加速器中引入了TPU.我找到了一个例子,如何在官方Tensorflow github中使用TPU .但这个例子并不适用于Google-colaboratory.它停留在以下行:

tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)
Run Code Online (Sandbox Code Playgroud)

当我在colab上打印可用设备时,它会返回[]TPU加速器.有谁知道如何在colab上使用TPU?

在此输入图像描述

keras tensorflow google-colaboratory google-cloud-tpu

11
推荐指数
1
解决办法
1万
查看次数

在PyTorch中使用TPU

我正在尝试使用Colab的Google Cloud TPU.我可以通过使用Tensorflow来完成本教程.

有人知道是否可以使用PyTorch来使用TPU?如果是这样我该怎么办?你有什么例子吗?

google-cloud-platform pytorch google-colaboratory google-cloud-tpu

9
推荐指数
2
解决办法
5618
查看次数

谷歌Colab错误:无法获得卷积算法.这可能是因为cuDNN无法初始化

UnknownError:无法获得卷积算法.这可能是因为cuDNN无法初始化,因此请尝试查看上面是否打印了警告日志消息.

 [[{{node conv2d_1/convolution}} = Conv2D[T=DT_FLOAT, data_format="NCHW", dilations=[1, 1, 1, 1], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](zero_padding2d_1/Pad, conv2d_1/kernel/read)]]
 [[{{node metrics/acc/Mean/_255}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_563_metrics/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Run Code Online (Sandbox Code Playgroud)

我在Google colab上遇到此错误.Colab的tensorflow版本是1.12.0.

我找不到任何解决方案.原因所有这些都属于本地系统.

python tensorflow cudnn google-colaboratory

8
推荐指数
1
解决办法
3359
查看次数

文件系统方案“[local]”未在 Google Colab TPU 中实现

我在 Google Colab 中使用 TPU 运行时,但在读取文件时遇到问题(不确定)。我使用以下方法初始化了 TPU:

import tensorflow as tf
import os
import tensorflow_datasets as tfds

resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(resolver)
# This is the TPU initialization code that has to be at the beginning.
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
Run Code Online (Sandbox Code Playgroud)

我在 Google Colab 存储的文件夹中有很多图像(例如'/content/train2017/000000000009.jpg')。我运行以下代码:

import tensorflow as tf
def load_image(image_path):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.resize(img, (299, 299))
    img = tf.keras.applications.inception_v3.preprocess_input(img)
    return img, image_path
load_image('/content/train2017/000000000009.jpg')
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

python tensorflow google-colaboratory google-cloud-tpu tpu

8
推荐指数
2
解决办法
5703
查看次数

TF/Keras错误:InputLayer不是Checkpointable

我正在尝试使用简单的猫与狗数据集在Google Colab上新添加的TPU支持.

创建简单的CNN后,我尝试将模型导出到TPU.但它失败了,错误

TypeError: Checkpointable._track_checkpointable() passed type <class 'keras.engine.topology.InputLayer'>, not a Checkpointable.
Run Code Online (Sandbox Code Playgroud)

这是我在Colab上编写的代码.

model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()

train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150,150), batch_size=20, class_mode='binary')

tpu_model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=tf.contrib.tpu.TPUDistributionStrategy(tf.contrib.cluster_resolver.TPUClusterResolver(tpu="grpc://" + os.environ['COLAB_TPU_ADDR'])))
Run Code Online (Sandbox Code Playgroud)

我的猜测是我做错了train_generator.但我不确定它是什么.任何帮助将受到高度赞赏.

keras tensorflow google-colaboratory google-cloud-tpu

7
推荐指数
1
解决办法
1949
查看次数

在Google Colab中设置环境变量

我正在尝试使用Kaggle CLI API,为了做到这一点,而不是kaggle.json用于身份验证,而是使用环境变量来设置凭据

!pip install --upgrade kaggle

!export KAGGLE_USERNAME=abcdefgh
!export KAGGLE_KEY=abcdefgh

!export -p
Run Code Online (Sandbox Code Playgroud)

但是,环境的打印清单。变量不包含我在上面设置的变量。

declare -x CLICOLOR="1"
declare -x CLOUDSDK_CONFIG="/content/.config"
declare -x COLAB_GPU="1"
declare -x CUDA_PKG_VERSION="9-2=9.2.148-1"
declare -x CUDA_VERSION="9.2.148"
declare -x CUDNN_VERSION="7.4.1.5"
declare -x DATALAB_SETTINGS_OVERRIDES="{\"kernelManagerProxyPort\":6000,\"kernelManagerProxyHost\":\"172.28.0.3\",\"jupyterArgs\":[\"notebook\",\"-y\",\"--no-browser\",\"--log-level=DEBUG\",\"--debug\",\"--NotebookApp.allow_origin=\\\"*\\\"\",\"--NotebookApp.log_format=\\\"%(message)s\\\"\",\"--NotebookApp.disable_check_xsrf=True\",\"--NotebookApp.token=\",\"--Session.key=\\\"\\\"\",\"--Session.keyfile=\\\"\\\"\",\"--ContentsManager.untitled_directory=\\\"Untitled Folder\\\"\",\"--ContentsManager.untitled_file=\\\"Untitled File\\\"\",\"--ContentsManager.untitled_notebook=\\\"Untitled Notebook\\\"\",\"--KernelManager.autorestart=True\",\"--ip=\\\"172.28.0.2\\\"\"]}"
declare -x DEBIAN_FRONTEND="noninteractive"
declare -x ENV="/root/.bashrc"
declare -x GIT_PAGER="cat"
declare -x GLIBCPP_FORCE_NEW="1"
declare -x GLIBCXX_FORCE_NEW="1"
declare -x HOME="/root"
declare -x HOSTNAME="2ced809e9844"
declare -x JPY_PARENT_PID="57"
declare -x LANG="en_US.UTF-8"
declare -x LD_LIBRARY_PATH="/usr/lib64-nvidia"
declare -x LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4"
declare -x MPLBACKEND="module://ipykernel.pylab.backend_inline"
declare -x …
Run Code Online (Sandbox Code Playgroud)

kaggle google-colaboratory

6
推荐指数
2
解决办法
3806
查看次数

在简单的colab教程中使用ELMo交换gnews

我正在研究这个colab笔记本:

https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb

我想用ELMo嵌入替换gnews旋转嵌入。

因此,更换

model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
Run Code Online (Sandbox Code Playgroud)

与:

model = "https://tfhub.dev/google/elmo/2"
Run Code Online (Sandbox Code Playgroud)

这里发生了一系列变化,例如需要

tf.compat.v1.disable_eager_execution()
Run Code Online (Sandbox Code Playgroud)

但是我不了解要成功完成此替换的图形形状。具体来说,我看到了。

#model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
model = "https://tfhub.dev/google/elmo/2"

elmo = hub.Module(model, trainable=True, name="{}_module".format("mymod"))

hub_layer = hub.KerasLayer(elmo, 
#                           output_shape=[3,20], 
#                           input_shape=(1,),
                           dtype=tf.string, 
                           trainable=True)

hub_layer(train_examples[:3])
Run Code Online (Sandbox Code Playgroud)

产生

<tf.Tensor 'keras_layer_14/mymod_module_14_apply_default/truediv:0' shape=(3, 1024) dtype=float32>
Run Code Online (Sandbox Code Playgroud)

这似乎很好。但:

model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

# First, I have to build, because I no longer have eager executon.
model.build(input_shape=(None,1024))

model.summary()
Run Code Online (Sandbox Code Playgroud)

然后给出:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-54-8786753617e4> in <module>()
      4 model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
      5 …
Run Code Online (Sandbox Code Playgroud)

tensorflow google-colaboratory tensorflow-hub elmo

6
推荐指数
1
解决办法
115
查看次数

将 Colab 连接到付费 TPU

我想将 Colab 连接到付费 TPU(从免费 TPU 升级)。我使用本指南创建了一个 JSON 密钥:https : //cloud.google.com/docs/authentication/production#auth-cloud-explicit-python,然后将其上传到 Colab。我可以连接到我的存储,但不能连接到 TPU:

%tensorflow_version 2.x
import tensorflow as tf
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './gcp-permissions.json'

# Authenticated API request - works.
storage_client = storage.Client.from_service_account_json(
    'gcp-permissions.json')
print(list(storage_client.list_buckets())

#Accessing the TPU - does not work. Request times out.
cluster_resolver = tf.distribute.cluster_resolver.TPUClusterResolver(
    tpu='My-TPU-Name',
    zone='us-central1-a',
    project='My-Project-Name'
)
Run Code Online (Sandbox Code Playgroud)

我还尝试了 TPUClusterResolver 调用,仅使用 tpu 名称和 'credentials=gcp-permissions.json' - 结果相同。我已经仔细检查了我的 TPU 是否已在 GCP 控制台中启动并运行。它不是抢占式的。我错过了什么?

谢谢!

google-cloud-platform google-colaboratory google-cloud-tpu tpu

6
推荐指数
1
解决办法
725
查看次数

在Tensorflow中使用TPU时,是否有一个可靠的解决方法来保存本地驱动器中的检查点?

对此问题的跟进:

在使用TPU模式时如何从Google Colaboratory保存Tensorflow Checkpoint文件?

使用Tensorflow TPU时保存检查点的官方方法是使用Google Cloud Service.

如果对于那些不希望使用GCS的人有解决方法,我正在工作.也许对于每个变量,执行.eval(),保存变量.然后将save变量设置为每个变量的'init'值.

我预见的一个主要问题是保存和加载优化器的参数.

对于Keras来说,权重似乎从TPU保存到本地

https://colab.research.google.com/github/tensorflow/tpu/blob/master/tools/colab/shakespeare_with_tpu_and_keras.ipynb

INFO:tensorflow:将TPU权重复制到CPU

所以我想也有一个普遍的解决方法,不使用keras.

python tensorflow google-colaboratory google-cloud-tpu

5
推荐指数
1
解决办法
437
查看次数

运行时错误:混合不同的 tf.distribute.Strategy 对象

你好?我在使用TPU编译模型时遇到了一些问题,部分代码如下:

resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TF_MASTER)

tf.contrib.distribute.initialize_tpu_system(resolver)

strategy = tf.contrib.distribute.TPUStrategy(resolver)

with strategy.scope():

  model = create_model()

  model.compile(optimizer=tf.keras.optimizers.Adadelta(),loss='categorical_crossentropy',metrics='accuracy'])
Run Code Online (Sandbox Code Playgroud)

我得到了 RuntimeError: enter image description here

你能帮助我吗?

google-colaboratory google-cloud-tpu tpu

5
推荐指数
1
解决办法
844
查看次数