Python Keras—“节点”对象没有“输出”属性

Python Keras—“节点”对象没有“输出”属性,python,tensorflow,keras,Python,Tensorflow,Keras,我是Tensorflow和Keras的新手。我试图在Keras中运行代码。我有一个标签,转换为一个热编码,深度为2,然后转换为Keras层格式。 现在,当我在代码中运行rum时,出现以下错误: 回溯最近一次呼叫上次: 文件/Users/vahid/Documents/Thesis/Code/new_Code/CapsNet_model.py,第224行,在 CapsNetNone,28,28,1,2,3,培训,培训 文件/Users/vahid/Documents/Thesis/Code/new

我是Tensorflow和Keras的新手。我试图在Keras中运行代码。我有一个标签,转换为一个热编码,深度为2,然后转换为Keras层格式。 现在,当我在代码中运行rum时,出现以下错误:

回溯最近一次呼叫上次: 文件/Users/vahid/Documents/Thesis/Code/new_Code/CapsNet_model.py,第224行,在 CapsNetNone,28,28,1,2,3,培训,培训 文件/Users/vahid/Documents/Thesis/Code/new_Code/CapsNet_model.py,第164行,在CapsNet中 x_recon=k.layers.Dense512,激活class='relu'屏蔽 调用中的文件/Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py,第443行 上一个\u掩码=\u收集\u上一个\u掩码 文件/Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base\u layer.py,第1311行,在上一个掩码中 掩码=节点。输出掩码[张量索引] AttributeError:“节点”对象没有“输出\u掩码”属性

我运行的代码中出现错误的部分:

def CapsNet(input_shape, n_class, num_routing,img,lbl):
"""
:param input_shape: (None, width, height, channels)
:param n_class: number of classes
:param num_routing: number of routing iterations
:return: A Keras Model with 2 inputs (image, label) and
         2 outputs (capsule output and reconstruct image)
"""
# Image
#x = k.layers.Input(shape=input_shape,tensor=img)
# ReLU Conv1

images, labels = tf.train.shuffle_batch([img, lbl], batch_size=50, capacity=30, num_threads=1, min_after_dequeue=10)

inp = k.layers.Lambda(lambda x: x)(images)
#inpy = k.layers.Lambda(lambda x: x,output_shape=(n_class,2))(labels)

#conv1 = k.layers.Conv2D(nb_filter= 256,nb_row= 9 , nb_col= 9, subsample =(1,1),activation='relu', name='conv1')(x)
conv1 = k.layers.Conv2D(filters = 256, kernel_size =9 , strides=(1,1),padding='valid', activation='relu', name='conv1')(inp)

# PrimaryCapsules: Conv2D layer with `squash` activation,
# reshape to [None, num_capsule, dim_vector]
primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32,kernel_size=9, strides=2, padding='valid')
#primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32 , subsample =(2,2) )

# DigiCap: Capsule layer. Routing algorithm works here.

digitcaps = DigiCap(num_capsule = n_class, dim_vector = 16, num_routing = num_routing, name='digitcaps')(primarycaps)

# The length of the capsule's output vector
out_caps = Length(name='out_caps')(digitcaps)

# Decoder network.
y = k.layers.Input(shape=(n_class,),tensor=labels)

# The true label is used to extract the corresponding vj
masked = Mask()([digitcaps, y])
x_recon = k.layers.Dense(512, activation='relu')(masked)
x_recon = k.layers.Dense(1024, activation='relu')(x_recon)
x_recon = k.layers.Dense(784, activation='sigmoid')(x_recon)
x_recon = k.layers.Reshape(target_shape=[28, 28, 1], name='out_recon')(x_recon)

# two-input-two-output keras Model
return k.models.Model([inp, y], [out_caps, x_recon])
面罩代码:

class Mask(tf.layers.Layer):
"""
Mask a Tensor with shape=[None, d1, d2] by the max value in axis=1.
Output shape: [None, d2]
"""
def call(self, inputs, **kwargs):
    # use true label to select target capsule, shape=[batch_size, num_capsule]
    if type(inputs) is list:  # true label is provided with shape = [batch_size, n_classes], i.e. one-hot code.
        assert len(inputs) == 2
        inputs, mask = inputs

    else:  # if no true label, mask by the max length of vectors of capsules
        x = inputs
        x = tf.cast(x,tf.float32)
        # Enlarge the range of values in x to make max(new_x)=1 and others < 0
        x = (x - K.max(x, 1, True)) / K.epsilon() + 1
        mask = K.clip(x, 0, 1)  # the max value in x clipped to 1 and other to 0

    # masked inputs, shape = [batch_size, dim_vector]
    inputs_masked = K.batch_dot(inputs, mask, [1, 1])
    return inputs_masked

好吧,我想知道如何解决这个问题。实际上,我们可以在代码中使用两种kera。Keras包或仅使用tf.Keras。在这段代码中,我使用Keras包时使用Dense。例如,在x_recon=k.layers.Dense512中,激活class='relu'被屏蔽,因此似乎tf.keras和keras有不同的来源,当我将k.layers更改为tf.keras时,问题得到了解决。

好的,我找到了解决这个问题的方法。实际上,我们可以在代码中使用两种kera。Keras包或仅使用tf.Keras。在这段代码中,我使用Keras包时使用Dense。例如,在x_recon=k.layers.Dense512中,激活class='relu'被屏蔽,因此似乎tf.keras和keras有不同的来源,当我将k.layers更改为tf.keras时,问题得到了解决。

我们在这里发布一些代码。你能提供面具类的代码吗?@Lescurel我已经添加了。我们正在这里发送一些代码。你能提供Mask类的代码吗?@Lescurel我已经添加了。