Commit 2366299b authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Set up the prediction code

parent 1f1732eb
......@@ -47,3 +47,4 @@ include %reldir%/mcts/Makefile.am
include %reldir%/simulation/Makefile.am
include %reldir%/xml/Makefile.am
include %reldir%/features/Makefile.am
include %reldir%/cnn/Makefile.am
# src/libtarot/cnn/Makefile.am
#
# Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
tarotinclude_HEADERS += %reldir%/tarot/cnn.h
src_libtarot_libtarot_la_SOURCES += \
%reldir%/cnn.c \
%reldir%/tarot/cnn_private.h \
%reldir%/tarot/cnn_private_impl.h \
%reldir%/tarot/first_filters.h \
%reldir%/tarot/second_filters.h \
%reldir%/tarot/fully_connected.h
INTROSPECTED_SOURCES += %reldir%/cnn.c
/**
* file tarot/cnn.c Convolutional Neural Network
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <tarot/cnn_private.h>
#include <tarot/game_private.h>
#include <tarot/features_private.h>
#include <stdlib.h>
#include <assert.h>
#include "xalloc.h"
size_t
tarot_cnn_predict (const TarotGame * game, size_t max_score, double *score)
{
return cnn_predict (game, max_score, score);
}
void
tarot_cnn_predict_alloc (const TarotGame * game, double **score,
size_t *n_score)
{
size_t n = cnn_predict (game, 0, NULL);
*score = xmalloc (n * sizeof (double));
n = cnn_predict (game, *n_score, *score);
assert (n == *n_score);
}
#include <tarot/cnn_private_impl.h>
#include <tarot/game_private_impl.h>
#include <tarot/features_private_impl.h>
/*
* file tarot/cnn.h Convolutional Neural Network.
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_CNN_INCLUDED
#define H_TAROT_CNN_INCLUDED
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct TarotGame;
/**
* tarot_cnn_predict:
* @score: (array length=max_score):
*/
size_t tarot_cnn_predict (const TarotGame *game, size_t max_score, double *score);
/**
* tarot_cnn_predict_alloc:
* @score: (out) (array length=n_score):
* @n_score: (out):
*/
void tarot_cnn_predict_alloc (const TarotGame *game, double **score, size_t *n_score);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_CNN_INCLUDED */
/**
* file tarot/cnn_private.h Convolutional Neural Network
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_CNN_PRIVATE_INCLUDED
#define H_TAROT_CNN_PRIVATE_INCLUDED
#include <tarot_private.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
static size_t cnn_predict (const TarotGame * game, size_t max_score,
double *score);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_CNN_PRIVATE_INCLUDED */
/*
* file tarot/cnn_private_impl.h
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_CNN_PRIVATE_IMPL_INCLUDED
#define H_TAROT_CNN_PRIVATE_IMPL_INCLUDED
#include <tarot/features_private.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <float.h>
static size_t
cnn_predict (const TarotGame * game, size_t max_score, double *score)
{
double features_noshape[92 * 6 * 32] = { 0 };
size_t input_dim = features_cnn_add (game, 92 * 6 * 32, features_noshape);
assert (input_dim == 92 * 6 * 32);
double first_filters[5][5][5][15] =
#include "first_filters.h"
;
double first_filters_out[5][32][6][92];
size_t i, j, k, l;
size_t full_index;
double features[32][6][92];
full_index = 0;
for (i = 0; i < 32; i++)
{
for (j = 0; j < 6; j++)
{
for (k = 0; k < 92; k++)
{
features[i][j][k] = features_noshape[full_index++];
}
}
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 32; j++)
{
for (k = 0; k < 6; k++)
{
for (l = 0; l < 92; l++)
{
int m, n, o;
double out = 0;
for (m = -2; m <= 2; m++)
{
for (n = -2; n <= 2; n++)
{
for (o = -7; o <= 7; o++)
{
int i1 = j + m;
int i2 = k + n;
int i3 = l + o;
if (i1 >= 0 && i2 >= 0 && i3 >= 0
&& i1 < 32 && i2 < 6 && i3 < 92)
{
out +=
features[i1][i2][i3] *
first_filters[i][m + 2][n + 2][o + 7];
}
}
}
}
if (out >= 0)
{
first_filters_out[i][j][k][l] = out;
}
else
{
first_filters_out[i][j][k][l] = 0;
}
}
}
}
}
double first_max_pooling_out[5][8][3][24];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 8; j++)
{
for (k = 0; k < 3; k++)
{
for (l = 0; l < 24; l++)
{
size_t m, n, o;
double out = 0;
for (m = 0; m < 4; m++)
{
for (n = 0; n < 2; n++)
{
for (o = 0; o < 4; o++)
{
size_t i1 = j * 4 + m;
size_t i2 = k * 2 + n;
size_t i3 = l * 4 + o;
if (first_filters_out[i][i1][i2][i3] > out)
{
out = first_filters_out[i][i1][i2][i3];
}
}
}
}
first_max_pooling_out[i][j][k][l] = out;
}
}
}
}
double second_filters[5][5][3][5] =
#include "second_filters.h"
;
double second_filters_out[5][4][1][20];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 1; k++)
{
for (l = 0; l < 20; l++)
{
size_t m, n, o, p;
double out = 0;
for (m = 0; m < 5; m++)
{
for (n = 0; n < 5; n++)
{
for (o = 0; o < 3; o++)
{
for (p = 0; p < 5; p++)
{
size_t i2 = j + n;
size_t i3 = k + o;
size_t i4 = l + p;
double filter_value =
second_filters[m][n][o][p];
out +=
(first_max_pooling_out[m][i2][i3][i4]
* filter_value);
}
}
}
}
if (out >= 0)
{
second_filters_out[i][j][k][l] = out;
}
else
{
second_filters_out[i][j][k][l] = 0;
}
}
}
}
}
double second_max_pooling_out[100];
full_index = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
for (k = 0; k < 1; k++)
{
for (l = 0; l < 10; l++)
{
size_t m, n, o;
double out = 0;
for (m = 0; m < 2; m++)
{
for (n = 0; n < 1; n++)
{
for (o = 0; o < 2; o++)
{
size_t i1 = j * 2 + m;
size_t i2 = k * 1 + n;
size_t i3 = l * 2 + o;
if (second_filters_out[i][i1][i2][i3] > out)
{
out = second_filters_out[i][i1][i2][i3];
}
}
}
}
second_max_pooling_out[full_index++] = out;
}
}
}
}
double fc[6][100] =
#include "fully_connected.h"
;
double final_output[6];
for (i = 0; i < 6; i++)
{
double out = 0;
for (j = 0; j < 100; j++)
{
out += fc[i][j] * second_max_pooling_out[j];
}
final_output[i] = out;
}
size_t n_players = tarot_game_n_players (game);
for (i = 0; i < max_score && i < n_players; i++)
{
score[i] = final_output[i];
}
return n_players;
}
#include <tarot/features_private_impl.h>
#endif /* H_TAROT_CNN_PRIVATE_IMPL_INCLUDED */
This diff is collapsed.
{
{
0.263651663670316, 0.105249198619276, 0.744604940293357,
0.215417892672122, 0.153435340151191, 0.578695559641346,
0.546770384302363, 0.0669839791953564, 0.101015117019415,
0.562004002043977, 0.643459548708051, 0.462663786252961,
0.00266541517339647, 0.280685898382217, 0.625420142430812,
0.799281451152638, 0.222176748560742, 0.640243295114487,
0.0389061309397221, 0.471551111433655, 0.302689241012558,
0.649039379553869, 0.946781895356253, 0.38684919080697,
0.831065296195447, 0.174257177393883, 0.263792181620374,
0.12091846531257, 0.382689703721553, 0.778471826575696,
0.965131811564788, 0.907174363499507, 0.296722571831197,
0.0948711666278541, 0.206468131626025, 0.980158125748858,
0.515059519559145, 0.777726802276447, 0.943464905954897,
0.640600300161168, 0.224656070349738, 0.101542372955009,
0.0234877506736666, 0.606864946428686, 0.614768577739596,
0.390715143177658, 0.0819758966099471, 0.0387260161805898,
0.122798552503809, 0.934490323299542, 0.974139056168497,
0.457172745838761, 0.542035990161821, 0.376783764222637,
0.103623520815745, 0.515934705035761, 0.422363598598167,
0.676665870007128, 0.463260676013306, 0.708966347854584,
0.559256228618324, 0.518735450692475, 0.602597216609865,
0.425370702520013, 0.616739343851805, 0.775227749953046,
0.126981767592952, 0.56034957501106, 0.471271046437323,
0.445600428618491, 0.399756523314863, 0.23066892940551,
0.215494680218399, 0.16015611239709, 0.574572486570105,
0.199025131994858, 0.735528585501015, 0.00174418278038502,
0.417645840439945, 0.999304570490494, 0.587052582763135,
0.0200556314084679, 0.614003004971892, 0.511644412763417,
0.486594901885837, 0.133495326386765, 0.737640427891165,
0.332766976673156, 0.422223367961124, 0.0312007507309318,
0.04561204649508, 0.511696964967996, 0.168499801307917,
0.275726893451065, 0.0701850967016071, 0.848961519310251,
0.258389025693759, 0.809747370891273, 0.775579683482647,
0.472943624481559}
,
{
0.097874408820644, 0.462731699226424, 0.587859204504639,
0.538624270120636, 0.651384241180494, 0.229971275432035,
0.378907423466444, 0.626245308434591, 0.130144245922565,
0.219654048793018, 0.986257058102638, 0.81172066857107,
0.297872397582978, 0.774649540893734, 0.223085005534813,
0.450436199316755, 0.713994901161641, 0.5555507207755,
0.307191573781893, 0.870186279527843, 0.373181107686833,
0.114895354257897, 0.7691751930397, 0.687893435126171,
0.38259216514416, 0.00500500621274114, 0.858683497644961,
0.404971019132063, 0.707709568319842, 0.272623640717939,
0.20105343637988, 0.264665837865323, 0.465090604731813,
0.303537430241704, 0.0609002418350428, 0.0504934876225889,
0.990352052031085, 0.249432903947309, 0.420930880354717,
0.340007428778335, 0.5776808608789, 0.0679493679199368,
0.755329283885658, 0.690391244599596, 0.439437226159498,
0.577364274067804, 0.126637895824388, 0.279401805251837,
0.0236758396495134, 0.478046403033659, 0.143654890591279,
0.422251904616132, 0.899082988267764, 0.186202579177916,
0.969598609954119, 0.0080176426563412, 0.297885052626953,
0.174175817752257, 0.503467896021903, 0.058633737033233,
0.755342147313058, 0.271265023155138, 0.86372963571921,
0.900328499497846, 0.216247229138389, 0.426246648421511,
0.609716134378687, 0.0770811806432903, 0.0823007852304727,
0.55309400614351, 0.591962239239365, 0.195860608946532,
0.718546779360622, 0.400497081922367, 0.158409433439374,
0.00440894323401153, 0.807065123692155, 0.717599287861958,
0.759173343889415, 0.777351864846423, 0.475849307142198,
0.0692153100389987, 0.303205183241516, 0.259307625470683,
0.465820004697889, 0.681341353571042, 0.162530737230554,
0.3315863115713, 0.658539676340297, 0.124418905936182,
0.641117241233587, 0.923606103053316, 0.809590051649138,
0.257430481957272, 0.0600384240970016, 0.189648887142539,
0.679506092099473, 0.994046133942902, 0.0726664692629129,
0.00785458227619529}
,
{
0.673194895498455, 0.399783695815131, 0.753779386403039,
0.417119642021134, 0.794403780950233, 0.158995585516095,
0.316654670750722, 0.0783972709905356, 0.636912218527868,
0.612548421602696, 0.967475160490721, 0.217684811446816,
0.11110937455669, 0.520486841443926, 0.942120139487088,
0.151384921744466, 0.872657116735354, 0.0387787881772965,
0.0815757869277149, 0.165870951255783, 0.471671406412497,
0.736385132418945, 0.622524855192751, 0.028793471865356,
0.519667379790917, 0.593978675547987, 0.678756370674819,
0.655512686818838, 0.130158879328519, 0.752131609711796,
0.184048459399492, 0.604403417557478, 0.27051350614056,
0.963967527728528, 0.332167807966471, 0.0125368849840015,
0.519466396421194, 0.93450183654204, 0.839593031676486,
0.698299503186718, 0.696962913032621, 0.959792926441878,
0.790323931258172, 0.126855689566582, 0.76317058946006,
0.190608815522864, 0.823164564790204, 0.476708648493513,
0.565590304555371, 0.900369515176862, 0.446481223450974,
0.668622281402349, 0.623048865934834, 0.024528166744858,
0.764200457138941, 0.616635400801897, 0.864079612540081,
0.785565724363551, 0.700472829397768, 0.588832657551393,
0.260776451556012, 0.980964586371556, 0.129802127135918,
0.434388930210844, 0.431986827170476, 0.109981863992289,
0.152973408112302, 0.794766302686185, 0.525937785860151,
0.0241602973546833, 0.0872119306586683, 0.117835569661111,
0.405183600028977, 0.538363397819921, 0.55323070124723,
0.363384932279587, 0.427423163549975, 0.925696173217148,
0.865805653389543, 0.0211085202172399, 0.54916608450003,
0.00576757593080401, 0.369096222566441, 0.485861075110734,
0.971170965814963, 0.0596857978962362, 0.293328213039786,
0.840538906631991, 0.165298456791788, 0.410420997999609,
0.734565482940525, 0.519759006565437, 0.995988487266004,
0.191634104819968, 0.122068796306849, 0.193352384259924,
0.389151367591694, 0.127207423327491, 0.529182572383434,
0.191402857890353}
,
{
0.9531698836945, 0.535932663595304, 0.947059206897393, 0.625590123701841,
0.945058590732515, 0.97517697326839, 0.57664468488656,
0.419502158882096, 0.0185727118514478, 0.790607315255329,
0.447259192587808, 0.851092496654019, 0.807941324543208,
0.951499760616571, 0.358506965916604, 0.708548852941021,
0.769208092708141, 0.154278667178005, 0.10724711092189,
0.438802414108068, 0.381212475243956, 0.405484271468595,
0.452529021073133, 0.6379026141949, 0.444399106316268,
0.869749018223956, 0.221252062590793, 0.827113367151469,
0.141391274053603, 0.15034171869047, 0.870336322113872,
0.891077547334135, 0.563291769474745, 0.353905509924516,
0.624009064864367, 0.693946255138144, 0.897827605949715,
0.18971412954852, 0.610238002147526, 0.71526803332381,
0.190885927528143, 0.112628571689129, 0.510833697626367,
0.344181762542576, 0.85283493087627, 0.659428725717589,
0.270318103488535, 0.00565324490889907, 0.419336627004668,
0.349025448784232, 0.0186233830172569, 0.323050158564001,
0.712710046209395, 0.804844955215231, 0.0531623840797693,
0.782567491289228, 0.303377624368295, 0.823014636524022,
0.805768594378605, 0.441028552362695, 0.955813688924536,
0.494286116678268, 0.0182494060136378, 0.0225219517014921,
0.46387910656631, 0.687106558354571, 0.854633205570281,
0.722025202121586, 0.0954627795144916, 0.289359803544357,
0.921230209991336, 0.427999468985945, 0.174602883169428,
0.995006264420226, 0.526536656776443, 0.195899005280808,
0.710152546875179, 0.183539561461657, 0.353663068264723,
0.477110689971596, 0.834386477479711, 0.334996455349028,
0.607541033998132, 0.676779257366434, 0.851272124331445,
0.212571184150875, 0.172617212869227, 0.508911204058677,
0.405589418951422, 0.635631131473929, 0.611441552173346,
0.271433789050207, 0.161990171764046, 0.276844876119867,
0.175747223664075, 0.865518645616248, 0.495430232025683,
0.0281702268403023, 0.661746475147083, 0.900061255320907}
,
{
0.59112447896041, 0.444016672903672, 0.245465626008809, 0.54158777045086,
0.452112084021792, 0.552023957017809, 0.802951224846765,
0.400967316003516, 0.901719536399469, 0.946039009140804,
0.366432680748403, 0.201664315769449, 0.59246712597087,
0.85211847606115, 0.143666664371267, 0.269787198165432,
0.0749373096041381, 0.213918520603329, 0.234198854770511,
0.715956015046686, 0.303782909642905, 0.0296715314034373,
0.529404437635094, 0.390481288777664, 0.501866272417828,
0.427308136830106, 0.870457975659519, 0.210664412938058,
0.038605121197179, 0.407960836775601, 0.984767082612962,
0.490844853688031, 0.828660155180842, 0.875939830439165,
0.981628121575341, 0.969404534902424, 0.73533609113656,
0.889404741581529, 0.676818241830915, 0.788778482470661,
0.756962252780795, 0.0223024962469935, 0.979092296445742,
0.886189316399395, 0.380334181012586, 0.464815365849063,
0.180780577939004, 0.757034917129204, 0.0393771638628095,
0.537408105330542, 0.999910312704742, 0.257119365036488,
0.629730414832011, 0.301125145982951, 0.482242173748091,
0.420118271606043, 0.609629075042903, 0.720422425772995,
0.27005881164223, 0.921293998137116, 0.0951801759656519,
0.0735787726007402, 0.554327541263774, 0.0918790379073471,
0.516475439537317, 0.101357686566189, 0.816779092652723,
0.776143685216084, 0.224324367474765, 0.0851805182173848,
0.593808056088164, 0.485213942360133, 0.131053277291358,
0.00284210243262351, 0.167911211727187, 0.1983051544521,
0.530765063827857, 0.0465932472143322, 0.316761933034286,
0.962087246589363, 0.791152755031362, 0.260008341632783,
0.570930656045675, 0.895368174416944, 0.3741359943524,
0.394990341737866, 0.44077640818432, 0.929511001799256,
0.873191928258166, 0.362396025797352, 0.348213847260922,
0.422118700342253, 0.826811002101749, 0.734413098311052,
0.150340433465317, 0.172394964145496, 0.199633224401623,
0.592963539296761, 0.564709898550063, 0.0819722099695355}