gpt4 book ai didi

python - 无法合并多个输入 tf.keras 模型/错误 : Graph disconnected: cannot obtain value for tensor Tensor

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:14 25 4
gpt4 key购买 nike

我无法理解为什么在编译 tf.keras 模型时连接层不能与 year_input 一起使用。

详细信息:

  • 要连接的 3 个层的类型为 tf.float32
  • 将 Keras 功能 API 与 Tensorflow 2.1.0 GPU 结合使用。
  • 这 3 层的类型为: <tf.Tensor 'year_input_ll_12:0' shape=(None, 5) dtype=float32>
  • 如果我创建一个没有 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

函数调用

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

错误

---------------------------------------------------------------------------
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']

最佳答案

此代码有效:

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

关于python - 无法合并多个输入 tf.keras 模型/错误 : Graph disconnected: cannot obtain value for tensor Tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59939344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com