Python 构造张量流双射体误差

Python 构造张量流双射体误差,python,tensorflow,machine-learning,unsupervised-learning,tensorflow-probability,Python,Tensorflow,Machine Learning,Unsupervised Learning,Tensorflow Probability,我不太会使用tensorflow。我想构造一个具有以下性质的Bijter:它取一个N维概率分布p(x1,x2,…,xn),它只变换两个特定的维数i和j,使得席席=Xi,xj′=xj*EXP(s(席))+t(xj),其中S和T是利用神经网络实现的两个函数。它输出p(x1,x2,…,xi',…,xj',…,xn)。 我有一个基本代码,如下所示: def net(x, out_size, block_w_id, block_d_id, layer_id): x = tf.contrib.l

我不太会使用tensorflow。我想构造一个具有以下性质的Bijter:它取一个N维概率分布p(x1,x2,…,xn),它只变换两个特定的维数i和j,使得席席=Xi,xj′=xj*EXP(s(席))+t(xj),其中S和T是利用神经网络实现的两个函数。它输出p(x1,x2,…,xi',…,xj',…,xn)。 我有一个基本代码,如下所示:

  def net(x, out_size, block_w_id, block_d_id, layer_id):
    x = tf.contrib.layers.fully_connected(x, 256, reuse=tf.AUTO_REUSE, scope='x1_block_w_{}_block_d_{}_layer_{}'.format(block_w_id, \
                                                                                                                       block_d_id,\
                                                                                                                       layer_id))
    x = tf.contrib.layers.fully_connected(x, 256, reuse=tf.AUTO_REUSE, scope='x2_block_w_{}_block_d_{}_layer_{}'.format(block_w_id,\
                                                                                                                       block_d_id,\
                                                                                                                       layer_id))
    y = tf.contrib.layers.fully_connected(x, out_size, reuse=tf.AUTO_REUSE, scope='y_block_w_{}_block_d_{}_layer_{}'.format(block_w_id,\
                                                                                                                           block_d_id,\
                                                                                                                           layer_id))
#     return layers.stack(x, layers.fully_connected(reuse=tf.AUTO_REUSE), [512, 512, out_size])
    return y


但它没有像我想的那样起作用。当我使用该类时,它会弹出一个错误:

base_dist = tfd.MultivariateNormalDiag(loc=tf.zeros([2], DTYPE))
num_bijectors = 4
bijectors = []
bijectors.append(NVPCoupling(input_idx1=0, input_idx2=1, \
                             block_w_id=0, block_d_id=0, layer_id=0))
bijectors.append(NVPCoupling(input_idx1=1, input_idx2=0, \
                             block_w_id=0, block_d_id=0, layer_id=1))
bijectors.append(NVPCoupling(input_idx1=0, input_idx2=1, \
                             block_w_id=0, block_d_id=0, layer_id=2))
bijectors.append(NVPCoupling(input_idx1=0, input_idx2=1, \
                             block_w_id=0, block_d_id=0, layer_id=3))
flow_bijector = tfb.Chain(list(reversed(bijectors)))
dist = tfd.TransformedDistribution(
    distribution=base_dist,
    bijector=flow_bijector)
dist.sample(1000)

有误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-04da05d30f8d> in <module>()
----> 1 dist.sample(1000)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/distributions/distribution.pyc in sample(self, sample_shape, seed, name)
    708       samples: a `Tensor` with prepended dimensions `sample_shape`.
    709     """
--> 710     return self._call_sample_n(sample_shape, seed, name)
    711 
    712   def _log_prob(self, value):

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/distributions/transformed_distribution.pyc in _call_sample_n(self, sample_shape, seed, name, **kwargs)
    412       # returned result.
    413       y = self.bijector.forward(x, **kwargs)
--> 414       y = self._set_sample_static_shape(y, sample_shape)
    415 
    416       return y

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/distributions/distribution.pyc in _set_sample_static_shape(self, x, sample_shape)
   1220       shape = tensor_shape.TensorShape(
   1221           [None]*(ndims - event_ndims)).concatenate(self.event_shape)
-> 1222       x.set_shape(x.get_shape().merge_with(shape))
   1223 
   1224     # Infer batch shape.

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.pyc in merge_with(self, other)
    671         return TensorShape(new_dims)
    672       except ValueError:
--> 673         raise ValueError("Shapes %s and %s are not compatible" % (self, other))
    674 
    675   def concatenate(self, other):

ValueError: Shapes (1000, 4) and (?, 2) are not compatible
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1区样品(1000)
/示例中的Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/Python/ops/distributions/distribution.pyc(self、sample_shape、seed、name)
708个样本:一个带有前置尺寸“样本形状”的“张量”。
709     """
-->710返回自我。调用样本(样本形状、种子、名称)
711
712定义日志问题(自身,值):
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/Python/ops/distributions/transformed_distribution.pyc in_call_sample_n(self,sample_shape,seed,name,**kwargs)
412#返回结果。
413 y=自双喷射器向前(x,**kwargs)
-->414 y=自身。\设置\样本\静态\形状(y,样本\形状)
415
416返回y
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/Python/ops/distributions/distribution.pyc in\u set\u sample\u static\u shape(self,x,sample\u shape)
1220形状=张量_形状。张量形状(
1221[None]*(ndims-event\u ndims)).concatenate(self.event\u形状)
->1222 x.set_shape(x.get_shape().将_与(shape)合并)
1223
1224#推断批次形状。
/库/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/Python/framework/tensor\u shape.pyc与(self,other)合并
671返回张力形状(新尺寸)
672除值错误外:
-->673提升值错误(“形状%s和%s不兼容”%(自身、其他))
674
675 def串联(自身、其他):
ValueError:形状(1000,4)和(?,2)不兼容

真的希望一些专家能帮助我理解我哪里做错了,以及如何纠正。非常感谢!
H.

我认为问题就在这里(为了清晰起见,稍微重新格式化):

这假设了
idx2>idx1
,但在给定
idx1=1
idx2=0
的情况下,这是不正确的。这会导致给定的第二个dim(4而不是2)比您想要的更多

我在
\u forward
中打印形状,因此:

print("self.idx1: %s" % self.idx1)
print("self.idx2: %s" % self.idx2)
print("x[:,0:self.idx1]: %s" % x[:,0:self.idx1].shape)
print("x_left: %s" % x_left.shape)
print("x[:, self.idx1+1:self.idx2]: %s" %
      x[:, self.idx1+1:self.idx2].shape)
print("x_right.shape: %s" % x_right.shape)
print("y_right: %s" % y_right.shape)
print("x[:, (self.idx2+1):]: %s" % x[:, (self.idx2+1):].shape)
print("output_tensor.shape: %s" % output_tensor.shape)
得到了这个输出:

self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 0)
output_tensor.shape: (1000, 2)

self.idx1: 1
self.idx2: 0
x[:,0:self.idx1]: (1000, 1)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 1)
output_tensor.shape: (1000, 4)

self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 2)
output_tensor.shape: (1000, 4)

self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 2)
output_tensor.shape: (1000, 4)
我认为,当idx1>idx2时,您需要更仔细地考虑重新组装此块中的混凝土件

希望这能让你回到正轨

print("self.idx1: %s" % self.idx1)
print("self.idx2: %s" % self.idx2)
print("x[:,0:self.idx1]: %s" % x[:,0:self.idx1].shape)
print("x_left: %s" % x_left.shape)
print("x[:, self.idx1+1:self.idx2]: %s" %
      x[:, self.idx1+1:self.idx2].shape)
print("x_right.shape: %s" % x_right.shape)
print("y_right: %s" % y_right.shape)
print("x[:, (self.idx2+1):]: %s" % x[:, (self.idx2+1):].shape)
print("output_tensor.shape: %s" % output_tensor.shape)
self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 0)
output_tensor.shape: (1000, 2)

self.idx1: 1
self.idx2: 0
x[:,0:self.idx1]: (1000, 1)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 1)
output_tensor.shape: (1000, 4)

self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 2)
output_tensor.shape: (1000, 4)

self.idx1: 0
self.idx2: 1
x[:,0:self.idx1]: (1000, 0)
x_left: (1000, 1)
x[:, self.idx1+1:self.idx2]: (1000, 0)
x_right.shape: (1000, 1)
y_right: (1000, 1)
x[:, (self.idx2+1):]: (1000, 2)
output_tensor.shape: (1000, 4)