Tensorflow模型,“元组”对象没有属性“层”

时间:2019-09-28 13:33:40

标签: python tensorflow keras deep-learning

我正在尝试建立一个暹罗网络模型。整个模型有两个输入。这两个输入将传递到两个孪生网络(换句话说,共享网络)中。它们的输出将在距离层中进行比较,然后连接到输出层。

对于我如何将输入层传递到共享网络,该错误似乎并不满意。这是构建模型的代码

def get_siamese_model(input_shape):
  """
        Model architecture based on the one provided in: http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf
  """
  #2 inputs
  left_input = Input(input_shape)
  right_input = Input(input_shape)

  #currently not convolutional NN
  model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=input_shape),
    tf.keras.layers.Dense(512, activation = tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation = tf.nn.softmax)
  ])

  #encoding layer
  encoded_l = model(left_input)
  encoded_r = model(right_input)

  #distance layer
  L1_layer = Lambda(lambda tensors: K.abs(tensors[0]- tensors[1]))
  L1_distance = L1_layer([encoded_l,encoded_r])

  #prediction layer
  prediction = Dense(1, activation = 'sigmoid', bias_initializer = initialize_bias)(L1_distance)

  #connect the inputs with the outputs
  siamese_net = Model(input=[left_input,right_input], outputs = prediction)

  return siamese_net

model = get_siamese_model((28, 28,1))
model.summary()

这是一堆完整的错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-43-655eb0c236b9> in <module>()
----> 1 model = get_siamese_model((28, 28,1))
      2 model.summary()

10 frames
<ipython-input-42-af8702cd4619> in get_siamese_model(input_shape)
     33   #                 kernel_initializer=initialize_weights,bias_initializer=initialize_bias))
     34   #encoding layer
---> 35   encoded_l = model(left_input)
     36   encoded_r = model(right_input)
     37 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    632                     outputs = base_layer_utils.mark_as_return(outputs, acd)
    633                 else:
--> 634                   outputs = call_fn(inputs, *args, **kwargs)
    635 
    636             except TypeError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py in call(self, inputs, training, mask)
    245       if not self.built:
    246         self._init_graph_network(self.inputs, self.outputs, name=self.name)
--> 247       return super(Sequential, self).call(inputs, training=training, mask=mask)
    248 
    249     outputs = inputs  # handle the corner case where self.layers is empty

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in call(self, inputs, training, mask)
    749                                 ' implement a `call` method.')
    750 
--> 751     return self._run_internal_graph(inputs, training=training, mask=mask)
    752 
    753   def compute_output_shape(self, input_shape):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in _run_internal_graph(self, inputs, training, mask)
    891 
    892           # Compute outputs.
--> 893           output_tensors = layer(computed_tensors, **kwargs)
    894 
    895           # Update tensor_dict.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    661               kwargs.pop('training')
    662             inputs, outputs = self._set_connectivity_metadata_(
--> 663                 inputs, outputs, args, kwargs)
    664           self._handle_activity_regularization(inputs, outputs)
    665           self._set_mask_metadata(inputs, outputs, previous_mask)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
   1706     kwargs.pop('mask', None)  # `mask` should not be serialized.
   1707     self._add_inbound_node(
-> 1708         input_tensors=inputs, output_tensors=outputs, arguments=kwargs)
   1709     return inputs, outputs
   1710 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
   1793     """
   1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
-> 1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,
   1797                                       input_tensors)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in <lambda>(t)
   1792             `call` method of the layer at the call that created the node.
   1793     """
-> 1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
   1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,

AttributeError: 'tuple' object has no attribute 'layer'

一篇可能与我有关的话题可能需要降低我的张量流。我没有尝试过,因为我想使用最新版本。我想知道在构建模型时是否做错了什么。

0 个答案:

没有答案