假设我使用Keras定义了一个网络,如下所示:
model = Sequential()
model.add(Dense(10, input_shape=(10,), activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(9, activation='sigmoid'))
它有一些重量:
[array([[ 0.33494413, -0.34308964, 0.12796348, 0.17187083, -0.40254939,
-0.06909397, -0.30374748, 0.14217842, 0.41163749, -0.15252003],
[-0.07053435, 0.53712451, -0.43015254, -0.28653857, 0.53299475, ...
当我给它一些输入时:
[[ 0. 0.5 0. 0.5 1. 1. 0. 0.5 0.5 0.5]]
它会产生一些输出:
[0.5476531982421875, 0.5172237753868103, 0.5247090458869934, 0.49434927105903625, 0.4599153697490692, 0.44612908363342285, 0.4727349579334259, 0.5116984844207764, 0.49565717577934265]
而期望的输出是:
[0.6776225034927386, 0.0, 0.5247090458869934, 0.0, 0.0, 0.0, 0.4727349579334259, 0.5116984844207764, 0.49565717577934265]
制作错误值:
[0.12996930525055106, -0.5172237753868103, 0.0, -0.49434927105903625, -0.4599153697490692, -0.44612908363342285, 0.0, 0.0, 0.0]
然后我可以按如下方式计算评估的梯度:
outputTensor = model.output
listOfVariableTensors = model.trainable_weights
gradients = k.gradients(outputTensor, listOfVariableTensors)
trainingInputs = inputs
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
evaluated_gradients = sess.run(gradients, feed_dict={model.input: trainingInputs})
评估渐变的结果:
[array([[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ],
[ 0.01015381, 0. , 0. , 0.03375177, -0.05576257,
0.03318337, -0.02608909, -0.06644543, -0.03461133, 0. ],
[ 0.02030762, 0. , 0. , 0.06750354, -0.11152515,
0.06636675, -0.05217818, -0.13289087, -0.06922265, 0. ],...
我想用这些渐变来调整我的模型,但我正在失去对数学和数学的追踪。反向传播理论。我是在正确的轨道上吗?