Skip to content

Instantly share code, notes, and snippets.

View ahmadyan's full-sized avatar

Adel Ahmadyan ahmadyan

View GitHub Profile
@ahmadyan
ahmadyan / rosen-tf.py
Created November 12, 2018 04:19
Autodifferentiation of Rosenbrock function using Tensorflow
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())
@ahmadyan
ahmadyan / rosen-autograd.py
Created November 12, 2018 04:18
Auto-differentiation of Rosenbrock function using AutoGrad
# 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')
@ahmadyan
ahmadyan / rosen-pytorch.py
Created November 12, 2018 04:17
Autodiff Rosenbrock function using PyTorch
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
@ahmadyan
ahmadyan / RosenbrockJet.cpp
Created November 12, 2018 04:16
Auto diff for Rosenbrock function using Ceres' Jets
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";
@ahmadyan
ahmadyan / Rosenbrock.py
Created November 12, 2018 04:15
Rosenbrock function in Python
def rosenbrock(x):
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2
@ahmadyan
ahmadyan / Rosenbrock.cpp
Created November 12, 2018 04:14
Rosenbrock function example for auto-differentiation
// 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;
}
};
@ahmadyan
ahmadyan / jet.cpp
Created November 12, 2018 04:13
Jet Operator Overloading
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
);
}
@ahmadyan
ahmadyan / simple_bundle_adjuster.cc
Created May 8, 2017 23:54
Test for computing the cost from residuals
// 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,
@ahmadyan
ahmadyan / bfs_benchmark.cpp
Created December 13, 2016 00:11
Benchmark of different BFS implementations
#include <benchmark/benchmark.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
#include <list>
#include <array>
#include <queue>
#include <deque>
@ahmadyan
ahmadyan / bfs_implementations.cpp
Created December 13, 2016 00:10
Different implementations of the BFS algorithm
#include <iostream>
#include <vector>
#include <random>
#include <list>
#include <array>
#include <queue>
#include <deque>
using namespace std;
class Graph{