def testSaveAndLoad(self):
    """Tests saving and loading a meta-optimizer."""
    layers = (2, 3)
    net_options = {"layers": layers, "initializer": "zeros"}
    num_unrolls = 2
    num_epochs = 1

    problem = problems.simple()

    # Original optimizer.
    with tf.Graph().as_default() as g1:
      optimizer = meta.MetaOptimizer(net=dict(
          net="CoordinateWiseDeepLSTM",
          net_options=net_options))
      minimize_ops = optimizer.meta_minimize(problem, 3)

    with self.test_session(graph=g1) as sess:
      sess.run(tf.global_variables_initializer())
      train(sess, minimize_ops, 1, 2)

      # Save optimizer.
      tmp_dir = tempfile.mkdtemp()
      save_result = optimizer.save(sess, path=tmp_dir)
      net_path = next(iter(save_result))

      # Retrain original optimizer.
      cost, x = train(sess, minimize_ops, num_unrolls, num_epochs)

    # Load optimizer and retrain in a new session.
    with tf.Graph().as_default() as g2:
      optimizer = meta.MetaOptimizer(net=dict(
          net="CoordinateWiseDeepLSTM",
          net_options=net_options,
          net_path=net_path))
      minimize_ops = optimizer.meta_minimize(problem, 3)

    with self.test_session(graph=g2) as sess:
      sess.run(tf.global_variables_initializer())
      cost_loaded, x_loaded = train(sess, minimize_ops, num_unrolls, num_epochs)

    # The last cost should be the same.
    self.assertAlmostEqual(cost, cost_loaded, places=3)
    self.assertAlmostEqual(x[0], x_loaded[0], places=3)

    # Cleanup.
    os.remove(net_path)
    os.rmdir(tmp_dir)
Tests saving and loading a meta-optimizer.
meta_test.py