tensorflow - Google Colab:TPU 不支持的数据类型:double,由输出 cond_8/Merge:0 引起
问题描述
我正在使用Talos和 Google colab TPU运行 Keras 模型的超参数调整。请注意,我使用的是Tensorflow 1.15.0 和 Keras 2.2.4 -tf。
import os
import tensorflow as tf
import talos as ta
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
def iris_model(x_train, y_train, x_val, y_val, params):
# Specify a distributed strategy to use TPU
resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)
# Use the strategy to create and compile a Keras model
with strategy.scope():
model = Sequential()
model.add(Dense(32, input_shape=(4,), activation=tf.nn.relu, name = "relu"))
model.add(Dense(3, activation=tf.nn.softmax, name = "softmax"))
model.compile(optimizer=Adam(learning_rate=0.1), loss=params['losses'])
# Fit the Keras model on the dataset
out = model.fit(x_train, y_train,
batch_size=params['batch_size'],
epochs=params['epochs'],
validation_data=[x_val, y_val],
verbose=0,
steps_per_epoch=2)
return out, model
x, y = ta.templates.datasets.iris()
# Create a hyperparameter distributions
p = {'losses': ['logcosh'],
'batch_size': (20, 50, 5),
'epochs': [10, 20]}
# Use Talos to scan the best hyperparameters of the Keras model
scan_object = ta.Scan(x, y, model=iris_model, params=p, fraction_limit=0.1, experiment_name='first_test')
使用out = model.fit拟合模型时出现以下错误:
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1382 '\nsession_config.graph_options.rewrite_options.'
1383 'disable_meta_optimizer = True')
-> 1384 raise type(e)(node_def, op, message)
1385
1386 def _extend_graph(self):
InvalidArgumentError: Unsupported data type for TPU: double, caused by output cond_8/Merge:0
解决方案
最近在 TPU 中增加了对 double 的支持。您现在可以参考https://github.com/tensorflow/tensorflow/blob/d0a48afee650b12dde805fadca868d6b113c3c5d/tensorflow/core/tpu/tpu_defs.h#L52了解所有支持的类型。
推荐阅读
- python - 尝试通过真实账户上的官方 python lib 使用 Docusign API 登录时出现问题
- odoo - How to give portal access to a model in odoo
- c# - C# 将控制台应用程序更改为服务?
- c# - 如何在图像上绘制并保存在 UWP 中?
- c# - WPF在失去焦点时删除列表视图突出显示的背景
- java - 如何使用 Mockito @InjectMocks 将 HttpServletRequest 注入到 ContainerRequestFilter
- python - 我的 Django 项目在 PyCharm 上没有运行调试
- python - 在 Python 中打印字符串 (TestDome)
- classloader - 未使用的从 Java 1.8 导入 lib 并使用 Java 1.7 运行
- java - 泛型中的 List 和 ArrayList 特殊行为