值:尺寸0的切片索引0超出边界.在使用Google Colab TPU时



我试图在TPU上运行具有密度层的简单自动编码器。但是这个错误发生了。当我在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>.

我之前遇到过同样的问题,直到我切掉一些样本以确保:

number of samples % batchsize = 0

(似乎是合理的,因为batchsize % 8应为0(

我通过减少batch_size

解决了问题

最新更新