Python ValueError:维度0的切片索引0超出范围。在使用google colab TPU时

Python ValueError:维度0的切片索引0超出范围。在使用google colab TPU时,python,tensorflow,google-colaboratory,tpu,Python,Tensorflow,Google Colaboratory,Tpu,我试着在TPU上运行带有密集层的简单自动编码器。但是这个错误发生了。当我在GPU和CPU上运行它时,它还可以。我在用谷歌Colab 我试着按照Tensorflow的建议,将第一层中的input_dims更改为input_形状 下面是我的代码示例: def dae (input_dims, output_dims, epoch, activation): model = tf.keras.models.Sequential() #model.add(tf.keras.laye

我试着在TPU上运行带有密集层的简单自动编码器。但是这个错误发生了。当我在GPU和CPU上运行它时,它还可以。我在用谷歌Colab

我试着按照Tensorflow的建议,将第一层中的input_dims更改为input_形状

下面是我的代码示例:

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>.
对68个样本进行训练,对14个样本进行验证
纪元1/30
信息:tensorflow:新输入形状;(重新)编译:mode=train(#of cores 8),[TensorSpec(shape=(2,),dtype=tf.int32,name='core\u id\u 100'),TensorSpec(shape=(2,8000),dtype=tf.float32,name='gaussian\u noise\u 4\u input\u 10'),TensorSpec(shape=(2,8000),dtype=tf.float32,name='activation\u 11\u target\u 10')]
信息:tensorflow:覆盖默认占位符。
信息:tensorflow:为高斯噪声输入重新映射占位符
信息:tensorflow:已开始编译
信息:tensorflow:已完成编译。所用时间:5.957217454910278秒
信息:tensorflow:在TPU模型上设置权重。
48/68[===================>…]ETA:5s-损耗:2.9962-acc:0.0000e+00信息:tensorflow:新输入形状;(重新)编译:mode=train(#of cores 8),[TensorSpec(shape=(0,),dtype=tf.int32,name='core\u id\u 100'),TensorSpec(shape=(0,8000),dtype=tf.float32,name='gaussian\u noise\u 4\u input\u 10'),TensorSpec(shape=(0,8000),dtype=tf.float32,name='activation\u 11\u target\u 10')]
---------------------------------------------------------------------------
InvalidArgumentError回溯(最后一次最近调用)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in_create_c_op(图形、节点定义、输入、控制输入)
1658尝试:
->1659 c_op=c_api.TF_FinishOperation(op_desc)
1660错误除外。InvalidArgumentError为e:
InvalidArgumentError:维度0的切片索引0超出范围。对于输入形状为[0]、[1]、[1]、[1]和[1]且具有计算输入张量的“跨步切面”(op:“跨步切面”),输入[1]=,输入[2]=,输入[3]=。
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
18帧
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in_create_c_op(图形、节点定义、输入、控制输入)
1660错误除外。InvalidArgumentError为e:
1661#转换为ValueError以实现向后兼容性。
->1662提升值错误(str(e))
1663
1664返回c_op
ValueError:维度0的切片索引0超出范围。对于输入形状为[0]、[1]、[1]、[1]和[1]且具有计算输入张量的“跨步切面”(op:“跨步切面”),输入[1]=,输入[2]=,输入[3]=。

我以前也遇到过同样的问题,直到我减少了一些样本以确保:

样本数%batchsize=0


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

我通过减少批大小解决了这个问题