Keras.backend.reshape:TypeError:无法转换< class'list'>类型的对象.到张量.考虑将元素强制转换为受支持的类型 [英] Keras.backend.reshape: TypeError: Failed to convert object of type <class 'list'> to Tensor. Consider casting elements to a supported type

查看:657
本文介绍了Keras.backend.reshape:TypeError:无法转换< class'list'>类型的对象.到张量.考虑将元素强制转换为受支持的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的神经网络设计一个自定义层,但是我从代码中得到了一个错误.

I'm designing a custom layer for my neural network, but I get an error from my code.

我想按照论文中的描述进行关注: SAGAN .而原始tf代码

I want to do a attention layer as described in the paper: SAGAN. And the original tf code

class AttentionLayer(Layer):
def __init__(self, **kwargs):
    super(AttentionLayer, self).__init__(**kwargs)

def build(self, input_shape):
    input_dim = input_shape[-1]
    filters_f_g = input_dim // 8
    filters_h = input_dim
    kernel_shape_f_g = (1, 1) + (input_dim, filters_f_g)
    kernel_shape_h = (1, 1) + (input_dim, filters_h)
    # Create a trainable weight variable for this layer:
    self.gamma = self.add_weight(name='gamma', shape=[1], initializer='zeros', trainable=True)
    self.kernel_f = self.add_weight(shape=kernel_shape_f_g,
                                    initializer='glorot_uniform',
                                    name='kernel')
    self.kernel_g = self.add_weight(shape=kernel_shape_f_g,
                                    initializer='glorot_uniform',
                                    name='kernel')
    self.kernel_h = self.add_weight(shape=kernel_shape_h,
                                    initializer='glorot_uniform',
                                    name='kernel')
    self.bias_f = self.add_weight(shape=(filters_f_g,),
                                  initializer='zeros',
                                  name='bias')
    self.bias_g = self.add_weight(shape=(filters_f_g,),
                                  initializer='zeros',
                                  name='bias')
    self.bias_h = self.add_weight(shape=(filters_h,),
                                  initializer='zeros',
                                  name='bias')
    super(AttentionLayer, self).build(input_shape)

def call(self, x):
    def hw_flatten(x):
        return K.reshape(x, shape=[x.shape[0], x.shape[1]*x.shape[2], x.shape[-1]])

    f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1), padding='same')  # [bs, h, w, c']
    f = K.bias_add(f, self.bias_f)
    g = K.conv2d(x, kernel=self.kernel_g, strides=(1, 1), padding='same')  # [bs, h, w, c']
    g = K.bias_add(g, self.bias_g)
    h = K.conv2d(x, kernel=self.kernel_h, strides=(1, 1), padding='same')  # [bs, h, w, c]
    h = K.bias_add(h, self.bias_h)

    # N = h * w
    flatten_g = hw_flatten(g)
    flatten_f = hw_flatten(f)
    s = K.batch_dot(flatten_g, flatten_f, axes=1)  # # [bs, N, N]

    beta = K.softmax(s, axis=-1)  # attention map

    o = K.batch_dot(beta, hw_flatten(h))  # [bs, N, C]

    o = K.reshape(o, shape=x.shape)  # [bs, h, w, C]
    x = self.gamma * o + x

    return x

当我在模型中添加此层时,出现错误:

When I added this layer in my model, I get an error:

TypeError: Expected binary or unicode string, got Dimension(None)

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-9d4e83945ade> in <module>()
      5 X = Conv2D(64, kernel_size=5, strides=1, name='conv1')(X)
      6 X = Activation('relu')(X)
----> 7 X = AttentionLayer()(X)
      8 X = Flatten(name='flatten2')(X)
      9 X = Dense(1000, activation='relu')(X)

/anaconda3/envs/pycharm/lib/python3.6/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    617 
    618             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619             output = self.call(inputs, **kwargs)
    620             output_mask = self.compute_mask(inputs, previous_mask)
    621 

~/Projects/inpainting/models/attention.py in call(self, x)
     49 
     50         # N = h * w
---> 51         flatten_g = hw_flatten(g)
     52         flatten_f = hw_flatten(f)
     53         s = K.batch_dot(flatten_g, flatten_f, axes=1)  # # [bs, N, N]

~/Projects/inpainting/models/attention.py in hw_flatten(x)
     39     def call(self, x):
     40         def hw_flatten(x):
---> 41             return K.reshape(x, shape=[x.shape[0], x.shape[1]*x.shape[2], x.shape[-1]])
     42 
     43         f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1), padding='same')  # [bs, h, w, c']

/anaconda3/envs/pycharm/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in reshape(x, shape)
   1896         A tensor.
   1897     """
-> 1898     return tf.reshape(x, shape)
   1899 
   1900 

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in reshape(tensor, shape, name)
   6111   if _ctx is None or not _ctx._eager_context.is_eager:
   6112     _, _, _op = _op_def_lib._apply_op_helper(
-> 6113         "Reshape", tensor=tensor, shape=shape, name=name)
   6114     _result = _op.outputs[:]
   6115     _inputs_flat = _op.inputs

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    511           except TypeError as err:
    512             if dtype is None:
--> 513               raise err
    514             else:
    515               raise TypeError(

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    508                 dtype=dtype,
    509                 as_ref=input_arg.is_ref,
--> 510                 preferred_dtype=default_dtype)
    511           except TypeError as err:
    512             if dtype is None:

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
   1102 
   1103     if ret is None:
-> 1104       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1105 
   1106     if ret is NotImplemented:

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    233                                          as_ref=False):
    234   _ = as_ref
--> 235   return constant(v, dtype=dtype, name=name)
    236 
    237 

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
    212   tensor_value.tensor.CopyFrom(
    213       tensor_util.make_tensor_proto(
--> 214           value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    215   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    216   const_tensor = g.create_op(

/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
    519       raise TypeError("Failed to convert object of type %s to Tensor. "
    520                       "Contents: %s. Consider casting elements to a "
--> 521                       "supported type." % (type(values), values))
    522     tensor_proto.string_val.extend(str_values)
    523     return tensor_proto

TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [Dimension(None), Dimension(64), Dimension(8)]. Consider casting elements to a supported type.

我试图在hw_flatten函数中创建x_shape = x.shape.as_list(),但是它不起作用,我不知道如何调试此错误.

I tried to make x_shape = x.shape.as_list() in the hw_flatten function, but it doesn't works, I have no idea how to debug this error.

推荐答案

您正在访问张量的.shape属性,该属性为您提供Dimension对象,而不是实际的形状值.您有2个选择:

You are accessing the tensor's .shape property which gives you Dimension objects and not actually the shape values. You have 2 options:

  1. 如果您知道形状并且在创建图层时已将其固定,则可以使用K.int_shape(x)[0],该值将为整数.但是,如果在创建时形状未知,它将返回None;否则,将返回None.例如,如果batch_size不明.
  2. 如果将在运行时确定形状,则可以使用K.shape(x)[0],它将返回一个符号张量,该张量将在运行时保存形状值.
  1. If you know the shape and it's fixed at layer creation time you can use K.int_shape(x)[0] which will give the value as an integer. It will however return None if the shape is unknown at creation time; for example if the batch_size is unknown.
  2. If shape will be determined at runtime then you can use K.shape(x)[0] which will return a symbolic tensor that will hold the shape value at runtime.

这篇关于Keras.backend.reshape:TypeError:无法转换&lt; class'list'&gt;类型的对象.到张量.考虑将元素强制转换为受支持的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆