我试图在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>.
答案 0 :(得分:0)
我以前也遇到过同样的问题,直到我减少一些样本以确保:
number of samples % batchsize = 0
(batchsize % 8
应该为0似乎很合理)
答案 1 :(得分:0)
我通过减小batch_size解决了这个问题