TF/keras子类在急切的执行中工作得很好,如果没有它,会抛出大量无法追踪的错误?

2024-04-20 12:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我从keras.io学会了编写自定义层。在这里:

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

    def build(self, input_shape):
        print(input_shape)
        self.b = self.add_weight(name='brad', shape=(input_shape[0][1],), initializer='uniform',
                                      trainable=True)
        super(modrelu, self).build(input_shape)  # Be sure to call this at the end

    def call(self, x):
        assert isinstance(x, list)
        ip_r, ip_i = x
        comp= tf.complex(ip_r, ip_i) 
        ABS= tf.math.abs(comp)
        ANG= tf.math.angle(comp)

        ABS= tf.nn.relu( self.b + ABS) 

        op_i=  ABS * tf.sin(ANG) #K.dot ??
        op_r= ABS * tf.cos(ANG)
        return [op_r, op_i]

    def compute_output_shape(self, input_shape):
        assert isinstance(input_shape, list)
        shape_a, shape_b = input_shape
        return [shape_a, shape_b]


act= modrelu()
a=tf.constant(np.array([[1,2], [4,4]]), dtype='float32')
b=tf.constant(np.array([[3,4], [5, -1]]), dtype='float32')
act([a,b])

当运行在急切的执行,我得到很好的输出。如果没有这种模式,我会得到非常奇怪的错误,我甚至无法追溯到它的起源,它是在一个不同的世界。在这里:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape) 526 try: --> 527 str_values = [compat.as_bytes(x) for x in proto_values] 528 except TypeError:

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\tensor_util.py in (.0) 526 try: --> 527 str_values = [compat.as_bytes(x) for x in proto_values] 528 except TypeError:

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\util\compat.py in as_bytes(bytes_or_text, encoding) 60 raise TypeError('Expected binary or unicode string, got %r' % ---> 61 (bytes_or_text,)) 62

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

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last) in 2 a=tf.constant(np.array([[1,2], [4,4]]), dtype='float32') 3 b=tf.constant(np.array([[3,4], [5, -1]]), dtype='float32') ----> 4 act([a,b])

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in call(self, inputs, *args, **kwargs) 744 # the user has manually overwritten the build method do we need to 745 # build it. --> 746 self.build(input_shapes) 747 # We must set self.built since user defined build functions are not 748 # constrained to set self.built.

in build(self, input_shape) 7 print(input_shape) 8 self.b = self.add_weight(name='brad', shape=(input_shape[0][1],), initializer='uniform', ----> 9 trainable=True) 10 # self.b= K.variable(value=np.random.rand(input_shape[0][1])-0.5, dtype='float32') # 11 super(modrelu, self).build(input_shape) # Be sure to call this at the end

~\AppData\Local\conda\conda\envs\py36\lib\site-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) 607 collections=collections, 608 synchronization=synchronization, --> 609 aggregation=aggregation) 610 backend.track_variable(variable) 611

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\training\checkpointable\base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter) 637 new_variable = getter( 638 name=name, shape=shape, dtype=dtype, initializer=initializer, --> 639 **kwargs_for_getter) 640 641 # If we set an initializer and the variable processed it, tracking will not

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in make_variable(name, shape, dtype, initializer, partition_info, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner) 1975
collections=collections, 1976
synchronization=synchronization, -> 1977 aggregation=aggregation) 1978 return v 1979

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\variables.py in call(cls, *args, **kwargs) 181 def call(cls, *args, **kwargs): 182 if cls is VariableV1: --> 183 return cls._variable_v1_call(*args, **kwargs) 184 elif cls is Variable: 185 return cls._variable_v2_call(*args, **kwargs)

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\variables.py in _variable_v1_call(cls, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint, use_resource, synchronization, aggregation) 144 use_resource=use_resource, 145 synchronization=synchronization, --> 146 aggregation=aggregation) 147 148 def _variable_v2_call(cls,

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\variables.py in (**kwargs) 123 aggregation=VariableAggregation.NONE): 124 """Call on Variable class. Useful to force the signature.""" --> 125 previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs) 126 for getter in ops.get_default_graph()._variable_creator_stack: # pylint: disable=protected-access 127 previous_getter = _make_getter(getter, previous_getter)

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\variable_scope.py in default_variable_creator(next_creator, **kwargs) 2435
caching_device=caching_device, name=name, dtype=dtype, 2436
constraint=constraint, variable_def=variable_def, -> 2437 import_scope=import_scope) 2438 else: 2439 return variables.RefVariable(

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\variables.py in call(cls, *args, **kwargs) 185 return cls._variable_v2_call(*args, **kwargs) 186 else: --> 187 return super(VariableMetaclass, cls).call(*args, **kwargs) 188 189

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in init(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint) 295 name=name, 296 dtype=dtype, --> 297 constraint=constraint) 298 299 # pylint: disable=unused-argument

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, constraint) 407 with ops.name_scope("Initializer"), ops.device(None): 408 initial_value = ops.convert_to_tensor( --> 409 initial_value() if init_from_fn else initial_value, 410 name="initial_value", dtype=dtype) 411 self._handle = eager_safe_variable_handle(

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in () 1957 initializer = initializer(dtype=dtype)
1958 init_val = lambda: initializer( # pylint: disable=g-long-lambda -> 1959 shape, dtype=dtype, partition_info=partition_info) 1960 variable_dtype = dtype.base_dtype 1961 if use_resource is None:

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\init_ops.py in call(self, shape, dtype, partition_info) 253 dtype = self.dtype 254 return random_ops.random_uniform( --> 255 shape, self.minval, self.maxval, dtype, seed=self.seed) 256 257 def get_config(self):

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\random_ops.py in random_uniform(shape, minval, maxval, dtype, seed, name) 233 maxval = 1 234 with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name: --> 235 shape = _ShapeTensor(shape) 236 minval = ops.convert_to_tensor(minval, dtype=dtype, name="min") 237 maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\ops\random_ops.py in _ShapeTensor(shape) 42 else: 43 dtype = None ---> 44 return ops.convert_to_tensor(shape, dtype=dtype, name="shape") 45 46

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype) 1048
name=name, 1049 preferred_dtype=preferred_dtype, -> 1050 as_ref=False) 1051 1052

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx) 1144 1145 if ret is None: -> 1146 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 1147 1148 if ret is NotImplemented:

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref) 227 as_ref=False): 228 _ = as_ref --> 229 return constant(v, dtype=dtype, name=name) 230 231

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name, verify_shape) 206 tensor_value.tensor.CopyFrom( 207 tensor_util.make_tensor_proto( --> 208 value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 209 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype) 210 const_tensor = g.create_op(

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape) 529 raise TypeError("Failed to convert object of type %s to Tensor. " 530 "Contents: %s. Consider casting elements to a " --> 531 "supported type." % (type(values), values)) 532 tensor_proto.string_val.extend(str_values) 533 return tensor_proto

TypeError: Failed to convert object of type to Tensor. Contents: (Dimension(2),). Consider casting elements to a supported type.


Tags: nameinselflibpackageslocalsitevariable
1条回答
网友
1楼 · 发布于 2024-04-20 12:24:14

您将获得作为TensorShape实例的形状。如果执行不急,可以使用as_list()属性将其转换为Python列表:

if tf.executing_eagerly():
    shape = (input_shape[0][1], )
else:
    shape = (input_shape[0].as_list()[1], )

在您的代码中将是这样的:

import tensorflow as tf
from tensorflow.keras.layers import Layer
import numpy as np

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

    def build(self, input_shape):
        if tf.executing_eagerly():
            shape = (input_shape[0][1])
        else:
            shape = (input_shape[0].as_list()[1], )
        self.b = self.add_weight(name='brad',
                                 shape=shape,
                                 initializer='uniform',
                                 trainable=True)
        super(modrelu, self).build(input_shape)  # Be sure to call this at the end

    def call(self, x):
        assert isinstance(x, list)
        ip_r, ip_i = x
        comp = tf.complex(ip_r, ip_i) 
        ABS = tf.math.abs(comp)
        ANG = tf.math.angle(comp)
        ABS = tf.nn.relu( self.b + ABS) 
        op_i =  ABS * tf.sin(ANG) #K.dot ??
        op_r = ABS * tf.cos(ANG)
        return [op_r, op_i]

    def compute_output_shape(self, input_shape):
        assert isinstance(input_shape, list)
        shape_a, shape_b = input_shape
        return [shape_a, shape_b]

act = modrelu()
a = tf.constant(np.array([[1,2], [4,4]]), dtype='float32')
b = tf.constant(np.array([[3,4], [5, -1]]), dtype='float32')
act([a,b])
# [<tf.Tensor 'modrelu_6/mul_1:0' shape=(2, 2) dtype=float32>,
#  <tf.Tensor 'modrelu_6/mul:0' shape=(2, 2) dtype=float32>]

相关问题 更多 >