遇到此错误
InvalidArgumentError: slice index 0 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/
在调用方法的输出与第一个密集层的输入之间存在一些问题。将输出从’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.tablesample_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)
训练和验证数据的形状为((),())
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,201Trainable params: 40,201
不可训练的参数: 0
回答:
请检查层的”input_shape”参数,因为它可能被提供为(0,x)形状,因此会导致索引0的错误