def _get_potentials(self, x, w):
# check sizes?
n_node_coefs = self.n_prop_states * self.n_prop_features
n_link_coefs = self.n_link_states * self.n_link_features
n_compat_coefs = self.n_prop_states ** 2 * self.n_link_states
if self.compat_features:
n_compat_coefs *= self.n_compat_features_
assert w.size == (n_node_coefs + n_link_coefs + n_compat_coefs +
self.n_second_order_features_ *
self.n_second_order_factors_)
w_node = w[:n_node_coefs]
w_node = w_node.reshape(self.n_prop_states, self.n_prop_features)
w_link = w[n_node_coefs:n_node_coefs + n_link_coefs]
w_link = w_link.reshape(self.n_link_states, self.n_link_features)
# for readability, consume w. This is not inplace, don't worry.
w = w[n_node_coefs + n_link_coefs:]
w_compat = w[:n_compat_coefs]
if self.compat_features:
w_compat = w_compat.reshape((self.n_compat_features_, -1))
w_compat = np.dot(x.X_compat, w_compat)
compat_potentials = w_compat.reshape((-1,
self.n_prop_states,
self.n_prop_states,
self.n_link_states))
else:
compat_potentials = w_compat.reshape(self.n_prop_states,
self.n_prop_states,
self.n_link_states)
w = w[n_compat_coefs:]
coparent_potentials = grandparent_potentials = sibling_potentials = []
if self.coparents:
w_coparent = w[:self.n_second_order_features_]
coparent_potentials = safe_sparse_dot(x.X_sec_ord, w_coparent)
w = w[self.n_second_order_features_:]
if self.grandparents:
w_grandparent = w[:self.n_second_order_features_]
grandparent_potentials = safe_sparse_dot(x.X_sec_ord,
w_grandparent)
w = w[self.n_second_order_features_:]
if self.siblings:
w_sibling = w[:self.n_second_order_features_]
sibling_potentials = safe_sparse_dot(x.X_sec_ord, w_sibling)
prop_potentials = safe_sparse_dot(x.X_prop, w_node.T)
link_potentials = safe_sparse_dot(x.X_link, w_link.T)
return (prop_potentials, link_potentials, compat_potentials,
coparent_potentials, grandparent_potentials,
sibling_potentials)
评论列表
文章目录