我尝试在 TPU 上运行带有 DENSE 层的简单自动编码器。但是发生了这个错误。当我在 GPU 和 CPU 上运行它时没问题。我正在使用 Google Colab
我尝试按照 Tensorflow 的建议将第一层的 input_dims 更改为 input_shape。
这是我的代码示例:
def dae (input_dims, output_dims, epoch, activation):
model = tf.keras.models.Sequential()
#model.add(tf.keras.layers.Dense(input_dims, input_dim = 8000))
model.add(tf.keras.layers.GaussianNoise(0.5, input_shape=(input_dims, )))
model.add(tf.keras.layers.Dense(output_dims))
model.add(tf.keras.layers.Activation(activation))
model.add(tf.keras.layers.Dense(input_dims))
model.add(tf.keras.layers.Activation(activation))
model.summary()
return model
使用 TPU 编译和运行。
autoencoder = dae(input_dims =8000, output_dims = 5000, epoch = 30, activation = 'relu')
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
autoencoder,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
)
)
tpu_model.compile(
optimizer=tf.train.AdamOptimizer(learning_rate=1e-3),
loss=tf.keras.losses.mae,
metrics=['accuracy']
)
训练模型。
tpu_model.fit(
small_train, small_train, epochs = 30, batch_size = 16, validation_split=0.2
)
突然发生这个错误
Train on 68 samples, validate on 14 samples
Epoch 1/30
INFO:tensorflow:New input shapes; (re-)compiling: mode=train (# of cores 8), [TensorSpec(shape=(2,), dtype=tf.int32, name='core_id_100'), TensorSpec(shape=(2, 8000), dtype=tf.float32, name='gaussian_noise_4_input_10'), TensorSpec(shape=(2, 8000), dtype=tf.float32, name='activation_11_target_10')]
INFO:tensorflow:Overriding default placeholder.
INFO:tensorflow:Remapping placeholder for gaussian_noise_4_input
INFO:tensorflow:Started compiling
INFO:tensorflow:Finished compiling. Time elapsed: 5.957217454910278 secs
INFO:tensorflow:Setting weights on TPU model.
48/68 [====================>.........] - ETA: 5s - loss: 2.9962 - acc: 0.0000e+00 INFO:tensorflow:New input shapes; (re-)compiling: mode=train (# of cores 8), [TensorSpec(shape=(0,), dtype=tf.int32, name='core_id_100'), TensorSpec(shape=(0, 8000), dtype=tf.float32, name='gaussian_noise_4_input_10'), TensorSpec(shape=(0, 8000), dtype=tf.float32, name='activation_11_target_10')]
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1658 try:
-> 1659 c_op = c_api.TF_FinishOperation(op_desc)
1660 except errors.InvalidArgumentError as e:
InvalidArgumentError: slice index 0 of dimension 0 out of bounds. for 'strided_slice_12' (op: 'StridedSlice') with input shapes: [0], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
18 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1660 except errors.InvalidArgumentError as e:
1661 # Convert to ValueError for backwards compatibility.
-> 1662 raise ValueError(str(e))
1663
1664 return c_op
ValueError: slice index 0 of dimension 0 out of bounds. for 'strided_slice_12' (op: 'StridedSlice') with input shapes: [0], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>.