简体   繁体   English

无法合并多个输入 tf.keras 模型/错误:图形断开连接:无法获得张量 Tensor 的值

[英]Cannot merge multiple inputs tf.keras model / Error: Graph disconnected: cannot obtain value for tensor Tensor

I cannot understand why the concatenate layer is not working with year_input when compiling the tf.keras model.我不明白为什么在编译 tf.keras 模型时连接层不能与year_input一起使用。

Details:细节:

  • The 3 layers to concatenate are of type tf.float32 .要连接的 3 个层是tf.float32类型。
  • Using Keras Functional API with Tensorflow 2.1.0 GPU.将 Keras Functional API 与 Tensorflow 2.1.0 GPU 结合使用。
  • The 3 layers are of type: <tf.Tensor 'year_input_ll_12:0' shape=(None, 5) dtype=float32> .这 3 个层的类型为: <tf.Tensor 'year_input_ll_12:0' shape=(None, 5) dtype=float32>
  • If I create a model without year_input the model compiles correctly.如果我创建一个没有year_input的模型,模型将正确编译。
def create_system_classifier_model(df, pretrain_model, create_date_df, 
                                   output_cat_nbr, spec_max_length, heading_max_length):

    heading_input = tf.keras.layers.Input((heading_max_length,), name="heading_input", dtype=tf.int32)
    spec_input = tf.keras.layers.Input((spec_max_length,), name="spec_input", dtype=tf.int32)
    year_input_ts = tf.keras.layers.Input((5,), name="year_input_ll", dtype=tf.float32)

    spec_embedding = pretrain_model(heading_input)[0]
    heading_embedding = pretrain_model(spec_input)[0]

    heading_pool_ts = tf.keras.layers.GlobalAveragePooling1D()(spec_embedding)
    spec_pool_ts = tf.keras.layers.GlobalAveragePooling1D()(heading_embedding)

    concat = tf.keras.layers.concatenate([heading_pool_ts, spec_pool_ts, year_input_ts], axis=1)

    dense_ts_1 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_1')(concat)
    dense_ts_2 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_2')(dense_ts_1)
    dense_ts_3 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_3')(dense_ts_2)
    drop_ts = tf.keras.layers.Dropout(0.2)(dense_ts_3)
    output_ts = tf.keras.layers.Dense(output_cat_nbr, activation='sigmoid')(drop_ts)

    model = tf.keras.models.Model(inputs=[heading_input, spec_input, year_input], outputs=output_ts)

    return model

Function call函数调用

system_classifier_model = create_system_classifier_model(df, 
                                                         distil_bert_model, 
                                                         create_date_df,
                                                         len(system_cat_ls),
                                                         spec_max_length, 
                                                         heading_max_length)

Error错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-171-367b40edc46e> in <module>()
      4                                                          len(system_cat_ls),
      5                                                          spec_max_length,
----> 6                                                          heading_max_length)

5 frames
<ipython-input-170-6b485af7c7be> in create_system_classifier_model(df, pretrain_model, create_date_df, output_cat_nbr, spec_max_length, heading_max_length)
     20     output_ts = tf.keras.layers.Dense(output_cat_nbr, activation='sigmoid')(drop_ts)
     21 
---> 22     model = tf.keras.models.Model(inputs=[heading_input, spec_input], outputs=output_ts)
     23     model = tf.keras.models.Model(inputs=[heading_input, spec_input, year_input], outputs=output_ts)
     24 

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in __init__(self, *args, **kwargs)
    144 
    145   def __init__(self, *args, **kwargs):
--> 146     super(Model, self).__init__(*args, **kwargs)
    147     _keras_api_gauge.get_cell('model').set(True)
    148     # initializing _distribution_strategy here since it is possible to call

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in __init__(self, *args, **kwargs)
    167         'inputs' in kwargs and 'outputs' in kwargs):
    168       # Graph network
--> 169       self._init_graph_network(*args, **kwargs)
    170     else:
    171       # Subclassed network

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
    322     # Keep track of the network's nodes and layers.
    323     nodes, nodes_by_depth, layers, _ = _map_graph_network(
--> 324         self.inputs, self.outputs)
    325     self._network_nodes = nodes
    326     self._nodes_by_depth = nodes_by_depth

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in _map_graph_network(inputs, outputs)
   1674                              'The following previous layers '
   1675                              'were accessed without issue: ' +
-> 1676                              str(layers_with_complete_input))
   1677         for x in nest.flatten(node.output_tensors):
   1678           computable_tensors.add(id(x))

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("year_input_ll_11:0", shape=(None, 5), dtype=float32) at layer "year_input_ll". The following previous layers were accessed without issue: ['spec_input', 'heading_input', 'tf_distil_bert_model_2', 'tf_distil_bert_model_2', 'global_average_pooling1d_72', 'global_average_pooling1d_73']

This code works:此代码有效:

def create_system_classifier_model(df, pretrain_model, create_date_df, 
                                   output_cat_nbr, spec_max_length, heading_max_length):

    heading_input = tf.keras.layers.Input((heading_max_length,), name="heading_input", dtype=tf.int32)
    spec_input = tf.keras.layers.Input((spec_max_length,), name="spec_input", dtype=tf.int32)
    year_input_ts = tf.keras.layers.Input((5,), name="year_input_ll", dtype=tf.float32)

    spec_embedding = pretrain_model(heading_input)[0]
    heading_embedding = pretrain_model(spec_input)[0]

    heading_pool_ts = tf.keras.layers.GlobalAveragePooling1D()(spec_embedding)
    spec_pool_ts = tf.keras.layers.GlobalAveragePooling1D()(heading_embedding)

    concat = tf.keras.layers.concatenate([heading_pool_ts, spec_pool_ts, year_input_ts], axis=1)

    dense_ts_1 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_1')(concat)
    dense_ts_2 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_2')(dense_ts_1)
    dense_ts_3 = tf.keras.layers.Dense(768, activation='relu', name='dense_ts_3')(dense_ts_2)
    drop_ts = tf.keras.layers.Dropout(0.2)(dense_ts_3)
    output_ts = tf.keras.layers.Dense(output_cat_nbr, activation='sigmoid')(drop_ts)

    model = tf.keras.models.Model(inputs=[heading_input, spec_input, year_input], outputs=output_ts)

    return model

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图断开连接:无法获取张量 Tensor Input Keras Python 的值 - Graph disconnected: cannot obtain value for tensor Tensor Input Keras Python 图断开:无法获取张量 Tensor Keras Python 的值 - Graph disconnected: cannot obtain value for tensor Tensor Keras Python keras - 图断开连接:无法获取张量 KerasTensor 的值 - keras - Graph disconnected: cannot obtain value for tensor KerasTensor DenseNet:图断开:无法获得张量值 - DenseNet: Graph disconnected: cannot obtain value for tensor 如何解决:Graph disconnected: cannot get value for tensor Tensor? - How to solve problem of : Graph disconnected: cannot obtain value for tensor Tensor? ValueError: Graph disconnected: cannot get value for tensor Tensor - ValueError: Graph disconnected: cannot obtain value for tensor Tensor 如何解决 ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor - How to resolve ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor 如何修复 ValueError:图表已断开:无法获取 tensorflow 中张量的值? - How to fix ValueError: Graph disconnected: cannot obtain value for tensor in tensorflow? 图断开连接:无法获取张量 KerasTensor() 迁移学习的值 - Graph disconnected: cannot obtain value for tensor KerasTensor() Transfer learning ValueError:图形已断开连接:无法获取张量Tensor的值…访问以下先前的层时没有出现问题:[] - ValueError: Graph disconnected: cannot obtain value for tensor Tensor…The following previous layers were accessed without issue: []
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM