Unverified Commit 558664bf authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #147 from kharazity/mmd_loss

Mmd loss
parents b17fdf1d f462cc38
......@@ -31,53 +31,50 @@ protected:
//it's always the case that x = y = [0, 1, ..., pow(2, num_bit)-1];
std::vector<int> temp_x(x.size());
std::vector<int> temp_y(y.size());
for(int i = 0; i < temp_x.size(); i++){
temp_x[i] = (x[i] >> i)&1;
temp_y[i] = (y[i] >> i)&1;
}
Eigen::MatrixXd dx2 = Eigen::MatrixXd::Zero(x.size(), x.size());
for(int i = 0; i < x.size(); i++){
for(int i = 0; i < num_bit; i++){
for(int j = 0; j < x.size(); j++){
if(x[i] != y[j]){
dx2(i,j)= 1;
temp_x[j] = (x[j]>>i)&1;
temp_y[j] = (y[j] >>i)&1;
}
for(int k = 0; k < x.size(); k++){
for(int l = 0; l < x.size(); l++){
dx2(k,l) += (temp_x[k] != temp_y[l]);
}
}
}
return _mix_rbf_kernel_d(dx2, sigma_list);
}
Eigen::MatrixXd _mix_rbf_kernel_d(Eigen::MatrixXd dx2, std::vector<double> sigma_list){
Eigen::MatrixXd K = Eigen::MatrixXd::Zero(dx2.rows(), dx2.cols());
Eigen::MatrixXd _dx2 = dx2;
for(auto &sigma : sigma_list){
double gamma = 1.0/(2*sigma);
for(int i = 0; i < dx2.rows(); i++){
for(int j = 0; j < dx2.cols(); j++){
_dx2(i,j) = std::exp(-gamma*dx2(i,j));
Eigen::MatrixXd _dx2 = dx2;
for(auto &sigma : sigma_list){
double gamma = 1.0/(2*sigma);
for(int i = 0; i < dx2.rows(); i++){
for(int j = 0; j < dx2.cols(); j++){
_dx2(i,j) = std::exp(-gamma*dx2(i,j));
}
}
K += _dx2;
}
K += _dx2;
}
return K;
return K;
}
double kernel_expect(Eigen::MatrixXd K,
std::vector<double> px,
std::vector<double> py){
int len = px.size();
//This takes px and py and turns them into eigen::Vector objects
//YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX
Eigen::VectorXd P = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(px.data(), px.size());
Eigen::VectorXd Q = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(py.data(), py.size());
//std::cout<<"K =\n" << K << "\n";
auto temp = K*Q;
double expectation = P.dot(temp);
//std::cout << "expect = \n" << expectation << "\n";
return expectation;
}
......@@ -105,12 +102,6 @@ public:
pxy[i] = std::abs(target[i] - q[i]);
}
std::cout<<"pxy = [";
for(int i = 0; i < pxy.size(); i++){
std::cout<< pxy[i] <<", ";
}
std::cout<<"]"<<"\n";
//worried about edge cases, anywhere in here where I can access this information
//without type instability
......@@ -123,7 +114,7 @@ public:
basis[i] = i;
}
std::vector<double> sigma_list = {0.1};
std::vector<double> sigma_list = {0.25};
Eigen::MatrixXd K = mix_rbf_kernel(basis, basis, sigma_list, num_bit);
auto mmd = kernel_expect(K, pxy, pxy);
......@@ -144,11 +135,6 @@ public:
//Loss function implemetation complete
//TODO figure a way to pass sigma_list to compute
//figure out how to get num_bit info without type instability
class MMDParameterShiftGradientStrategy : public GradientStrategy {
......@@ -163,12 +149,10 @@ public:
//YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX
Eigen::VectorXd P = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(px.data(), px.size());
Eigen::VectorXd Q = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(py.data(), py.size());
//std::cout<<"K =\n" << K << "\n";
auto temp = K*Q;
double expectation = P.dot(temp);
double expectation = P.dot(K*Q);
//std::cout << "expect = \n" << expectation << "\n";
return expectation;
}
......@@ -177,18 +161,18 @@ public:
//it's always the case that x = y = [0, 1, ..., pow(2, num_bit)-1];
std::vector<int> temp_x(x.size());
std::vector<int> temp_y(y.size());
for(int i = 0; i < temp_x.size(); i++){
temp_x[i] = (x[i] >> i)&1;
temp_y[i] = (y[i] >> i)&1;
}
Eigen::MatrixXd dx2 = Eigen::MatrixXd::Zero(x.size(), x.size());
for(int i = 0; i < x.size(); i++){
for(int j = 0; j < x.size(); j++){
if(x[i] != y[j]){
dx2(i,j)= 1;
}
Eigen::MatrixXd dx2 = Eigen::MatrixXd::Zero(x.size(), x.size());
for(int i = 0; i < num_bit; i++){
for(int j = 0; j < x.size(); j++){
temp_x[j] = (x[j]>>i)&1;
temp_y[j] = (y[j] >>i)&1;
}
for(int k = 0; k < x.size(); k++){
for(int l = 0; l < x.size(); l++){
dx2(k,l) += (temp_x[k] != temp_y[l]);
}
}
}
return _mix_rbf_kernel_d(dx2, sigma_list);
}
......@@ -197,12 +181,12 @@ public:
Eigen::MatrixXd _dx2 = dx2;
for(auto &sigma : sigma_list){
double gamma = 1.0/(2*sigma);
for(int i = 0; i < dx2.rows(); i++){
for(int j = 0; j < dx2.cols(); j++){
_dx2(i,j) = std::exp(-gamma*dx2(i,j));
for(int i = 0; i < dx2.rows(); i++){
for(int j = 0; j < dx2.cols(); j++){
_dx2(i,j) = std::exp(-gamma*dx2(i,j));
}
}
}
K += _dx2;
K += _dx2;
}
return K;
}
......@@ -258,6 +242,7 @@ public:
int counter = 0;
std::vector<std::vector<double>> qplus_theta, qminus_theta;
for (int i = 0; i < results.size(); i += 2) {
std::vector<double> qp(q_dist.size()), qm(q_dist.size());
for (auto &x : results[i]->getMeasurementCounts()) {
int idx = std::stoi(x.first, nullptr, 2);
......@@ -271,9 +256,11 @@ public:
std::vector<double> shiftedm = currentParameterSet;
auto xplus = currentParameterSet[counter] + xacc::constants::pi / 2;
auto xminus = currentParameterSet[counter] - xacc::constants::pi / 2;
shiftedp[counter] = xplus;
shiftedm[counter] = xminus;
results[i]->addExtraInfo("gradient-index", counter);
results[i+1]->addExtraInfo("gradient-index", counter);
......@@ -290,6 +277,7 @@ public:
qminus_theta.push_back(qm);
counter++;
}
std::vector<double> sigma_list = {0.1};
......@@ -300,17 +288,17 @@ public:
}
Eigen::MatrixXd K = mix_rbf_kernel(basis, basis, sigma_list, num_bit);
//compute gradient vector
std::vector<double> grad_pos(q_dist.size());
std::vector<double> grad_neg(q_dist.size());
std::vector<double> grad_pos_targ(q_dist.size());
std::vector<double> grad_neg_targ(q_dist.size());
for(int i = 0; i < q_dist.size(); i++){
std::vector<double> grad_pos(counter);
std::vector<double> grad_neg(counter);
std::vector<double> grad_pos_targ(counter);
std::vector<double> grad_neg_targ(counter);
for(int i = 0; i < counter; i++){
grad_pos[i] = kernel_expect(K, qplus_theta[i], q_dist);
grad_neg[i] = kernel_expect(K, qminus_theta[i], q_dist);
grad_pos_targ[i] = kernel_expect(K, qplus_theta[i], target_dist);
grad_neg_targ[i] = kernel_expect(K, qminus_theta[i], target_dist);
grad[i] = grad_pos[i]-grad_pos_targ[i]-grad_neg[i]+grad_neg_targ[i];
std::cout<<"grad: "<<grad[i]<<"\n";
}
return;
}
......@@ -321,4 +309,3 @@ public:
} // namespace algorithm
} // namespace xacc
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment