Python 3.x 使用任何Tensorflow概率分布的层权重自定义权重初始值设定项
我想创建一个自定义TF层,并使用来自TFP的分布初始化层权重。但是我遇到了以下问题。作为参考,我将基初始值设定项类划分为以下子类:Python 3.x 使用任何Tensorflow概率分布的层权重自定义权重初始值设定项,python-3.x,tensorflow,initialization,tf.keras,tensorflow-probability,Python 3.x,Tensorflow,Initialization,Tf.keras,Tensorflow Probability,我想创建一个自定义TF层,并使用来自TFP的分布初始化层权重。但是我遇到了以下问题。作为参考,我将基初始值设定项类划分为以下子类: class RandomInverseGamma(tf.keras.initializers.Initializer): def __init__(self, alpha, beta): #scale = beta #concentration = alpha self.alpha=alpha self.beta=beta
class RandomInverseGamma(tf.keras.initializers.Initializer):
def __init__(self, alpha, beta):
#scale = beta
#concentration = alpha
self.alpha=alpha
self.beta=beta
def __call__(self, shape, dtype=None):
#scale = beta
#concentration = alpha
return tfd.InverseGamma(concentration=self.alpha,scale=self.beta)
def get_config(self):
return {"alpha": self.alpha, "beta": self.beta}
然后,我尝试获取并绘制权重,以查看它们是否近似为逆伽马(我不知道是否有更好的方法,如果有,请让我知道!)
但是我得到了以下错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-a14493da74d7> in <module>()
6 bias_initializer='zeros',
7 activation='relu')
----> 8 y = dense1(input_x)
19 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: TypeError: object of type 'InverseGamma' has no len()
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
6偏差\u初始值设定项='0',
7激活(='relu')
---->8 y=密度1(输入x)
19帧
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value、ctx、dtype)
96 dtype=dtypes.as\u dtype(dtype).as\u datatype\u enum
97 ctx.确保_已初始化()
--->98返回操作数(值,ctx.device\u名称,数据类型)
99
100
ValueError:TypeError:InverseGamma类型的对象没有len()
我真的不知道这是不是最好的方法。我看过文档,只看到它使用tf.random模块用于初始权重。这是正确的吗?是否有一种从任何分布初始化层权重的通用方法?(还有一个额外的问题:有没有更好的方法来测试这个权重初始化是否遵循该分布?)
提前谢谢你
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-a14493da74d7> in <module>()
6 bias_initializer='zeros',
7 activation='relu')
----> 8 y = dense1(input_x)
19 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: TypeError: object of type 'InverseGamma' has no len()