-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
266 lines (234 loc) · 8.04 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This file may be freely modified, used, and redistributed without restriction. */
/* Set up the repositories to get the LensKit plugin.
* This configuration pulls in things needed for the build.gradle script itself */
buildscript {
repositories {
// LensKit snapshots are published to the Sonatype snapshot repository
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
// LensKit releases are published to Maven Central
mavenCentral()
}
dependencies {
classpath 'org.lenskit:lenskit-gradle:3.0-M2'
classpath "org.yaml:snakeyaml:1.17"
}
}
apply plugin: 'java' // if you use Groovy or Scala, add those plugins
apply plugin: 'lenskit'
import org.lenskit.gradle.*
import org.yaml.snakeyaml.Yaml
/* Set up the repositories for getting LensKit and other libraries.
* These repositories are used for your Java or Groovy code, and for running LensKit.
*/
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
mavenCentral()
}
dependencies {
// Code needs to build with LensKit.
compile "org.lenskit:lenskit-all:3.0-M2"
// To run the code, we also need the LensKit CLI.
runtime "org.lenskit:lenskit-cli:3.0-M2"
// Tests use JUnit
testCompile "junit:junit:4.12"
}
/* Configure LensKit */
lenskit {
// you can configure the threadCount and maxMemory here, or on the command line
if (project.hasProperty('lenskitMaxMemory')) {
maxMemory lenskitMaxMemory
}
if (project.hasProperty('lenskitThreadCount')) {
threadCount = lenskitThreadCount.toInteger()
}
}
def dataset = (project.hasProperty("dataset"))? dataset : "ml-100k"
def dataDir = 'data/'+dataset
def zipFile = "data/"
def url = ""
def pattern = "*"
def yml = "data/"+dataset+".yml"
def datasetResultFolder = "$buildDir/results/"+dataset;
task loadDatasetYAML {
Yaml parser = new Yaml()
LinkedHashMap example = parser.load((yml as File).text)
//example.each{println it.subject}
url=example.ratings.download_url
pattern=example.ratings.zip_file_pattern
zipFile=zipFile+example.ratings.zip_file
}
/*Configure dataset values*/
task fetchData {
description 'Fetches the corresponding data set. '
dependsOn loadDatasetYAML
outputs.dir dataDir
outputs.file zipFile
doLast {
mkdir dataDir
ant {
get(src: url,
dest: zipFile,
skipExisting: true)
unzip(src: zipFile, dest: dataDir) {
patternset {
include name: pattern
}
mapper type: 'flatten'
}
}
}
}
task crossfold(type: Crossfold, group: 'evaluate') {
// download data before evaluating
dependsOn fetchData
input yml
// test on random 1/5 of each user's ratings
holdoutFraction(0.2, 'random')
// use 5-fold cross-validation
partitionCount 5
outputDir "$buildDir/crossfold/"+dataset
}
task getCrossfoldData {
doLast {
description 'Copy to use in analyze'
delete "$buildDir/crossfold.out"
copy {
from "$buildDir/crossfold/"+dataset
into "$buildDir/crossfold.out"
}
}
}
def set_hyperparameter(method,hyperparam_name,value) {
def f1 = new File("algorithms/parameters/"+method+"_"+hyperparam_name+".txt")
f1.text=value
}
def get_parameter_or_default(parameter_name,default_value) {
return (project.hasProperty(parameter_name)? project.findProperty(parameter_name):default_value)
}
task testFileTask {
doLast{
set_hyperparameter("funksvd","featcount",32)
}
}
/*Configure algorithms*/
/* Run the LensKit evaluation */
task evaluateOneAlgorithm(type: TrainTest, group: 'evaluate') {
description 'Runs the LensKit evaluation for ONE algorithm. You need to specify the algorithm (-pmethod), as well as the hyperparameters, and the filename suffix (-psuffix).'
logFile "$buildDir/evaluate.log"
logFileLevel 'DEBUG'
// we add our crossfold task as evaluation input
dependsOn getCrossfoldData
dataSet crossfold
//get parameters
def method = get_parameter_or_default("method","user-user")
def suffix = get_parameter_or_default("suffix",30)
def neighborhood = get_parameter_or_default("neighborhood",30)
def featcount = get_parameter_or_default("featcount",25)
def itercount = get_parameter_or_default("itercount",125)
// send the output to appropriate files
outputFile datasetResultFolder+"/eval-results"+suffix+".csv"
userOutputFile datasetResultFolder+"/eval-users"+suffix+".csv"
// configure our algorithms
if(method.equals("item-item")) {
//hyperparams: neighborhood
set_hyperparameter("item-item","neighborhood",neighborhood)
algorithm 'ItemItem'+neighborhood, 'algorithms/item-item.groovy'
} else if(method.equals("pers-mean")) {
algorithm 'PersMean', 'algorithms/pers-mean.groovy'
} else if(method.equals("user-user")) {
//hyperparams: neighborhood
set_hyperparameter("user-user","neighborhood",neighborhood)
algorithm 'UserUser'+neighborhood, 'algorithms/user-user.groovy'
} else if(method.equals("funksvd")) {
//hyperparams: featcount, itercount
set_hyperparameter("funksvd","featcount",featcount)
set_hyperparameter("funksvd","itercount",itercount)
algorithm 'FunkSVD'+featcount+"_"+itercount, 'algorithms/funksvd.groovy'
println "Loaded itercount: itercount"
}
// and some evaluation tasks and metrics
predict {
metric 'rmse'
metric 'ndcg'
}
recommend {
listSize 10
metric 'mrr'
}
}
task evaluate(type: TrainTest, group: 'evaluate') {
description 'Runs the LensKit evaluation.'
logFile "$buildDir/evaluate.log"
logFileLevel 'DEBUG'
// we add our crossfold task as evaluation input
dependsOn getCrossfoldData
dataSet crossfold
// send the output to appropriate files
outputFile datasetResultFolder+"/eval-results.csv"
userOutputFile datasetResultFolder+"/eval-users.csv"
// configure our algorithms
algorithm 'ItemItem', 'algorithms/item-item.groovy'
algorithm 'PersMean', 'algorithms/pers-mean.groovy'
algorithm 'UserUser', 'algorithms/user-user.groovy'
algorithm 'FunkSVD', 'algorithms/funksvd.groovy'
// and some evaluation tasks and metrics
predict {
metric 'rmse'
metric 'ndcg'
}
recommend {
listSize 10
metric 'mrr'
}
}
task evaluateOutputs {
dependsOn evaluate
doLast{
description 'Copy to use in analyze'
copy {
from datasetResultFolder+"/eval-results.csv"
into "$buildDir"
}
copy {
from datasetResultFolder+"/eval-users.csv"
into "$buildDir"
}
}
}
task analyzeResults(type: Exec, group: 'evaluate') {
description 'Post-processes evaluation results to draw charts.'
dependsOn evaluateOutputs
inputs.files "$buildDir/eval-results.csv", 'analyze-output.ipynb'
outputs.file datasetResultFolder+"/analysis.html"
/* run Jupyter/IPython. Location can be overridden with -Pipython.bin=/usr/bin/ipython */
if (project.hasProperty('ipython.bin')) {
executable project.getProperty('ipython.bin')
} else {
executable 'jupyter'
}
args 'nbconvert', '--to', 'html', '--execute'
args '--output', file(datasetResultFolder+"/analysis.html")
args file("analyze-output.ipynb")
doLast {
delete "$buildDir/eval-results.csv"
delete "$buildDir/eval-users.csv"
}
}
task analyzeAllResults(type: Exec, group: 'evaluate') {
description 'Post-processes evaluation results to draw charts from data-analysis folder'
inputs.files 'analyze-output.ipynb'
outputs.file "data-analysis/analysis.html"
/* run Jupyter/IPython. Location can be overridden with -Pipython.bin=/usr/bin/ipython */
if (project.hasProperty('ipython.bin')) {
executable project.getProperty('ipython.bin')
} else {
executable 'jupyter'
}
args 'nbconvert', '--to', 'html', '--execute'
args '--output', file("data-analysis/analysis.html")
args file("analyze-output.ipynb")
}