Keras中的noise_shape丢失的TypeError

时间:2018-03-15 17:42:13

标签: python tensorflow keras

我正在设计一个LSTM自动编码器。您可能会看到下面的编码层是一个LSTM架构,一旦我有一个嵌入式矢量,我将使用DNN层对其进行解码。在解码嵌入式矢量之前,我想使用一个丢失层来防止过度拟合。我按照Keras文档中的说明操作,但收到了TypeError: Failed to convert object of type <type 'tuple'> to Tensor. Contents: (None, 1, 128). Consider casting elements to a supported type.

def LSTMautoencoder_with_node_features(dataset, adj, feats):
    aug_adj = sp.hstack([adj, feats])
    input_dim = aug_adj.shape[1]
    timesteps = 8
    latent_dim = 128
    data = Input(shape=(timesteps, input_dim))

    encoded = LSTM(latent_dim,
               name='encoded1',
               dropout=0.5,
               recurrent_dropout=0.5,
               return_sequences=True)(data)
    encoder = Model([data], encoded)
    #### The error happens here. ####
    decoded = Dropout(0.5, noise_shape=(None, 1, latent_dim))(encoded)
    ################################
    decoded = TimeDistributed(Dense(input_dim))(encoded)
    ...

错误消息发布在下面:

In [5]: encoder, ae = LSTMautoencoder_with_node_features(dataset, adj, feats)
   ...: 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-84133d8a0b2d> in <module>()
----> 1 encoder, ae = LSTMautoencoder_with_node_features(dataset, adj, feats)

    <ipython-input-1-ba7fb4525efe> in LSTMautoencoder_with_node_features(dataset, adj, feats)
    286     encoder = Model([data], encoded)
    287 
--> 288     decoded = Dropout(0.5, noise_shape=(None, 1, latent_dim))(encoded)
    289     decoded = TimeDistributed(Dense(input_dim))(encoded)
    290 

/home/qy/anaconda2/lib/python2.7/sitepackages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
    594 
    595             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 596             output = self.call(inputs, **kwargs)
    597             output_mask =self.compute_mask(inputs,previous_mask)
    598 

/home/qy/anaconda2/lib/python2.7/site-packages/keras/layers/core.pyc in call(self, inputs, training)
    109                                  seed=self.seed)
    110             return K.in_train_phase(dropped_inputs, inputs,
--> 111                                     training=training)
    112         return inputs
    113 

/home/qy/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in in_train_phase(x, alt, training)
   2618 
   2619     # else: assume learning phase is a placeholder tensor.
-> 2620     x = switch(training, x, alt)
   2621     if uses_learning_phase:
   2622         x._uses_learning_phase = True

/home/qy/anaconda2/lib/python2.7/sitepackages/keras/backend/tensorflow_backend.pyc in switch(condition, then_expression, else_expression)
   2577     x = tf.cond(condition,
   2578                 then_expression_fn,
-> 2579                 else_expression_fn)
   2580     return x
   2581 
/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/util/deprecation.pyc in new_func(*args, **kwargs)
   287             'in a future version' if date is None else ('after %s' % date),
   288             instructions)
--> 289       return func(*args, **kwargs)
   290     return tf_decorator.make_decorator(func, new_func, 'deprecated',
   291                                       _add_deprecated_arg_notice_to_docstring(

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/ops/control_flow_ops.pyc in cond(pred, true_fn, false_fn, strict, name, fn1, fn2)
   1812     context_t = CondContext(pred, pivot_1, branch=1)
   1813     context_t.Enter()
-> 1814     orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
   1815     if orig_res_t is None:
   1816       raise ValueError("true_fn must have a return value.")
/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/ops/control_fl$w_ops.pyc in BuildCondBranch(self, fn)
   1687   def BuildCondBranch(self, fn):
   1688     """Add the subgraph defined by fn() to the graph."""
-> 1689     original_result = fn()
   1690     if original_result is None:
   1691       return None, None

/home/qy/anaconda2/lib/python2.7/site-packages/keras/layers/core.pyc in dropped$inputs()
   107             def dropped_inputs():
   108                 return K.dropout(inputs, self.rate, noise_shape,
--> 109                                  seed=self.seed)
   110             return K.in_train_phase(dropped_inputs, inputs,
   111                                     training=training)

/home/qy/anaconda2/lib/python2.7/sitepackages/keras/backend/tensorflow_backend$pyc in dropout(x, level, noise_shape, seed)
   2878     # the dummy 1. works around a TF bug
   2879     # (float32_ref vs. float32 incompatibility)
-> 2880     return tf.nn.dropout(x * 1., retain_prob, noise_shape, seed=seed)
   2881 
   2882 

 /home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/ops/nn_ops.pyc in dropout(x, keep_prob, noise_shape, seed, name)
   1910     random_tensor += random_ops.random_uniform(noise_shape,
   1911                                                seed=seed,
-> 1912                                                dtype=x.dtype)
   1913     # 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
   1914     binary_tensor = math_ops.floor(random_tensor)

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/ops/random_ops$pyc in random_uniform(shape, minval, maxval, dtype, seed, name)
    225     maxval = 1
    226   with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) $s name:
--> 227     shape = _ShapeTensor(shape)
    228     minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
    229     maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/ops/random_ops$pyc in _ShapeTensor(shape)
    40   else:
    41     dtype = None
---> 42   return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
    43 
    44 

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/framework/ops.$yc in convert_to_tensor(value, dtype, name, preferred_dtype)
    674       name=name,
    675       preferred_dtype=preferred_dtype,
--> 676       as_ref=False)
    677 
    678 

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/framework/ops.pyc in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    739 
    740         if ret is None:
--> 741           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    742 
    743         if ret is NotImplemented:

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    111                                          as_ref=False):
    112   _ = as_ref
--> 113   return constant(v, dtype=dtype, name=name)
    114 
    115 

 /home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
    100   tensor_value = attr_value_pb2.AttrValue()
    101   tensor_value.tensor.CopyFrom(
--> 102       tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    103   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    104   const_tensor = g.create_op(

/home/qy/anaconda2/lib/python2.7/sitepackages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
    460       raise TypeError("Failed to convert object of type %s to Tensor. "
    461                       "Contents: %s. Consider casting elements to a "
--> 462                       "supported type." % (type(values), values))
    463     tensor_proto.string_val.extend(str_values)
    464     return tensor_proto

TypeError: Failed to convert object of type <type 'tuple'> to Tensor. Contents: (None, 1, 128). Consider casting elements to a supported type.

1 个答案:

答案 0 :(得分:0)

辍学噪音形状不能有无尺寸。