简体   繁体   English

InvalidArgumentError:序列 Model 中自定义层的维度 0 的切片索引 0 超出范围

[英]InvalidArgumentError: slice index 0 of dimension 0 out of bounds for custom layer in Sequential Model

Getting this error收到此错误

InvalidArgumentError: slice index 0 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/

Some problem in the output of the call method compared to the input of the first dense layer.与第一个密集层的输入相比,调用方法的output中的一些问题。 Changing the output from 'tf.constant[results]' to 'tf.constant[results]' just gives error 'min_ndim=2', got ndim=1.将 output 从 'tf.constant[results]' 更改为 'tf.constant[results]' 只会给出错误 'min_ndim=2',得到 ndim=1。

class TextVectorizationLayer(keras.layers.Layer):

   def __init__(self, **kwargs):
      super().__init__(**kwargs, dynamic=True)
       self.table = {}

   def call(self, inputs, **kwargs):
       review = preprocess(inputs)
       results = []

       for word in self.table:
           if word in review:
               results.append(self.table.get(word))
           else:
               results.append(0)

       return tf.constant([results])

   def adapt(self, data, count):
           reviews = [preprocess(r) for (r,_) in data]
           for review in reviews:
               for word in review.numpy():
                   self.table[word] = \
                       self.table.get(word, 0) + 1

           self.table = OrderedDict(sorted(self.table.items(),
                                 key=lambda x: x[1],
                                 reverse=True)[:count])

           return self.table

sample_string_batches = train_set.take(25)
vectorization = TextVectorizationLayer()
words = vectorization.adapt(sample_string_batches, 400)

model = keras.models.Sequential([
    vectorization,
    keras.layers.Dense(100, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(loss="binary_crossentropy", optimizer="nadam",
              metrics=["accuracy"])
model.fit(train_set, epochs=5, validation_data=val_set)

Train and Val Data is of shape ((),())训练和验证数据的形状为 ((),())

Model: "sequential_15"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
text_vectorization_layer_10  multiple                  0         
_________________________________________________________________
dense_30 (Dense)             multiple                  40100     
_________________________________________________________________
dense_31 (Dense)             multiple                  101       
=================================================================
Total params: 40,201
Trainable params: 40,201

Non-trainable params: 0不可训练参数:0

Please check the layer's "input_shape" parameter as it would have been provided with (0,x) shape.请检查图层的“input_shape”参数,因为它会提供 (0,x) 形状。 thus getting error of index 0从而得到索引 0 的错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError:维度 1 的切片索引 7 超出范围。 [Op:StridedSlice] 名称:strided_slice/ - InvalidArgumentError: slice index 7 of dimension 1 out of bounds. [Op:StridedSlice] name: strided_slice/ Tensorflow 2.0:ValueError:维度 0 的切片索引 0 超出范围 - Tensorflow 2.0: ValueError: slice index 0 of dimension 0 out of bounds 对于尺寸为 1 的维度 0,索引 1 超出范围 - index 1 is out of bounds for dimension 0 with size 1 ValueError: 维度 0 的切片索引 0 越界。 使用 google colab TPU 时 - ValueError: slice index 0 of dimension 0 out of bounds. while using google colab TPU Tensorflow: tfa.image.random_cutout() -- ValueError: 维度 0 的切片索引 3 超出范围 - Tensorflow: tfa.image.random_cutout() -- ValueError: slice index 3 of dimension 0 out of bounds Tensorflow 2:维度 1 的切片索引 64 越界。 [Op:StridedSlice] 名称:caption_generator_5/strided_slice/ - Tensorflow 2: slice index 64 of dimension 1 out of bounds. [Op:StridedSlice] name: caption_generator_5/strided_slice/ 顺序 model tensorflow 中的自定义层 - Custom layer in sequential model tensorflow IndexError:索引 -9223372036854775808 超出尺寸为 2 的维度 1 的范围 - IndexError: index -9223372036854775808 is out of bounds for dimension 1 with size 2 自定义张量流模型上的 InvalidArgumentError - InvalidArgumentError on custom tensorflow model 'IndexError: index 0 is out of bounds for dimension 1 with size 0' 的解决方案是什么? - What is the solution for 'IndexError: index 0 is out of bounds for dimension 1 with size 0'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM