python - Keras - 'Node' 对象没有属性 'output_masks'

标签 python tensorflow keras

我是 Tensorflow 和 Keras 的新手。我尝试在 Keras 中运行代码。我的标签可以转换为 2 深度的一种热编码,然后将其转换为 Keras 层格式。 现在,当我朗读我的代码时,我收到此错误:

Traceback (most recent call last): File "/Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py", line 224, in CapsNet((None,28,28,1),2,3,trainImg,trainLbl) File "/Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py", line 164, in CapsNet x_recon = k.layers.Dense(512, activation='relu')(masked) File "/Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py", line 443, in call previous_mask = _collect_previous_mask(inputs) File "/Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1311, in _collect_previous_mask mask = node.output_masks[tensor_index] AttributeError: 'Node' object has no attribute 'output_masks'

以及我运行并出现错误的代码部分:

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

最佳答案

好吧,我知道如何解决这个问题。实际上,我们可以在代码中使用两种 Keras。 Keras 包或仅使用 tf.keras。在此代码中,我在使用“Dense”时使用 Keras 包。例如 x_recon = k.layers.Dense(512, activation='relu')(masked)所以看来 tf.keras 和 Keras 有不同的来源,当我更改 k.layers 时至tf.keras问题已解决。

关于python - Keras - 'Node' 对象没有属性 'output_masks',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53516966/

相关文章:

python - 无法在Python脚本上拖动文件

python - numpy.ma.cov - 与缺失值的成对相关性?

javascript - 本地加载keras模型到tensorflow.js

python - 使用tensorflow和numpy时出现奇怪的数据类型错误

python - 用于 DQN 强化学习的 Keras Tensorboard

python - 如果 Keras Tensorflow 的 `Model.fit` 的目标/输出是 `None` ,会发生什么?

python - 如何从远程计算机获取控制台输出(ssh + python)

python - 简单 python 正则表达式中的可选分组

python - 我如何知道我的神经网络模型是否过拟合(Keras)

python - ValueError : Error when checking target: expected model_2 to have shape (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组