TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是开始训练模型时的“元组”

时间:2019-08-09 17:45:06

标签: python tensorflow autoencoder tf.keras

我正在尝试在张量流中构建矢量量化的变分自动编码器。当我建立模型并开始训练时,会发生一个非常神秘的错误。

我已遍历代码以查找错误,但找不到。该错误对查找原因非常不利。下面是模型代码的一部分

  def call(self, inputs):

    z = self.encode(inputs)

    vq_output = self.vector_quantizer(z)

    reconstructed_image = self.decode(vq_output['quantize'])

    return reconstructed_image, vq_output


  def encode(self, image):

    encoded = Conv2D(int(self._num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='enc1')(image)
    encoded = Conv2D(self._num_hidden, kernel_size=(4,4), strides=(2,2), activation='relu', name='enc2')(encoded)
    encoded = Conv2D(self._num_hidden, kernel_size=(3,3), strides=(1,1), name='enc2')(encoded)

    encoded = residual_stack(encoded, self._num_hidden, self._num_residual_layers, self._num_residual_hiddens)
    return Conv2D(self._embeding_dim, kernel_size=(1,1) ,strides=(1,1), name='enc3')(encoded)

  def decode(self, encoded_input):

    decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)

    decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)

    decoded = Conv2DTranspose(int(self._decoder_num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='dec2')(decoded)
    return Conv2DTranspose(3, kernel_size=(4,4), strides=(2,2), name='dec3')(decoded)

发生以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-90a0a1fb38f6> in <module>()
     15 
     16   for batch in train_batches:
---> 17     _loss, _perplexity = train(vq_vae, batch['images'], optimizer)
     18     loss.update_state(_loss)
     19     perplexity.update_state(_perplexity)

14 frames
<ipython-input-45-0025b05fe5c9> in train(model, inputs, optimizer, outputs)
      1 def train(model, inputs, optimizer, outputs=None):
      2   with tf.GradientTape() as tape:
----> 3     loss, perplexity = compute_loss(model, inputs)
      4   gradients = tape.gradient(loss, model.trainable_variables)
      5   optimizer.apply_gradients(zip(gradients, model.trainable_variables))

<ipython-input-44-242959fe043f> in compute_loss(model, inputs, outputs)
      1 def compute_loss(model, inputs, outputs=None):
----> 2   recon, vq_outputs = model(inputs)
      3   recon_loss = tf.reduce_mean((recon - inputs)**2) / data_variance
      4   loss = recon_loss + vq_outputs['loss']
      5   return loss, vq_outputs['perplexity']

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    677           with base_layer_utils.autocast_context_manager(
    678               input_list, self._mixed_precision_policy.should_cast_variables):
--> 679             outputs = self.call(inputs, *args, **kwargs)
    680           self._handle_activity_regularization(inputs, outputs)
    681           self._set_mask_metadata(inputs, outputs, previous_mask)

<ipython-input-43-f28d8aaad600> in call(self, inputs)
     40     vq_output = self.vector_quantizer(z)
     41 
---> 42     reconstructed_image = self.decode(vq_output['quantize'])
     43 
     44     return reconstructed_image, vq_output

<ipython-input-43-f28d8aaad600> in decode(self, encoded_input)
     55 
     56   def decode(self, encoded_input):
---> 57     decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)
     58 
     59     decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    674         # Eager execution on data tensors.
    675         with backend.name_scope(self._name_scope()):
--> 676           self._maybe_build(inputs)
    677           with base_layer_utils.autocast_context_manager(
    678               input_list, self._mixed_precision_policy.should_cast_variables):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   1879       # operations.
   1880       with tf_utils.maybe_init_scope(self):
-> 1881         self.build(input_shapes)
   1882     # We must set self.built since user defined build functions are not
   1883     # constrained to set self.built.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py in build(self, input_shape)
    163         constraint=self.kernel_constraint,
    164         trainable=True,
--> 165         dtype=self.dtype)
    166     if self.use_bias:
    167       self.bias = self.add_weight(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, **kwargs)
    382         collections=collections_arg,
    383         synchronization=synchronization,
--> 384         aggregation=aggregation)
    385     backend.track_variable(variable)
    386 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
    661         dtype=dtype,
    662         initializer=initializer,
--> 663         **kwargs_for_getter)
    664 
    665     # If we set an initializer and the variable processed it, tracking will not

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_utils.py in make_variable(name, shape, dtype, initializer, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner)
    140   # TODO(apassos,rohanj) figure out how to remove collections from here so we
    141   # can remove the V1.
--> 142   variable_shape = tensor_shape.TensorShape(shape)
    143   return tf_variables.VariableV1(
    144       initial_value=init_val,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
    772       else:
    773         # Got a list of dimensions
--> 774         self._dims = [as_dimension(d) for d in dims_iter]
    775 
    776   @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in <listcomp>(.0)
    772       else:
    773         # Got a list of dimensions
--> 774         self._dims = [as_dimension(d) for d in dims_iter]
    775 
    776   @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in as_dimension(value)
    714     return value
    715   else:
--> 716     return Dimension(value)
    717 
    718 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
    183       raise TypeError("Cannot convert %s to Dimension" % value)
    184     else:
--> 185       self._value = int(value)
    186       if (not isinstance(value, compat.bytes_or_text_types) and
    187           self._value != value):

TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

通过查看此错误,我无法理解我在哪里犯了错误。

0 个答案:

没有答案