警告:tensorflow:模型是用形状(无,57)构造的,但在形状不兼容的输入上被调用(无,0)

时间:2021-05-09 12:55:03

标签: python tensorflow machine-learning keras deep-learning

在这里,我创建了 LTSM 模型

model = Sequential()
model.add(Embedding(1000, 128,input_length = x.shape[1]))
model.add(SpatialDropout1D(0.4))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(2,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
print(model.summary())

我在这一行中犯了一些错误:

classifier=model.fit(X_train, Y_train, epochs = 1, batch_size=128,validation_split=0.1, verbose = 1,callbacks=callbacks)

ValueError: in user code: /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) /usr/local /lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function ** 输出 = model.distribute_strategy.run(run_step, args=(data,)) /usr/local/lib /python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 运行返回 self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /usr/local/lib/python3.7/dist -packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica 返回 self._call_for_each_replica(fn, args, kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py :3417 _call_for_each_replica 返回 fn(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step ** 输出 = model.train_step(数据)/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step y_pred = self(x, training=True) /usr/local/lib/python3. 7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012 call 输出 = call_fn(inputs, *args, **kwargs) /usr/local/lib/python3.7 /dist-packages/tensorflow/python/keras/engine/sequential.py:375 call return super(Sequential, self).call(inputs, training=training, mask=mask) /usr/local/lib/python3.7/ dist-packages/tensorflow/python/keras/engine/functional.py:425 调用输入,training=training,mask=mask) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine /functional.py:560 _run_internal_graph 输出 = node.layer(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:660 call return super(RNN, self).call(inputs, **kwargs) /usr/local/lib/python3.7/dist-p ackages/tensorflow/python/keras/engine/base_layer.py:1012 call 输出 = call_fn(inputs, *args, **kwargs) /usr/local/lib/python3.7/dist-packages /tensorflow/python/keras/layers/recurrent_v2.py:1185 call zero_output_for_mask=self.zero_output_for_mask) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py​​:201 包装器返回目标(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4345 rnn [inp[0] for inp in flatted_inputs]) /usr/local /lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4345 [inp[0] for inp in flatted_inputs]) /usr/local/lib/python3.7/dist-packages/tensorflow/ python/util/dispatch.py​​:201 包装器返回目标(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:1047 _slice_helper name=name ) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py​​:201 包装器返回目标(*args,**kwargs)/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:1219 strided_sliceshrink_axis_mask=shrink_axis_mask)/usr/local/lib/python3. 7/dist-packages/tensorflow/python/ops/gen_array_ops.py:10479 strided_slice shrink_axis_mask=shrink_axis_mask, name=name) /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/op_def_library.py :750 _apply_op_helper attrs=attr_protos, op_def=op_def) /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py:592 _create_op_internal compute_device) /usr/local/lib/python3.7 /dist-packages/tensorflow/python/framework/ops.py:3536 _create_op_internal op_def=op_def) /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:2016 init control_input_ops, op_def) /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:1856 _create_c_op 引发 ValueError(str(e)) V alueError: 维度 0 的切片索引 0 越界。对于 '{{node Sequential_9/lstm_4/strided_slice_2}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0,shrink_axis_mask=1](sequential_4/translstm sequence_9/lstm_4/strided_slice_2/stack、sequential_9/lstm_4/strided_slice_2/stack_1、sequential_9/lstm_4/strided_slice_2/stack_2)' 输入形状:[0,?,128], [1], [1], [1] 和计算输入张量:输入[1] = <0>,输入[2] = <1>,输入[3] = <1>。站点:stackoverflow.com

为什么我会收到这样的错误信息?我浏览了其他标题但找不到答案

0 个答案:

没有答案