本文介绍了AssertionError:无法计算输出张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用功能性API构建一个采用多个输入和多个输出的模型.我遵循了来创建代码.

I am trying to build a model that takes multiple inputs and multiple outputs using a functional API. I followed this to create the code.

def create_model_multiple():
    input1 = tf.keras.Input(shape=(13,), name = 'I1')
    input2 = tf.keras.Input(shape=(6,), name = 'I2')
    hidden1 = tf.keras.layers.Dense(units = 4, activation='relu')(input1)
    hidden2 = tf.keras.layers.Dense(units = 4, activation='relu')(input2)
    merge = tf.keras.layers.concatenate([hidden1, hidden2])
    hidden3 = tf.keras.layers.Dense(units = 3, activation='relu')(merge)
    output1 = tf.keras.layers.Dense(units = 2, activation='softmax', name ='O1')(hidden3)
    output2 = tf.keras.layers.Dense(units = 2, activation='softmax', name = 'O2')(hidden3)
    model = tf.keras.models.Model(inputs = [input1,input2], outputs = [output1,output2])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model

我的model.fit命令如下:

My model.fit command looks like this:

history = model.fit({'I1':train_data, 'I2':new_train_data},
                    {'O1':train_labels, 'O2': new_target_label},
                    validation_data=(val_data,val_labels),
                    epochs=100,
                    verbose = 1)

输入数据的形状如下:
train_data是(192,13)
new_train_data是(192,6)
train-labels,new_target_labels为(192,)
该代码运行了几个步骤,然后引发了此错误:

The shapes of input data are as follows:
train_data is (192,13)
new_train_data is (192,6)
train-labels,new_target_labels is (192,)
The code runs for a few steps then raises this error:

Epoch 1/100
1/6 [====>.........................] - ETA: 0s - loss: 360.3317 - O1_loss: 127.8019 - O2_loss: 232.5298 - O1_accuracy: 0.3438 - O2_accuracy: 0.4062
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-29-db61ad0a9d8b> in <module>
      3                     validation_data=(val_data,val_labels),
      4                     epochs=100,
----> 5                     verbose = 1)

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
     64   def _method_wrapper(self, *args, **kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self, *args, **kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\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)
    870               workers=workers,
    871               use_multiprocessing=use_multiprocessing,
--> 872               return_dict=True)
    873           val_logs = {'val_' + name: val for name, val in val_logs.items()}
    874           epoch_logs.update(val_logs)

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
     64   def _method_wrapper(self, *args, **kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self, *args, **kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\training.py in evaluate(self, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, return_dict)
   1079                 step_num=step):
   1080               callbacks.on_test_batch_begin(step)
-> 1081               tmp_logs = test_function(iterator)
   1082               # Catch OutOfRangeError for Datasets of unknown size.
   1083               # This blocks until the batch has finished executing.

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
    578         xla_context.Exit()
    579     else:
--> 580       result = self._call(*args, **kwds)
    581 
    582     if tracing_count == self._get_tracing_count():

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
    616       # In this case we have not created variables on the first call. So we can
    617       # run the first trace but we should fail if variables are created.
--> 618       results = self._stateful_fn(*args, **kwds)
    619       if self._created_variables:
    620         raise ValueError("Creating variables on a non-first call to a function"

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
   2417     """Calls a graph function specialized to the inputs."""
   2418     with self._lock:
-> 2419       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
   2420     return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
   2421 

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
   2772           and self.input_signature is None
   2773           and call_context_key in self._function_cache.missed):
-> 2774         return self._define_function_with_shape_relaxation(args, kwargs)
   2775 
   2776       self._function_cache.missed.add(call_context_key)

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\function.py in _define_function_with_shape_relaxation(self, args, kwargs)
   2704         relaxed_arg_shapes)
   2705     graph_function = self._create_graph_function(
-> 2706         args, kwargs, override_flat_arg_shapes=relaxed_arg_shapes)
   2707     self._function_cache.arg_relaxed[rank_only_cache_key] = graph_function
   2708 

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2665             arg_names=arg_names,
   2666             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667             capture_by_value=self._capture_by_value),
   2668         self._function_attributes,
   2669         # Tell the ConcreteFunction to clean up its graph once it goes out of

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\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)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
    439         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    440         # the function a weak reference to itself to avoid a reference cycle.
--> 441         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    442     weak_wrapped_fn = weakref.ref(wrapped_fn)
    443 

c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

AssertionError: in user code:

    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\training.py:941 test_function  *
        outputs = self.distribute_strategy.run(
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\training.py:909 test_step  **
        y_pred = self(x, training=False)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:927 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\network.py:719 call
        convert_kwargs_to_constants=base_layer_utils.call_context().saving)
    c:\users\aniket\documents\aniket\learning-ml\ml_env\lib\site-packages\tensorflow\python\keras\engine\network.py:899 _run_internal_graph
        assert str(id(x)) in tensor_dict, 'Could not compute output ' + str(x)

    AssertionError: Could not compute output Tensor("O1_6/Identity:0", shape=(None, 2), dtype=float32)

具有完整代码的jupyter-notebook在此处

The jupyter-notebook with complete code is here

推荐答案

您必须以正确的格式(例如您的火车)提供validation_data.您必须传递2个输入数据和2个目标...您仅传递一个

you have to provide validation_data in the correct format (like your train). you have to pass 2 input data and 2 targets... you are passing only one

这是一个虚拟的例子

def create_model_multiple():

    input1 = tf.keras.Input(shape=(13,), name = 'I1')
    input2 = tf.keras.Input(shape=(6,), name = 'I2')
    hidden1 = tf.keras.layers.Dense(units = 4, activation='relu')(input1)
    hidden2 = tf.keras.layers.Dense(units = 4, activation='relu')(input2)
    merge = tf.keras.layers.concatenate([hidden1, hidden2])
    hidden3 = tf.keras.layers.Dense(units = 3, activation='relu')(merge)
    output1 = tf.keras.layers.Dense(units = 2, activation='softmax', name ='O1')(hidden3)
    output2 = tf.keras.layers.Dense(units = 2, activation='softmax', name = 'O2')(hidden3)
    model = tf.keras.models.Model(inputs = [input1,input2], outputs = [output1,output2])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model


x1 = np.random.uniform(0,1, (190,13))
x2 = np.random.uniform(0,1, (190,6))
val_x1 = np.random.uniform(0,1, (50,13))
val_x2 = np.random.uniform(0,1, (50,6))

y1 = np.random.randint(0,2, 190)
y2 = np.random.randint(0,2, 190)
val_y1 = np.random.randint(0,2, 50)
val_y2 = np.random.randint(0,2, 50)


model = create_model_multiple()

history = model.fit({'I1':x1, 'I2':x2},
                    {'O1':y1, 'O2': y2},
                    validation_data=([val_x1,val_x2], [val_y1,val_y2]), # <=========
                    epochs=100,
                    verbose = 1)

这篇关于AssertionError:无法计算输出张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:19