Python Keras.backend.reforme:TypeError:转换类型为<;的对象失败;类别';列表'&燃气轮机;到张量。将铸造元素考虑为支持类型
我正在为我的神经网络设计一个自定义层,但是我的代码出错了 我想做一个文章中描述的注意层:。和 当我在模型中添加此层时,出现了一个错误:Python Keras.backend.reforme:TypeError:转换类型为<;的对象失败;类别';列表'&燃气轮机;到张量。将铸造元素考虑为支持类型,python,tensorflow,keras,generative-adversarial-network,Python,Tensorflow,Keras,Generative Adversarial Network,我正在为我的神经网络设计一个自定义层,但是我的代码出错了 我想做一个文章中描述的注意层:。和 当我在模型中添加此层时,出现了一个错误: TypeError: Expected binary or unicode string, got Dimension(None) During handling of the above exception, another exception occurred: TypeError Trac
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.
TypeError:应为二进制或unicode字符串,获取维度(无)
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
在()
5 X=Conv2D(64,内核大小=5,步幅=1,name='conv1')(X)
6 X=激活('relu')(X)
---->7 X=注意层()(X)
8 X=展平(name='flatt2')(X)
9 X=密度(1000,活化='relu')(X)
/anaconda3/envs/pycharm/lib/python3.6/site-packages/keras/engine/topology.py in_u_调用(self,input,**kwargs)
617
618#实际调用层,收集输出、掩码和形状。
-->619输出=自调用(输入,**kwargs)
620输出屏蔽=自计算屏蔽(输入,前一个屏蔽)
621
~/Projects/inpaint/models/attention.py在通话中(self,x)
49
50#N=h*w
--->51展平度=hw展平度(g)
52展平f=hw展平(f)
53 s=K.batch_dot(展平,展平,轴=1)35;#[bs,N,N]
~/Projects/inpaint/models/attention.py在硬件展平(x)中
39 def呼叫(自我,x):
40 def硬件扁平化(x):
--->41返回K.重塑(x,shape=[x.shape[0],x.shape[1]*x.shape[2],x.shape[-1]]
42
43 f=K.conv2d(x,kernel=self.kernel_f,步长=(1,1),padding='same')#[bs,h,w,c']
/anaconda3/envs/pycharm/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py在整形(x,shape)中
1896张量。
1897 """
->1898返回tf。重塑(x,形状)
1899
1900
/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/ops/gen_-array_-ops.py in重塑(张量、形状、名称)
6111如果_ctx为None或not _ctx._eager_context.is eager:
6112 u,u,_op=_op_def_lib._apply_op_helper(
->6113“重塑”,张量=张量,形状=形状,名称=名称)
6114 _结果=_运算输出[:]
6115 _输入_平坦=_操作输入
/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in_apply_op_helper(self,op_type_name,name,**关键字)
511除类型错误作为错误外:
512如果数据类型为无:
-->513上升误差
514其他:
515上升类型错误(
/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in_apply_op_helper(self,op_type_name,name,**关键字)
508数据类型=数据类型,
509 as_ref=输入参数为,
-->510首选类型(默认类型)
511除类型错误作为错误外:
512如果数据类型为无:
/内部的anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/ops.py转换为tensor(值、数据类型、名称、as-ref、首选数据类型、ctx)
1102
1103如果ret为无:
->1104 ret=conversion\u func(值,dtype=dtype,name=name,as\u ref=as\u ref)
1105
1106如果未实施ret:
/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in_constant_tensor_conversion_函数(v,dtype,name,as_ref)
233 as_ref=False):
234=作为参考
-->235返回常量(v,dtype=dtype,name=name)
236
237
/anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(值、数据类型、形状、名称、验证形状)
212 tensor_value.tensor.CopyFrom(
213张量使用。生成张量原型(
-->214值,dtype=dtype,shape=shape,verify_shape=verify_shape))
215 dtype\u value=attr\u value\u pb2.AttrValue(type=tensor\u value.tensor.dtype)
216常数张量=g.create\u op(
/make_tensor_proto中的anaconda3/envs/pycharm/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py(值、数据类型、形状、验证形状)
519 raise TypeError(“未能将%s类型的对象转换为Tensor。”
520“内容:%s考虑铸造元素为”
-->521“支持的类型”。%(类型(值),值))
522张量原始字符串值扩展(str值)
523返回张量
TypeError:将类型对象转换为张量失败。内容:[维度(无),维度(64),维度(8)]。
我试图在hw_flatten函数中设置
x_shape=x.shape.as_list()
,但它不起作用,我不知道如何调试此错误。您正在访问tensor的.shape
属性,该属性为您提供尺寸对象,而不是实际的形状值。您有两个选项:
K.int\u shape(x)[0]
将值作为整数给出。但是,如果形状在创建时未知,例如,如果批次大小未知,它将返回None
K.shape(x)[0]
,它将返回一个符号张量,在运行时保存形状值您正在访问tensor的
.shape
属性,该属性为您提供标注对象,而不是实际的形状值。您有两个选项:
K.int\u shape(x)[0]
将值作为整数给出。但是,如果形状在创建时未知,例如,如果批次大小未知,它将返回None
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.