我正在尝试使用 TensorFlow 和 TensorFlow Probability 实现期望最大化算法。它运行得很好,直到我尝试实现缺失数据(数据可以在某些随机维度中包含 NaN 值)。
问题是,由于缺少数据,我无法再将所有操作作为向量运算进行,我必须使用索引和 for 循环,如下所示:
# Here we iterate through all the data samples
for i in range(n):
# x_i is the sample i
x_i = tf.expand_dims(x[:, i], 1)
gamma.append(estimate_gamma(x_i, pi, norm, ber))
est_x_n_i = []
est_xx_n_i = []
est_x_b_i = []
for j in range(k):
mu_k = norm.mean()[j, :]
sigma_k = norm.covariance()[j, :, :]
rho_k = ber.mean()[j, :]
est_x_n_i.append(estimate_x_norm(x_i[:d, :], mu_k, sigma_k))
est_xx_n_i.append(estimate_xx_norm(x_i[:d, :], mu_k, sigma_k))
est_x_b_i.append(estimate_x_ber(x_i[d:, :], rho_k))
est_x_n.append(tf.convert_to_tensor(est_x_n_i))
est_xx_n.append(tf.convert_to_tensor(est_xx_n_i))
est_x_b.append(tf.convert_to_tensor(est_x_b_i))
Run Code Online (Sandbox Code Playgroud)
我发现这些操作效率并不高。虽然第一个样本每个样本花费的时间大约不到 …