今天写代码的时候发现以下报错内容
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-12-741fcb121fdb> in <module>
1 train_generator = DataGenerator(question1_id,question1_segment,question2_id,question2_segment,label_id)
----> 2 model.fit(
3 train_generator.cycle(),
4 steps_per_epoch = len(train_generator),
5 epochs = 1
~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1185 _r=1):
1186 callbacks.on_train_batch_begin(step)
-> 1187 tmp_logs = self.train_function(iterator)
1188 if data_handler.should_sync:
1189 context.async_wait()
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
870 tracing_count = self.experimental_get_tracing_count()
871 with trace.Trace(self._name) as tm:
--> 872 result = self._call(*args, **kwds)
873 compiler = "xla" if self._jit_compile else "nonXla"
874 new_tracing_count = self.experimental_get_tracing_count()
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
914 # This is the first call of __call__, so we have to initialize.
915 initializers = []
--> 916 self._initialize(args, kwds, add_initializers_to=initializers)
917 finally:
918 # At this point we know that the initialization is complete (or less
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
751 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
752 self._concrete_stateful_fn = (
--> 753 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
754 *args, **kwds))
755
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3048 args, kwargs = None, None
3049 with self._lock:
-> 3050 graph_function, _ = self._maybe_define_function(args, kwargs)
3051 return graph_function
3052
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3442
3443 self._function_cache.missed.add(call_context_key)
-> 3444 graph_function = self._create_graph_function(args, kwargs)
3445 self._function_cache.primary[cache_key] = graph_function
3446
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3277 arg_names = base_arg_names + missing_arg_names
3278 graph_function = ConcreteFunction(
-> 3279 func_graph_module.func_graph_from_py_func(
3280 self._name,
3281 self._python_function,
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
997 _, original_func = tf_decorator.unwrap(python_func)
998
--> 999 func_outputs = python_func(*func_args, **func_kwargs)
1000
1001 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
660 xla_context.Exit()
661 else:
--> 662 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
663 return out
664
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
AssertionError: in user code:
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2825 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3600 _call_for_each_replica
return fn(*args, **kwargs)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:838 run_step **
outputs = model.train_step(data)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 train_step
y_pred = self(x, training=True)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1045 __call__
outputs = call_fn(inputs, *args, **kwargs)
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:420 call
return self._run_internal_graph(
/home/xiaoguzai/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:565 _run_internal_graph
assert x_id in tensor_dict, 'Could not compute output ' + str(x)
AssertionError: Could not compute output KerasTensor(type_spec=TensorSpec(shape=(None, 3), dtype=tf.float32, name=None), name='dense_1/Softmax:0', description="created by layer 'dense_1'")
后来发现是因为定义的模型输入与实际的输入不匹配导致的
定义模型之中的操作为
import models
from models import Bert
batch_size = 48
max_seq_len = 128
bertmodel = Bert(maxlen=max_seq_len,batch_size=batch_size)
input_ids = [[keras.layers.Input(shape=(None,),dtype='int32',name="token_ids1"),
keras.layers.Input(shape=(None,),dtype='int32',name="segment_ids1")],
[keras.layers.Input(shape=(None,),dtype='int32',name="token_ids2"),
keras.layers.Input(shape=(None,),dtype='int32',name="segment_ids2")]]
output1 = bertmodel(input_ids[0])
output2 = bertmodel(input_ids[1])
output = K.concatenate([output1,output2],axis=-1)
output = keras.layers.Lambda(lambda seq: seq[:,0,:])(output)
#!!!这里取出0而不取出-1是因为最后一位可能有填充数值
output = keras.layers.Dropout(0.5)(output)
output = keras.layers.Dense(units=768,activation="tanh")(output)
output = keras.layers.Dropout(0.5)(output)
output = keras.layers.Dense(units=3,activation="softmax")(output)
model = keras.Model(inputs=input_ids,outputs=output)
model.compile(optimizer=keras.optimizers.Adam(),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.SparseCategoricalAccuracy(name="acc")])
#from_logits = False输入已经符合某种分布,系统只会把你概率归一化
#from_logits = True输入的是原始数据,系统会帮你softmax之后再进行计算
model.summary()
可以看出这里传入了两波四个变量的input_ids的内容
input_ids = [[keras.layers.Input(shape=(None,),dtype='int32',name="token_ids1"),
keras.layers.Input(shape=(None,),dtype='int32',name="segment_ids1")],
[keras.layers.Input(shape=(None,),dtype='int32',name="token_ids2"),
keras.layers.Input(shape=(None,),dtype='int32',name="segment_ids2")]]
但是在给模型传入参数的过程中,却少传入了一波相应的参数
yield [np.array(batch_token_ids1),np.array(batch_segment_ids1)],np.array(batch_label_ids)
由此报错