def test_optimize(self):
scalar = variables.Variable(random_ops.random_normal([]), 'scalar')
vector = variables.Variable(random_ops.random_normal([2]), 'vector')
matrix = variables.Variable(random_ops.random_normal([2, 3]), 'matrix')
minimum_location = constant_op.constant(np.arange(9), dtype=dtypes.float32)
loss = math_ops.reduce_sum(math_ops.square(vector -
minimum_location[:2])) / 2.
loss += math_ops.reduce_sum(math_ops.square(scalar - minimum_location[
2])) / 2.
loss += math_ops.reduce_sum(
math_ops.square(matrix - array_ops.reshape(minimum_location[3:],
[2, 3]))) / 2.
optimizer = MockOptimizerInterface(loss)
with self.test_session() as sess:
sess.run(variables.global_variables_initializer())
optimizer.minimize(sess)
self.assertAllClose(np.arange(2), sess.run(vector))
self.assertAllClose(np.arange(1) + 2, sess.run(scalar))
self.assertAllClose(np.arange(6).reshape(2, 3) + 3, sess.run(matrix))
external_optimizer_test.py 文件源码
python
阅读 18
收藏 0
点赞 0
评论 0
评论列表
文章目录