- Neural Network Programming with TensorFlow
- Manpreet Singh Ghotra Rajdeep Dua
- 177字
- 2021-07-02 15:17:10
Hessian
Gradient is the first derivative for functions of vectors, whereas hessian is the second derivative. We will go through the notation now:

Similar to the gradient, the hessian is defined only when f(x) is real-valued.
The algebraic function used is
.

The following example shows the hessian implementation using TensorFlow:
import tensorflow as tf
import numpy as np
X = tf.Variable(np.random.random_sample(), dtype=tf.float32)
y = tf.Variable(np.random.random_sample(), dtype=tf.float32)
def createCons(x):
return tf.constant(x, dtype=tf.float32)
function = tf.pow(X, createCons(2)) + createCons(2) * X * y + createCons(3) * tf.pow(y, createCons(2)) + createCons(4) * X + createCons(5) * y + createCons(6)
# compute hessian
def hessian(func, varbles):
matrix = []
for v_1 in varbles:
tmp = []
for v_2 in varbles:
# calculate derivative twice, first w.r.t v2 and then w.r.t v1
tmp.append(tf.gradients(tf.gradients(func, v_2)[0], v_1)[0])
tmp = [createCons(0) if t == None else t for t in tmp]
tmp = tf.stack(tmp)
matrix.append(tmp)
matrix = tf.stack(matrix)
return matrix
hessian = hessian(function, [X, y])
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(hessian))
The output of this is shown as follows:
[[ 2. 2.] [ 2. 6.]]
推薦閱讀
- Access 2007數據庫應用上機指導與練習
- 大數據導論
- Hadoop 3.x大數據開發實戰
- 智能數據時代:企業大數據戰略與實戰
- Oracle PL/SQL實例精解(原書第5版)
- PostgreSQL指南:內幕探索
- 數據庫原理與應用
- 貫通SQL Server 2008數據庫系統開發
- 深入理解InfluxDB:時序數據庫詳解與實踐
- Unity 2018 By Example(Second Edition)
- The Natural Language Processing Workshop
- Scratch 2.0 Game Development HOTSHOT
- PostgreSQL高可用實戰
- Oracle 11g數據庫管理與開發基礎教程
- 云原生架構:從技術演進到最佳實踐