如何解决CapsNet的错误“考虑将元素转换为支持的类型”?

时间:2019-07-18 07:08:08

标签: python deep-learning

我正在尝试了解CapsNet的分类。训练网络时,壁球功能出现错误Consider casting elements to a supported type

我尝试了一个现有代码并打印了每一层的输出,但是无法理解错误原因。

这里是壁球功能和CapsNet体系结构的代码。

def squash(vectors, axis=-1):

    s_squared_norm = K.sum(K.square(vectors), axis, keepdims=True)
    scale = s_squared_norm / (1 + s_squared_norm) /K.sqrt(s_squared_norm + K.epsilon())
    return scale * vectors

def capsnetwork(self, capsnetwork):
    # depending on dataset we define input shape for our network
    img = Input(self.shape_high_reso)

    print("x",img.shape)
    #x (?, 32, 32, 3)

    x = Conv2D(filters=256, kernel_size=9, strides=1, padding='valid', name='conv1')(img)
    print("x",x.shape)
    #x (?, 24, 24, 256)
    x = LeakyReLU()(x)

    # # original 'Dynamic Routing Between Capsules' paper does not include the batch norm layer after the first conv group
    x = BatchNormalization(momentum=0.8)(x)

    # filters 256 (n_vectors=8 * channels=32)
    x = Conv2D(filters=8 * 32, kernel_size=9, strides=2, padding='valid', name='primarycap_conv2')(x)
    print("x",x.shape)
    #x (?, 8, 8, 256)

    # reshape into the 8D vector for all 32 feature maps combined
    # (primary capsule has collections of activations which denote orientation of the digit
    # while intensity of the vector which denotes the presence of the digit)
    x = Reshape(target_shape=[-1, 8], name='primarycap_reshape')(x)
    print("x",x.shape)
    #x (?, ?, 8)
    # the purpose is to output a number between 0 and 1 for each capsule where the length of the input decides the amount
    x = Lambda(self.squash, name='primarycap_squash')(x)

我希望应该对网络进行培训,但是它会在一行输出错误

x = Lambda(self.squash, name='primarycap_squash')(x)

错误是

TypeError: Failed to convert object of type <class 'models.Capsnet.Capsnet'> to Tensor.
Contents: <models.Capsnet.Capsnet object at 0x7fcd234eccc0>.
Consider casting elements to a supported type.

0 个答案:

没有答案