【问题标题】:keras - Graph disconnected: cannot obtain value for tensor KerasTensorkeras - 图表断开连接:无法获取张量 KerasTensor 的值
【发布时间】:2021-09-26 15:19:36
【问题描述】:

我一直在尝试使用 Keras 功能 API 创建一个 7 列(功能)模型并将其映射到 6 个类输出。

import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, Dense, Concatenate

input_message = Input(shape=(128,))
x = Dense(64, activation="relu")(input_message)
x = Dense(32, activation="relu")(x)
x = Dense(4, activation="relu")(x)
model_message = Model(inputs=input_message, outputs=x)

input_description = Input(shape=(128,))
x = Dense(64, activation="relu")(input_description)
x = Dense(32, activation="relu")(x)
x = Dense(4, activation="relu")(x)
model_description = Model(inputs=input_description, outputs=x)

input_errors = Input(shape=(2,))
x = Dense(1, activation="relu")(input_errors)
model_errors = Model(inputs=input_errors, outputs=x)

input_panics = Input(shape=(2,))
x = Dense(1, activation="relu")(input_panics)
model_panics = Model(inputs=input_panics, outputs=x)

input_images = Input(shape=(2,))
x = Dense(1, activation="relu")(input_images)
model_images = Model(inputs=input_images, outputs=x)

input_committer = Input(shape=(16,))
x = Dense(4, activation="relu")(input_description)
model_committer = Model(inputs=input_committer, outputs=x)

input_reporter = Input(shape=(6,))
x = Dense(1, activation="relu")(input_reporter)
model_reporter = Model(inputs=input_reporter, outputs=x)

combined = Concatenate()([model_message.output, model_description.output, model_errors.output, 
                         model_panics.output, model_images.output, model_committer.output, model_reporter.output])

z = Dense(6, activation='softmax')(combined)
model = Model(inputs=[model_message.input, model_description.input, model_errors.input, 
                     model_panics.input, model_images.input, model_committer.input, model_reporter.input], 
              outputs=z)

不幸的是,它导致了以下错误:

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 128), dtype=tf.float32, name='input_23'), name='input_23', description="created by layer 'input_23'") at layer "dense_74". The following previous layers were accessed without issue: []

我的功能列表如下:

  1. 消息 - 文本
  2. 说明 - 文字
  3. has_errors - int 表示布尔值
  4. has_panics - int 表示布尔值
  5. has_images - int 表示布尔值
  6. 提交者组 - 分类输入
  7. 记者组 - 分类输入 - 总共 6 个可能的值

我一直在尝试点击以下链接: https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/

所以 2 个问题:

  1. 产生上述错误的原因是什么?
  2. 我不应该为文本应用某种嵌入吗?

提前致谢

【问题讨论】:

    标签: python tensorflow keras text-classification


    【解决方案1】:

    你为什么要创建这么多模型,而你真的想要一个?

    import tensorflow as tf
    from tensorflow.keras import Model
    from tensorflow.keras.layers import Input, Dense, Concatenate
    
    input_message = Input(shape=(128,))
    x = Dense(64, activation="relu")(input_message)
    x = Dense(32, activation="relu")(x)
    x1 = Dense(4, activation="relu")(x)
    
    input_description = Input(shape=(128,))
    x = Dense(64, activation="relu")(input_description)
    x = Dense(32, activation="relu")(x)
    x2 = Dense(4, activation="relu")(x)
    
    input_errors = Input(shape=(2,))
    x3 = Dense(1, activation="relu")(input_errors)
    
    input_panics = Input(shape=(2,))
    x4 = Dense(1, activation="relu")(input_panics)
    
    input_images = Input(shape=(2,))
    x5 = Dense(1, activation="relu")(input_images)
    
    input_committer = Input(shape=(16,))
    x6 = Dense(4, activation="relu")(input_description)
    
    input_reporter = Input(shape=(6,))
    x7 = Dense(1, activation="relu")(input_reporter)
    
    combined = Concatenate()([x1,x2,x3,x4,x5,x6,x7])
    
    z = Dense(6, activation='softmax')(combined)
    model = Model(inputs=[input_message,input_description,input_errors,input_panics,input_images,input_committer,input_reporter ], 
                  outputs=z)
    

    我还没有运行它,但这似乎对我有用。

    【讨论】:

    • 这么多型号?只有 1 个。由于我有 7 个功能/列,我正在为每个功能/列创建一个输入
    【解决方案2】:

    您可能想查看您拥有的变量名称。通常GraphDisconnected 错误是由程序中的覆盖名称引起的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2019-06-06
      • 2018-08-11
      • 2019-04-07
      • 1970-01-01
      • 2020-05-13
      相关资源
      最近更新 更多