[英]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.