我的最终目标是训练以均值和协方差为参数的4D多元高斯分布
哪里
和
当前我有以下代码:
import tensorflow as tf
import numpy as np
value = [[1,2.0,3,4,5],[0,2,4,6,8],[80,7,6,5,4]]
value=tf.constant(value)
cov= tf.slice(value,[0,int(value.shape[1])-1],[int(value.shape[0]),1])
mean= tf.slice(value,[0,0],[int(value.shape[0]),int(value.shape[1])-1])
eyes=tf.eye(int(mean.shape[1]),batch_shape=[int(value.shape[0])])
#eyes = tf.multiply(eyes,cov)
normal = tf.contrib.distributions.MultivariateNormalFullCovariance(
loc=mean,
covariance_matrix=eyes)
value = [[1,2.0,3,4,5],[0,2,4,6,8],[80,7,6,5,4]]
是其余代码可能正在接收的示例。
在上面的示例中
cov = <tf.Tensor 'Slice_2:0' shape=(3, 1) dtype=float32>
eyes = <tf.Tensor 'eye_1/MatrixDiag:0' shape=(3, 4, 4) dtype=float32>
cov = [[5.] [8.] [4.]]`
eyes = [[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]]`
我的问题是,在给定result
和cov
的情况下,如何获得eyes
?结果如下:
result = [[[5., 0., 0., 0.],
[0., 5., 0., 0.],
[0., 0., 5., 0.],
[0., 0., 0., 5.]],
[[8., 0., 0., 0.],
[0., 8., 0., 0.],
[0., 0., 8., 0.],
[0., 0., 0., 8.]],
[[4., 0., 0., 0.],
[0., 4., 0., 0.],
[0., 0., 4., 0.],
[0., 0., 0., 4.]]]
预先感谢。
答案 0 :(得分:1)
Tensorflow使用与numpy相同的索引类型,这可能非常强大。
您可以在此处查看详细信息:https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html请注意,np.newaxis
的定义与None
相同。
对于您的问题,您可以在数据中添加额外的维度,以确保数组的乘法运算没有歧义。
import numpy as np
cov = np.array([[5.],[8.],[4.]])
eyes = np.array([[[1.0,0,0,0],[0.0,1.0,0.0,0],[0.0,0.0,1.0,0],[0.0,0.0,0.0,1.0]],[[1.0,0,0,0],[0.0,1.0,0.0,0],[0.0,0.0,1.0,0],[0.0,0.0,0.0,1.0]],[[1.0,0,0,0],[0.0,1.0,0.0,0],[0.0,0.0,1.0,0],[0.0,0.0,0.0,1.0]]])
result = cov[:,:,None]*eyes
在此处使用None
会增加一个额外的维度,使cov
成为3x1x1数组,可以明确地与3x4x4数组相乘。您也可以在tensorflow中以这种方式使用None
。
如果每个对应维度的大小相同或其中一个的大小为1,则两个数组可以明确相乘。