本文介绍了Keras TPU.编译失败:检测到不支持的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google Colab TPU运行我的keras UNet模型,我遇到了UpSampling2D这个问题.有解决方案或解决方法吗?

I try to run my keras UNet model using Google Colab TPU and I faced this problem with UpSampling2D. Any solutions or workaround?

要运行的代码:

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import UpSampling2D

model = Sequential()
model.add(UpSampling2D((2, 2), input_shape=(16, 16, 1)))
model.compile(optimizer=tf.train.RMSPropOptimizer(learning_rate=0.01),
              loss='binary_crossentropy', metrics=['acc'])

TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
tf.logging.set_verbosity(tf.logging.INFO)


model = tf.contrib.tpu.keras_to_tpu_model(
    model,strategy=tf.contrib.tpu.TPUDistributionStrategy(
        tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

X = np.zeros((1024, 16, 16, 1))
Y = np.zeros((1024, 32, 32, 1))

model.fit(X, Y, batch_size=1024)

错误:

推荐答案

从该错误看来,您的Tensorflow后端(ResizeNearestNeighbor)图中针对Keras的操作之一当前与TPU不兼容.目前有少量Tensorflow操作不适用于TPU(云TPU常见问题解答).

From the error it looks like one of the operations in your Tensorflow backend (ResizeNearestNeighbor) graph for Keras is currently not compatible with TPUs. There are a small number of Tensorflow ops that are currently not available for TPUs (Cloud TPU FAQs).

您可以在此处查阅与TPU兼容的Tensorflow操作的当前列表. .您还可以使用Tensorboard查看 TPU兼容性图.

You can check out the current list of TPU compatible Tensorflow ops here. You can also use Tensorboard to see TPU Compatibility Graphs.

作为一种解决方法,您可以尝试结合使用与TPU兼容的Tensorflow op,以复制ResizeNearestNeighbor的行为.特别是,您可能对 ResizeBilinear 感兴趣. Op,它与TPU兼容.

As a workaround, you can try combine the TPU compatible Tensorflow ops to replicate the behavior of ResizeNearestNeighbor. In particular, you may be interested in the ResizeBilinear Op, which is TPU compatible.

这篇关于Keras TPU.编译失败:检测到不支持的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 08:53