如何创建具有随机形状值的 tf.Variable [None,1,512,1]

原文标题How to create a tf.Variable with random values of shape [None,1,512,1]

TypeError: Failed to convert elements of (None, 512, 1) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

在此处输入图像描述

完全错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-7e65d3842564> in <module>()
      3 print(x1)
      4 a=IPC(filters=512,alpha=0.875,k=9)
----> 5 outputs=a(x1)
      6 #a.train_step(x1,outputs)
      7 #print(f'output{a(x1).shape}')

5 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer_v1.py in __call__(self, *args, **kwargs)
    743           # Build layer if applicable (if the `build` method has been
    744           # overridden).
--> 745           self._maybe_build(inputs)
    746           cast_inputs = self._maybe_cast_inputs(inputs)
    747 

/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer_v1.py in _maybe_build(self, inputs)
   2072         # operations.
   2073         with tf_utils.maybe_init_scope(self):
-> 2074           self.build(input_shapes)
   2075       # We must set also ensure that the layer is marked as built, and the build
   2076       # shape is stored since user defined build functions may not be calling

<ipython-input-13-74cd8b1454f0> in build(self, inputs)
     28 
     29         seed_init=tf.random_uniform_initializer(minval=-1, maxval=0)
---> 30         seed_init_val=seed_init(shape=(None,self.input_shap[-1],1),dtype='float32')
     31         #seed_init_val=
     32         #initializer = RandomUniform(-1, 1)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/init_ops.py in __call__(self, shape, dtype, partition_info)
    473       dtype = self.dtype
    474     return random_ops.random_uniform(
--> 475         shape, self.minval, self.maxval, dtype, seed=self.seed)
    476 
    477   def get_config(self):

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
    151     except Exception as e:
    152       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153       raise e.with_traceback(filtered_tb) from None
    154     finally:
    155       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    543       str_values = [compat.as_bytes(x) for x in proto_values]
    544     except TypeError:
--> 545       raise TypeError(f"Failed to convert elements of {values} to Tensor. "
    546                       "Consider casting elements to a supported type. See "
    547                       "https://www.tensorflow.org/api_docs/python/tf/dtypes "

TypeError: Failed to convert elements of (None, 512, 1) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

导致错误的代码:

seed_init=tf.random_uniform_initializer(minval=-1, maxval=0)
seed_init_val=seed_init(shape=(None,self.input_shap[-1],1),dtype='float32')
self.seed=tf.Variable(initial_value=seed_init_val,trainable=True,validate_shape=False)

原文链接:https://stackoverflow.com//questions/71991364/how-to-create-a-tf-variable-with-random-values-of-shape-none-1-512-1

回复

我来回复
  • Pritam Dodeja的头像
    Pritam Dodeja 评论

    一般当你看到None的时候,它指的是batch轴,所以这里我假设你要生成一个形状为(1, 512, 1)的随机值的张量,然后添加一个batch轴。您可以按如下方式执行此操作:

    weights = tf.Variable(tf.expand_dims(tf.random.uniform(shape=(1, 512, 1)), axis=0)) 
    

    从最里面到最外面,这就是正在发生的事情:

    具有随机值的张量由 tf.random.uniform 创建。
    批处理轴被添加到该张量。
    weights 是一个可变张量,由形状 (1, 1, 512, 1) 构成。

    希望这能让您更接近解决方案。

    1年前 0条评论