Last active
December 18, 2018 09:29
-
-
Save Shikugawa/0498c45dc81d28a02dfdfaf8596cd863 to your computer and use it in GitHub Desktop.
アドカレ用
This file contains hidden or 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 <array> | |
#include <cmath> | |
template<typename T, typename U, size_t DATA_NUM> | |
using dataType = std::array<std::pair<T, U>, DATA_NUM>; | |
template<typename Array = std::array<double, 2>> | |
using Array = Array; | |
constexpr double operator*(Array<> a, Array<> b) { | |
double num = 0; | |
for(size_t i = 0; i < a.size(); ++i) | |
num += a[i]*b[i]; | |
return num; | |
} | |
constexpr Array<> operator*(double a, Array<> b) { | |
return {b[0]*a, b[1]*a}; | |
} | |
constexpr Array<> operator+(Array<> a, Array<> b) { | |
return {a[0]+b[0], a[1]+b[1]}; | |
} | |
constexpr Array<> operator+(Array<> a, double b) { | |
return {a[0]+b, a[1]+b}; | |
} | |
constexpr Array<> operator/(Array<> a, double b) { | |
return {a[0]/b, a[1]/b}; | |
} | |
constexpr Array<> operator/(Array<> a, Array<> b) { | |
return {a[0]/b[0], a[1]/b[1]}; | |
} | |
constexpr Array<> operator-(Array<> a, Array<> b) { | |
return {a[0]-b[0], a[1]-b[1]}; | |
} | |
constexpr double pow(double num, int times){ | |
if(times == 0) return 1; | |
return num*pow(num, times-1); | |
} | |
constexpr double constexpr_sqrt(double num){ | |
double current = num; | |
double prev = 0; | |
while(current-prev != 0) { | |
prev = current; | |
current = (current+num/current)/2; | |
} | |
return current; | |
} | |
constexpr std::uint64_t fact(int num){ | |
if(num == 0) return 1; | |
return num*fact(num-1); | |
} | |
constexpr double constexpr_exp(double num){ | |
double result = 0; | |
for(int i = 0; i < 10; ++i){ | |
result += (pow(num, i)/fact(i)); | |
} | |
return result; | |
} | |
constexpr double sigmoid(Array<> _w, Array<> _ed) { | |
double num = _w*_ed; | |
return 1 / (1 + constexpr_exp(-num)); | |
} | |
constexpr Array<> pow(Array<> a, int b) { | |
return {pow(a[0], 2), pow(a[1], 2)}; | |
} | |
constexpr Array<> sqrt(Array<> a) { | |
return {constexpr_sqrt(a[0]), constexpr_sqrt(a[1])}; | |
} | |
constexpr Array<> train() { | |
constexpr dataType<double, int, 12> data = { | |
std::make_pair(1.0, 0), std::make_pair(1.15, 0), std::make_pair(1.2, 0), | |
std::make_pair(1.25, 0), std::make_pair(1.3, 0), std::make_pair(1.35, 0), | |
std::make_pair(1.4, 0), std::make_pair(1.45, 1), std::make_pair(1.5, 1), | |
std::make_pair(1.55, 1), std::make_pair(1.6, 1), std::make_pair(1.65, 1), | |
}; | |
Array<> w = {1.0, 0.0}; | |
double alpha = 0.001; | |
double pred = 0.0; | |
for(size_t time = 1; time <= 3000; ++time) { | |
for(auto& d: data) { | |
Array<> extended_data = {d.first, 1}; | |
pred = sigmoid(w, extended_data); | |
w = w-alpha*(pred-d.second)*pred*(1-pred)*extended_data; | |
} | |
} | |
return w; | |
} | |
int main(int argc, char const *argv[]) { | |
constexpr Array<> result = train(); | |
std::cout << result[0] << " " << result[1] << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment