1. 参考官方文档
2. 参考tensorflow学习笔记（三十）
3. 关于神经网络中的梯度计算，推荐吴恩达的deeplearning公开课

aggregation_method=None,

gradients() adds ops to the graph to output the derivatives of ys with respect to xs. It returns a list of Tensor of length len(xs) where each tensor is the sum(dy/dx) for y in ys.

2. 求导返回值是一个list，list的长度等于len(xs)
• $grad1=\frac{y1}{x1}+\frac{y2}{x1}$$grad1 = \frac {y1}{x1} + \frac {y2}{x1}$
• $grad2=\frac{y1}{x2}+\frac{y2}{x2}$$grad2 = \frac {y1}{x2} + \frac {y2}{x2}$
• $grad3=\frac{y1}{x3}+\frac{y2}{x3}$$grad3 = \frac {y1}{x3} + \frac {y2}{x3}$

## 基础实践

import numpy as np
import tensorflow as tf

sess = tf.Session()

x_input = tf.placeholder(tf.float32, name='x_input')
y_input = tf.placeholder(tf.float32, name='y_input')
w = tf.Variable(2.0, name='weight')
b = tf.Variable(1.0, name='biases')
loss_op = tf.reduce_sum(tf.pow(y_input - y, 2)) / (2 * 32)

'''tensorboard'''
# merged = tf.summary.merge_all()
# writer = tf.summary.FileWriter('log')

init = tf.global_variables_initializer()
sess.run(init)

'''构造数据集'''
x_pure = np.random.randint(-10, 100, 32)
x_train = x_pure + np.random.randn(32) / 10  # 为x加噪声
y_train = 3 * x_pure + 2 + np.random.randn(32) / 10  # 为y加噪声

for i in range(20):
feed_dict={x_input: x_train[i], y_input: y_train[i]})

sess.close()

epoch: 0     loss: 94.6083221436     gradients: [-187.66052]
epoch: 1     loss: 1.52120530605     gradients: [3.0984864]
epoch: 2     loss: 101.41834259      gradients: [241.91911]
...
epoch: 18    loss: 0.0215022582561   gradients: [-0.44370675]
epoch: 19    loss: 0.0189439821988   gradients: [-0.31349587]

## 其他参数

import tensorflow as tf

w1 = tf.get_variable('w1', shape=[3])
w2 = tf.get_variable('w2', shape=[3])

w3 = tf.get_variable('w3', shape=[3])
w4 = tf.get_variable('w4', shape=[3])

z1 = 3 * w1 + 2 * w2+ w3
z2 = -1 * w3 + w4

with tf.Session() as sess:
tf.global_variables_initializer().run()

[array([ 3.,  3.,  3.], dtype=float32),
array([ 2.,  2.,  2.], dtype=float32),
array([ 0.,  0.,  0.], dtype=float32),
array([ 1.,  1.,  1.], dtype=float32)]

[array([ -6.,  -9., -12.], dtype=float32),
array([-4., -6., -8.], dtype=float32),
array([0., 0., 0.], dtype=float32),
array([-2., -3., -4.], dtype=float32)]

a = tf.constant(0.)
b = 2 * a
c = a + b

a = tf.constant(0.)
b = 2 * a