def compute_hessian(y, params): grads = chainer.grad([y], params, enable_double_backprop=True) flat_grads = trpo._flatten_and_concat_variables(grads) hessian_rows = [] for i in range(len(flat_grads)): ggrads = chainer.grad([flat_grads[i]], params) assert all(ggrad is not None for ggrad in ggrads) ggrads_data = [ggrad.data for ggrad in ggrads] flat_ggrads_data = trpo._flatten_and_concat_ndarrays(ggrads_data) hessian_rows.append(flat_ggrads_data) return np.asarray(hessian_rows)
def compute_hessian_vector_product(y, params, vec): grads = chainer.grad( [y], params, enable_double_backprop=True) flat_grads = trpo._flatten_and_concat_variables(grads) return trpo._hessian_vector_product(flat_grads, params, vec)