【发布时间】:2020-03-27 07:49:46
【问题描述】:
我正在使用 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
【问题讨论】:
-
尝试将您的 x_train、y_train、x_val、y_val 转换为 tf.float32。
-
谢谢,我已经解决了。我现在面临另一个问题:stackoverflow.com/questions/58958437/…
标签: tensorflow keras google-colaboratory google-cloud-tpu talos