python - 使用 google colab TPU 时出现 ValueError : slice index 0 of dimension 0 out of bounds.

标签 python tensorflow google-colaboratory tpu

我尝试在 TPU 上运行带有 DENSE 层的简单自动编码器。但这个错误发生了。当我在GPU和CPU上运行它时,一切都OK。我正在使用 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>.

最佳答案

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

样本数 % batchsize = 0

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

关于python - 使用 google colab TPU 时出现 ValueError : slice index 0 of dimension 0 out of bounds.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962508/

相关文章:

python - web2py适合大型公共(public)网站吗?

python - 如何在 TensorFlow 对象检测 API 中保留类特定权重的同时重置类

python - tensorflow CNN损失函数在tensorboard中上下(振荡),如何去除它们?

jupyter-notebook - 如何解决 google colab 上没有名为 'nltk.translate.meteor_score' 的模块

python - 父进程无法访问 PyQt 中的共享内存

python - 如何修复我的 Python 函数,使其返回时带有输入提示?

python - 如何创建调用生成器函数的生成器函数

python - tf.layers.conv2d和tf.contrib.slim.conv2d之间的区别

python - 如何从 Google Colab 笔记本中的 gmplot draw 命令查看 html 输出文件?

google-drive-api - 如何以编程方式获取当前 Google Colab 笔记本的 Google Drive ID?