Vah*_*hid 1 python keras tensorflow
我是 Tensorflow 和 Keras 的新手。我试图在 Keras 中运行代码。我有标签可以转换为一种深度为 2 的热编码,然后将其转换为 Keras 层格式。现在当我朗姆酒我的代码时,我收到了这个错误:
回溯(最近一次通话):文件“/Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py”,第 224 行,在 CapsNet((None,28,28,1),2,3,trainImg, trainLbl) 文件“/Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py”,第 164 行,在 CapsNet x_recon = k.layers.Dense(512, activation='relu')(masked) 文件“/Users /vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py”,第 443 行,调用 previous_mask = _collect_previous_mask(inputs) 文件“/Users/vahid/TensorProject/lib/python3.6/ site-packages/keras/engine/base_layer.py”,第 1311 行,在 _collect_previous_mask mask = node.output_masks[tensor_index] AttributeError: 'Node' 对象没有属性 '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])
Run Code Online (Sandbox Code Playgroud)
面具代码:
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
Run Code Online (Sandbox Code Playgroud)
好吧,我找到了解决这个问题的方法。实际上,我们可以在代码中使用两种 Keras。Keras 包或仅使用 tf.keras。在这段代码中,我在使用“密集”时使用了 Keras 包。例如, x_recon = k.layers.Dense(512, activation='relu')(masked)似乎 tf.keras 和 Keras 有不同的来源,当我更改k.layers为tf.keras问题时,问题已解决。
| 归档时间: |
|
| 查看次数: |
3858 次 |
| 最近记录: |