1. 程式人生 > >TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int

TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int

Problem

    embed2 = TimeDistributed(Encoder1)(input2)
  File "E:\Python\Python36\lib\site-packages\keras\engine\topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "E:\Python\Python36\lib\site-packages\keras\layers\wrappers.py", line 196, in call
    inputs = K.reshape(inputs, (-1,) + input_shape[2:])
  File "E:\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 1776, in reshape
    return tf.reshape(x, shape)
  File "E:\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7546, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "E:\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 609, in _apply_op_helper
    param_name=input_name)
  File "E:\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 60, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64
 

Solution

將x或者x.values轉換為dtype # tensor a is [1.8, 2.2], dtype=tf.float # tf.cast(a, tf.int32) ==> [1, 2],dtype=tf.int32 tf.cast(x, dtype, name=None)

將傳給shape的變數變成int32的==》tf.cast(shape, tf.int32)