首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InvalidArgumentError:序列模型中自定义层维数0的切片索引0

InvalidArgumentError:序列模型中自定义层维数0的切片索引0
EN

Stack Overflow用户
提问于 2020-07-15 23:23:49
回答 1查看 786关注 0票数 1

得到这个错误

代码语言:javascript
复制
InvalidArgumentError: slice index 0 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/

调用方法的输出与第一稠密层的输入相比较的一些问题。将输出从“tf.constantresults”更改为“tf.constantresults”只会导致错误'min_ndim=2',得到ndim=1。

代码语言:javascript
复制
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)

列车和Val数据是形状((),())

代码语言:javascript
复制
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

不可训练参数:0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-19 06:34:06

请检查图层的"input_shape“参数,因为它将提供(0,x)形状。从而得到索引0的误差。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62925158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档