Skip to content

Instantly share code, notes, and snippets.

@cosmicexplorer
Last active February 2, 2016 06:39
Show Gist options
  • Save cosmicexplorer/bf39a1b1e0b5e3bb6bd0 to your computer and use it in GitHub Desktop.
Save cosmicexplorer/bf39a1b1e0b5e3bb6bd0 to your computer and use it in GitHub Desktop.
makefile with lots of fun magic and dollar signs
.PHONY: all train test clean
SRC_DIR := cs362
IN_JAVA := $(wildcard $(SRC_DIR)/*.java)
OUT_JAVA := $(IN_JAVA:.java=.class)
all_past_n = $(wordlist $(1),$(words $(2)),$(2))
JAVAC_OPTS := -Xdiags:verbose
JAVA_CP := "commons-cli-1.2.jar:commons-math3-3.2.jar:."
JAVA_MAIN := cs362.Learn
# javac compiles all these files at once (it leads to race condition errors if
# this is not done and -j is used; also it's much faster when multiple files
# need to be built). so we only depend on the first file, which then depends
# upon all input java files; this ensures the $(OUT_JAVA): target is a
# bottleneck and only run once at a time in parallel builds
compile_java: $(word 1,$(OUT_JAVA))
$(OUT_JAVA): Makefile $(IN_JAVA)
# ignore Makefile dep
javac -cp $(JAVA_CP) $(call all_past_n,2,$^) $(JAVAC_OPTS)
all: compile_java
DATA_DIR := data
define make_model_with_alg
MODELS += $(patsubst %.train,%-$(1).model,$(DATA_DIR)/$(2).train)
PREDICTIONS += $(patsubst %.train,%-$(1).predictions,$(DATA_DIR)/$(2).train)
%$(2)-$(1).model: %$(2).train compile_java $(FORCE_MODEL)
java -cp $(JAVA_CP) $(JAVA_MAIN) \
-mode train -algorithm $(1) \
-model_file $$@ -data $$< -task regression $(3)
%$(2)-$(1).predictions: %$(2)-$(1).model %$(2).dev compile_java $(FORCE_PRED)
java -cp $(JAVA_CP) $(JAVA_MAIN) \
-mode test -model_file $$< -data $$(word 2,$$^) \
-predictions_file $$@ -task regression
endef
LINREG_DATA := easy hard nasdaq
BAYES_DATA := bio easy hard
BAYES_ARGS := -lambda 1.0
PERCEPTRON_DATA := bio easy hard
$(foreach data,$(LINREG_DATA), \
$(eval $(call make_model_with_alg,linear_regression,$(data))))
$(foreach data,$(BAYES_DATA), \
$(eval $(call make_model_with_alg,naive_bayes,$(data),$(BAYES_ARGS))))
$(foreach data,$(PERCEPTRON_DATA), \
$(eval $(call make_model_with_alg,perceptron,$(data))))
train: FORCE_MODEL = .FORCE
train: $(MODELS)
test: FORCE_PRED = .FORCE
test: $(PREDICTIONS)
clean:
rm -f $(PREDICTIONS) $(MODELS) $(OUT_JAVA)
.FORCE:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment