Python 3.x 在Tensorflow概率回报中具有密度变化层的简单线性回归模型:TypeError:';非类型';对象不可调用
这是一次尝试使用Tensforflow概率,更具体地说是密度变化层,但由于某些原因失败了。如何更正代码Python 3.x 在Tensorflow概率回报中具有密度变化层的简单线性回归模型:TypeError:';非类型';对象不可调用,python-3.x,linear-regression,tensorflow2.0,tensorflow-probability,Python 3.x,Linear Regression,Tensorflow2.0,Tensorflow Probability,这是一次尝试使用Tensforflow概率,更具体地说是密度变化层,但由于某些原因失败了。如何更正代码 x_train = np.linspace(-1, 1, 100)[:, np.newaxis] y_train = x_train + 0.3*np.random.randn(100)[:, np.newaxis] def prior(kernel_size, bias_size, dtype = None): n = kernel_size + bias_size
x_train = np.linspace(-1, 1, 100)[:, np.newaxis]
y_train = x_train + 0.3*np.random.randn(100)[:, np.newaxis]
def prior(kernel_size, bias_size, dtype = None):
n = kernel_size + bias_size
prior_model = Sequential([
tfpl.DistributionLambda(
lambda t: tfd.MultivariateNormalDiag(loc = tf.zeros(n) , scale_diag = tf.ones(n)
))
])
def posterior(kernel_size, bias_size, dtype = None):
n = kernel_size + bias_size
posterior_model = Sequential([
tfpl.VariableLayer(tfpl.MultivariateNormalTriL.params_size(n) , dtype = dtype), # The parameters of the model are declared Variables that are trainable
tfpl.MultivariateNormalTriL(n) # The posterior function will return to the Variational layer that will call it a MultivariateNormalTril object that will have as many dimensions
# as the parameters of the Variational Dense Layer. That means that each parameter will be generated by a distinct Normal Gaussian shifted and scaled
# by a mu and sigma learned from the data, independently of all the other weights. The output of this Variablelayer will become the input to the
# MultivariateNormalTriL object.
# The shape of the VariableLayer object will be defined by the number of parameters needed to create the MultivariateNormalTriL object given
# that it will live in a Space of n dimensions (event_size = n). This number is returned by the tfpl.MultivariateNormalTriL.params_size(n)
])
return(posterior_model)
model = Sequential([
tfpl.DenseVariational(
input_shape = (1, ), # The input is of dimensionality 1, a series
units = 1, # A linear regression is represented by a Dense layer with one single unit
make_prior_fn = prior, # We pass the function we have defined which returns the prior distribution on the weights
make_posterior_fn = posterior, # We pass the function we have defined which returns the variational approximation of the posterior distribution on the weights
kl_weight = 1/ x_train.shape[0], # Tensorflow scales the likelihood loss calculated using the mini-batch to become an unbiased estimator of the true loss but does not do the
# same for the DL divergence loss. Here we instruct it to do the necessary scaling.
kl_use_exact = True # Unless there is a closed form equation for the KL divergence in the library of Tensorflow setting True will return error. By setting False instead
# the KL Divergence will be approxiated using Sampling
)
])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-e7cf0bfd5902> in <module>
17 # same for the DL divergence loss. Here we instruct it to do the necessary scaling.
18
---> 19 kl_use_exact = True # Unless there is a closed form equation for the KL divergence in the library of Tensorflow setting True will return error. By setting False instead
20 # the KL Divergence will be approxiated using Sampling
21
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\sequential.py in __init__(self, layers, name)
140 layers = [layers]
141 for layer in layers:
--> 142 self.add(layer)
143
144 @property
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
204 # and create the node connecting the current layer
205 # to the input layer we just created.
--> 206 layer(x)
207 set_inputs = True
208
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
925 return self._functional_construction_call(inputs, args, kwargs,
--> 926 input_list)
927
928 # Maintains info about the `Layer.call` stack.
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1115 try:
1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
-> 1117 outputs = call_fn(cast_inputs, *args, **kwargs)
1118
1119 except errors.OperatorNotAllowedInGraphError as e:
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\autograph\impl\api.py in wrapper(*args, **kwargs)
253 try:
254 with conversion_ctx:
--> 255 return converted_call(f, args, kwargs, options=options)
256 except Exception as e: # pylint:disable=broad-except
257 if hasattr(e, 'ag_error_metadata'):
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\autograph\impl\api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
455 if conversion.is_in_whitelist_cache(f, options):
456 logging.log(2, 'Whitelisted %s: from cache', f)
--> 457 return _call_unconverted(f, args, kwargs, options, False)
458
459 if ag_ctx.control_status_ctx().status == ag_ctx.Status.DISABLED:
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\autograph\impl\api.py in _call_unconverted(f, args, kwargs, options, update_cache)
337
338 if kwargs is not None:
--> 339 return f(*args, **kwargs)
340 return f(*args)
341
~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\layers\dense_variational_v2.py in call(self, inputs)
120
121 q = self._posterior(inputs)
--> 122 r = self._prior(inputs)
123 self.add_loss(self._kl_divergence_fn(q, r))
124
TypeError: 'NoneType' object is not callable
x_train=np.linspace(-1,1100)[:,np.newaxis]
y_-train=x_-train+0.3*np.random.randn(100)[:,np.newaxis]
def prior(内核大小、偏差大小、数据类型=无):
n=内核大小+偏差大小
先验模型=序贯模型([
tfpl.DistributionLambda(
lambda t:tfd.多变量非线性诊断(loc=tf.零(n),scale_diag=tf.一(n)
))
])
def后验(内核大小、偏差大小、数据类型=无):
n=内核大小+偏差大小
后验模型=序贯模型([
tfpl.VariableLayer(tfpl.multivarianenormaltril.params_size(n),dtype=dtype),模型的参数是可训练的声明变量
tfpl.multivarianentormaltril(n)#后验函数将返回变分层,变分层将称之为具有相同维数的multivarianentormaltril对象
#作为变分稠密层的参数,这意味着每个参数将由一个不同的正态高斯位移和标度产生
#通过从数据中学习mu和sigma,独立于所有其他权重。此可变层的输出将成为
#多变量对象。
#VariableLayer对象的形状将由创建给定多变量对象所需的参数数量定义
#它将存在于n维空间中(event_size=n)。该数字由tfpl.multivarianentormaltril.params_size(n)返回
])
返回(后置模型)
模型=顺序([
去变异(
输入_shape=(1,),#输入为维度1,一个系列
单位=1,#线性回归用一个单位的密集层表示
使_prior_fn=prior,#我们传递我们定义的函数,该函数返回权重的先验分布
使_posterior_fn=posterior,#我们传递我们定义的函数,该函数返回权重上后验分布的变分近似值
kl_weight=1/x_train.shape[0],#Tensorflow对使用小批量计算的可能性损失进行缩放,以成为真实损失的无偏估计器,但不执行以下操作:
#DL发散损失也是如此。这里我们指示它进行必要的缩放。
kl_使用_exact=True#除非Tensorflow库中存在kl散度的封闭式方程,否则设置True将返回错误。改为设置False
#KL散度将通过采样进行近似
)
])
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
17#DL发散损失相同。在这里,我们指示它进行必要的缩放。
18
--->19 kl_使用_exact=True#除非Tensorflow库中存在kl散度的封闭式方程,否则设置True将返回错误。改为设置为False
20#KL散度将通过抽样进行近似
21
~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\training\tracking\base.py in\u method\u包装(self,*args,**kwargs)
455 self._self_setattr_tracking=False#pylint:disable=protected access
456试试:
-->457结果=方法(自身、*args、**kwargs)
458最后:
459 self._self_setattr_tracking=上一个值#pylint:disable=受保护访问
~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\keras\engine\sequential.py in\uuuuuu init\uuuu(self,layers,name)
140层=[层]
141对于分层:
-->142.添加(层)
143
144@property
~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\training\tracking\base.py in\u method\u包装(self,*args,**kwargs)
455 self._self_setattr_tracking=False#pylint:disable=protected access
456试试:
-->457结果=方法(自身、*args、**kwargs)
458最后:
459 self._self_setattr_tracking=上一个值#pylint:disable=受保护访问
添加中的~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\keras\engine\sequential.py(self,layer)
204#并创建连接当前层的节点
205#到我们刚刚创建的输入层。
-->206层(x)
207设置_输入=真
208
~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\keras\engine\base\u layer.py in\uuuuu调用(self,*args,**kwargs)
924如果处于功能构建模式(自身、输入、参数、kwargs、输入列表):
925返回自功能构造调用(输入、参数、kwargs、,
-->926输入(U列表)
927
928#维护有关“Layer.call”堆栈的信息。
~\Anaconda3\envs\tf2\lib\site packages\tensorflow\python\keras\engine\base\u layer.py in\u functional\u construction\u call(self、input、args、kwargs、input\u list)
1115尝试:
1116带操作。启用自动转换变量(自计算类型对象):
->1117输出=呼叫(强制转换)
def prior(kernel_size, bias_size, dtype=None):
n = kernel_size + bias_size
prior_model = tf.keras.Sequential([
tfp.layers.DistributionLambda(
lambda t: tfd.MultivariateNormalDiag(loc=tf.zeros(n), scale_diag=tf.ones(n)
))
])
return (prior_model)