This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = tf.Variable([1., 3.], tf.float32) | |
# Rosenbrock function | |
y = tf.add(tf.pow(tf.subtract(1.0, x[0]), 2.0), | |
tf.multiply(100.0, tf.pow(tf.subtract(x[1],tf.pow(x[0], 2.0)), 2.0)), 'y') | |
dx = tf.gradients(y, x)[0] | |
val = np.array([1, 3], dtype=np.float32) | |
with tf.Session() as sess: | |
sess.run(tf.global_variables_initializer()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build a function that also returns gradients using autograd. | |
rosenbrock_with_grad = autograd.value_and_grad(rosenbrock) | |
# Optimize using conjugate gradients from scipy | |
result = scipy.optimize.minimize(rosenbrock_with_grad, x0=np.array([0.0, 0.0]), jac=True, method='CG') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = [Variable(FloatTensor([1]), requires_grad=True), | |
Variable(FloatTensor([3]), requires_grad=True)] | |
y = rosenbrock(x) | |
y.backward() | |
print(y, x[0].grad, x[1].grad) | |
#Expect to see 400, -800, 400 as a result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto rosenbrockCostFunction = new ceres::AutoDiffCostFunction<Rosenbrock, 1, 1, 1>(new Rosenbrock()); | |
constexpr double x = 1, y = 3; | |
parameters[0][0] = x; | |
parameters[1][0] = y; | |
rosenbrockCostFunction->Evaluate(parameters, &residuals, jacobians); | |
cout << residuals << " df/dx=" << jacobians[0][0] << " df/dy" << jacobians[1][0] << "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rosenbrock(x): | |
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rosenbrock function, a = 1.0, b = 100 | |
class Rosenbrock { | |
public: | |
template <typename T> | |
bool operator()(const T *const x, const T *const y, T *cost) const { | |
cost[0] = (T(1.0) - x[0]) * (T(1.0) - x[0]) + | |
T(100.0) * (y[0] - x[0] * x[0]) * (y[0] - x[0] * x[0]); | |
return true; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jet operator*(Jet& f, Jet& g) { | |
return Jet(f.a * g.a, // The scalar part | |
f.a * g.v + f.v * g.a // The infinitesimal vector part | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ceres Solver - A fast non-linear least squares minimizer | |
// Copyright 2015 Google Inc. All rights reserved. | |
// http://ceres-solver.org/ | |
// | |
// Redistribution and use in source and binary forms, with or without | |
// modification, are permitted provided that the following conditions are met: | |
// | |
// * Redistributions of source code must retain the above copyright notice, | |
// this list of conditions and the following disclaimer. | |
// * Redistributions in binary form must reproduce the above copyright notice, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <benchmark/benchmark.h> | |
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <list> | |
#include <array> | |
#include <queue> | |
#include <deque> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <list> | |
#include <array> | |
#include <queue> | |
#include <deque> | |
using namespace std; | |
class Graph{ |
NewerOlder