Graph ValueError:Keras2中的图形已断开连接

Graph ValueError:Keras2中的图形已断开连接,graph,model,tensorflow2.0,keras-2,Graph,Model,Tensorflow2.0,Keras 2,我得到这个错误,但不知道为什么 下面是我的代码。我怎样才能修好它 def f_stocks_embed_module(cat_input_embed_dim, batch_size): ''' Returning a Model that can be used as a Layer within the broader Keras Model. ''' X = Input(shape = (1,), batch_size = batc

我得到这个错误,但不知道为什么

下面是我的代码。我怎样才能修好它

def f_stocks_embed_module(cat_input_embed_dim, batch_size):

    '''

     Returning a Model that can be used as a Layer within the broader Keras Model.

    '''
    
    
    X = Input(shape = (1,), batch_size = batch_size)
    
    cat_input = Input(shape = (1, ), batch_size = batch_size)
    
    cat_input_add = Embedding(input_dim = cat_input_embed_dim, output_dim = 1)(cat_input)
    
    cat_input_mult = Embedding(input_dim = cat_input_embed_dim, output_dim = 1)(cat_input)
    
    cat_input_add = Flatten()(cat_input_add)
    
    cat_input_mult = Flatten()(cat_input_mult)
    
    x = Multiply()([X, cat_input_mult])
    
    x = Add()([x, cat_input_add])
    
    x = Dense(1)(x)
    
    x = Add()([X, x])
    
    model = Model(inputs = [X, cat_input], outputs = x)
    
    return(model)

color_embed_dim = 7
clarity_embed_dim = 8
batch_size = 20

dense1 = 2**7
dense2 = 2**8
dense3 = 2**9
dropout = 0.8
price_loss = 1
cut_loss = 1
activation= LeakyReLU()
batch_size = 20
threshold = 0.7
#====================================================================

# INPUTS

#====================================================================


#----------------------------------------------------------------

carat = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'carat'
)

#----------------------------------------------------------------

Color = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'color'
)

#----------------------------------------------------------------

Clarity = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'clarity'
)

#----------------------------------------------------------------

depth = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'depth'
)

#----------------------------------------------------------------

table = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'table'
)

#----------------------------------------------------------------

X = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'x'
)

#----------------------------------------------------------------

y = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'y'
)

#----------------------------------------------------------------

z = Input(
    shape= (1,),
    batch_size= batch_size,
    name= 'z'
)

#----------------------------------------------------------------

#====================================================================

# CONCATENATE FEATURES

#====================================================================


Y = Concatenate()([carat, depth, table, X, y, z])



#====================================================================

# DENSE NETWORK FOR BOTH PRICE AND CUT

#====================================================================

Y = Dense(dense1, activation = activation)(Y)

Y = BatchNormalization()(Y)

Y = Dense(dense2, activation = activation)(Y)

Y = BatchNormalization()(Y)


#====================================================================

# DENSE NETWORK TO PREDICT CUT

#====================================================================

x = Dense(dense3, activation = activation)(Y)

x = BatchNormalization()(x)

x = Dropout(dropout)(x)

#====================================================================

# PREDICTING CUT USING THE EMBEDDINGS AND SKIP CONNECTIONS

# ====================================================================

x = Dense(1)(x)





#-------------------------------------------------------------

# THE EFFECT OF COLOR ON CUT

#-------------------------------------------------------------

model_embed_color_cut =  f_stocks_embed_module(color_embed_dim, batch_size)

model_embed_clarity_cut =  f_stocks_embed_module(clarity_embed_dim, batch_size)


x = model_embed_color_cut([x, Color])

# At this point the problem appears.  Although my code is longer, for simplicity I cut it here and create a Model.

model = Model([carat, depth, table, X, y, z], x)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-182-acd9c88f235b> in <module>
    149 
    150 
--> 151 model = Model([carat, depth, table, X, y, z], x)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py in __new__(cls, *args, **kwargs)
    240       # Functional model
    241       from tensorflow.python.keras.engine import functional  # pylint: disable=g-import-not-at-top
--> 242       return functional.Functional(*args, **kwargs)
    243     else:
    244       return super(Model, cls).__new__(cls, *args, **kwargs)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\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

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\functional.py in __init__(self, inputs, outputs, name, trainable)
    113     #     'arguments during initialization. Got an unexpected argument:')
    114     super(Functional, self).__init__(name=name, trainable=trainable)
--> 115     self._init_graph_network(inputs, outputs)
    116 
    117   @trackable.no_automatic_dependency_tracking

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\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

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\functional.py in _init_graph_network(self, inputs, outputs)
    189     # Keep track of the network's nodes and layers.
    190     nodes, nodes_by_depth, layers, _ = _map_graph_network(
--> 191         self.inputs, self.outputs)
    192     self._network_nodes = nodes
    193     self._nodes_by_depth = nodes_by_depth

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\functional.py in _map_graph_network(inputs, outputs)
    929                              'The following previous layers '
    930                              'were accessed without issue: ' +
--> 931                              str(layers_with_complete_input))
    932         for x in nest.flatten(node.outputs):
    933           computable_tensors.add(id(x))

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("color_9:0", shape=(20, 1), dtype=float32) at layer "functional_29". The following previous layers were accessed without issue: ['concatenate_9', 'dense_34', 'batch_normalization_16', 'dense_35', 'batch_normalization_17', 'dense_36', 'batch_normalization_18', 'dropout_6', 'dense_37']
def f_库存嵌入模块(cat_输入嵌入尺寸、批次尺寸):
'''
返回可以用作更广泛的Keras模型中的层的模型。
'''
X=输入(形状=(1,),批次大小=批次大小)
类别输入=输入(形状=(1,),批次大小=批次大小)
cat\u输入添加=嵌入(输入尺寸=cat\u输入尺寸,输出尺寸=1)(cat\u输入)
cat\u input\u mult=嵌入(输入尺寸=cat\u input\u嵌入尺寸,输出尺寸=1)(cat\u输入)
cat_输入_添加=展平()(cat_输入_添加)
cat\u input\u mult=flatte()(cat\u input\u mult)
x=Multiply()([x,cat\u input\u mult])
x=添加()([x,类别输入添加])
x=密度(1)(x)
x=Add()([x,x])
模型=模型(输入=[X,cat_输入],输出=X)
退货(型号)
颜色嵌入尺寸=7
清晰度\嵌入\尺寸=8
批量大小=20
密度1=2**7
密度2=2**8
密度3=2**9
辍学率=0.8
价格损失=1
切割损耗=1
激活=LeakyReLU()
批量大小=20
阈值=0.7
#====================================================================
#投入
#====================================================================
#----------------------------------------------------------------
克拉=输入(
形状=(1,),
批次大小=批次大小,
名称='克拉'
)
#----------------------------------------------------------------
颜色=输入(
形状=(1,),
批次大小=批次大小,
名称='color'
)
#----------------------------------------------------------------
清晰度=输入(
形状=(1,),
批次大小=批次大小,
名称=‘清晰度’
)
#----------------------------------------------------------------
深度=输入(
形状=(1,),
批次大小=批次大小,
名称='深度'
)
#----------------------------------------------------------------
表=输入(
形状=(1,),
批次大小=批次大小,
名称='表'
)
#----------------------------------------------------------------
X=输入(
形状=(1,),
批次大小=批次大小,
名称='x'
)
#----------------------------------------------------------------
y=输入(
形状=(1,),
批次大小=批次大小,
name='y'
)
#----------------------------------------------------------------
z=输入(
形状=(1,),
批次大小=批次大小,
name='z'
)
#----------------------------------------------------------------
#====================================================================
#连接特征
#====================================================================
Y=连接()([K,深度,表,X,Y,z])
#====================================================================
#密集网络,价格和价格都可以降低
#====================================================================
Y=致密(密度1,活化=活化)(Y)
Y=BatchNormalization()(Y)
Y=致密(密度2,活化=活化)(Y)
Y=BatchNormalization()(Y)
#====================================================================
#密集网络预测切割
#====================================================================
x=致密(密度3,活化=活化)(Y)
x=BatchNormalization()(x)
x=辍学(辍学)(x)
#====================================================================
#使用嵌入和跳过连接预测切割
# ====================================================================
x=密度(1)(x)
#-------------------------------------------------------------
#颜色对切割的影响
#-------------------------------------------------------------
模型嵌入颜色切割=库存嵌入模块(颜色嵌入尺寸、批量大小)
模型嵌入清晰切割=库存嵌入模块(清晰嵌入尺寸、批量大小)
x=模型\嵌入\颜色\剪切([x,颜色])
#此时问题出现了。虽然我的代码较长,但为了简单起见,我在这里将其剪切并创建了一个模型。
模型=模型([克拉,深度,表,X,y,z],X)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
149
150
-->151型号=型号([克拉,深度,台面,X,y,z],X)
~\AppData\Roaming\Python\Python37\site packages\tensorflow\Python\keras\engine\training.py in\uuuuuuuu new\uuuuuu(cls、*args、**kwargs)
240#功能模型
241来自tensorflow.python.keras.engine import functional#pylint:disable=g-import-not-at-top
-->242返回functional.functional(*args,**kwargs)
243其他:
244返回超级(型号,cls)。\uuuuu新的\uuuuuu(cls,*args,**kwargs)
~\AppData\Roaming\Python\Python37\site packages\tensorflow\Python\training\tracking\base.py in\u method\u包装(self,*args,**kwargs)
455 self._self_setattr_tracking=False#pylint:disable=protected access
456试试:
-->457结果=方法(自身、*args、**kwargs)
458最后:
459 self._self_setattr_tracking=上一个值#pylint:disable=受保护访问
~\AppData\Roaming\Python\Python37\site packages\tensorflow\Python\keras\engine\functional.py in\uuuuuuu init\uuuuuu(self、输入、输出、名称、可培训)
初始化期间的113个#参数。获取了一个意外参数:')
114超级(功能,自我)。\uuuuu初始(名称=名称,可培训=可培训)
-->115自初始化图网络(输入、输出)
116
117@trackable.no\自动\依赖\跟踪
~\AppData\Roaming\Python\Python37\site packages\tensorflow\Python\training\tracking\base.py in\u method\u包装(self,*args,**kwargs)
455 self._self_setattr_tracking=False#pylint:disable=protected access
456试试:
-->457结果=方法(自身、*args、**kwargs)
458最后:
459 self._self_setattr_tracking=上一个值#pylint:disable=受保护访问
~\AppData\Roaming\Python\Python37\site packages\tensorflow\Python\keras\engine\function