aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-08-16 08:31:33 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-08-16 08:31:33 -0500
commitba9a7343ad22d507cfaed6b7bb8618a99c2f2976 (patch)
tree14f5be8b79e89ad3e4aa0a48773a9bf078eb2e0f
parentc888c09e8ecb166efa01929c0f27d5085929a2b2 (diff)
add: Cli defined
-rw-r--r--src/main.c50
-rw-r--r--src/util.c33
-rw-r--r--src/util.h6
3 files changed, 69 insertions, 20 deletions
diff --git a/src/main.c b/src/main.c
index 3bbfd5c..683447f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
#include <getopt.h>
#include <json-c/json.h>
+#include "util.h"
#include "nn.h"
const size_t MAX_FILE_SIZE = 1<<29; // 0.5 GiB
@@ -26,7 +27,7 @@ static void usage(int exit_code)
{
FILE *fp = (!exit_code) ? stdout : stderr;
fprintf(fp,
- "Usage: ml train [Options] {[-i INPUT]}... {[-l LABEL]}... JSON_FILE\n"
+ "Usage: ml train [Options] JSON_FILE\n"
" or: ml predict [-o FILE] FILE\n"
"Train and predict json data\n"
"\n"
@@ -34,12 +35,10 @@ static void usage(int exit_code)
" -a, --alpha=ALPHA Learning rate (only works with train) [default: 1e-5]\n"
" -e, --epochs=EPOCHS Number of epochs to train the model (only works with train)\n"
" [default: 100]\n"
- " -i INPUT Input key from json file (only works with train)\n"
- " -l LABEL Label key from json file (only works with train)\n"
" -o, --output FILE Output file (only works with predict)\n"
"\n"
"Examples:\n"
- " $ ml train -i area -i latitude -i longitude -l price housing.json\n"
+ " $ ml train -e 150 -a 1e-4 housing.json\n"
" $ ml predict housing.json -o predictions.json\n"
);
exit(exit_code);
@@ -117,10 +116,14 @@ json_read_error:
}
int main(int argc, char *argv[]) {
+
char **input_keys, **label_keys;
- char *out_filename, *in_filename;
- double epochs = 100, alpha = 1e-5;
+ int in_key_size = 0, label_key_size = 0;
+ char *out_filename = NULL, *in_filename = NULL;
+ size_t epochs = 100;
+ double alpha = 1e-5;
+ if (argc <= 1) usage(1);
static struct option long_opts[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
@@ -133,34 +136,41 @@ int main(int argc, char *argv[]) {
while (1) {
c = getopt_long(argc, argv, "hve:a:o:i:l:", long_opts, NULL);
- printf("optind: %d\n", optind);
+
if (c == -1) {
break;
}
switch (c) {
- case 'h':
- usage(0);
- case 'v':
- version();
case 'e':
- printf("epochs: %s\n", optarg);
+ epochs = (size_t)atol(optarg);
break;
case 'a':
- printf("alpha: %s\n", optarg);
+ alpha = (double)atof(optarg);
break;
case 'o':
- printf("output: '%s'\n", optarg);
- break;
- case 'i':
- printf("input: '%s'\n", optarg);
- break;
- case 'l':
- printf("label: '%s'\n", optarg);
+ out_filename = optarg;
break;
+ case 'h':
+ usage(0);
+ case 'v':
+ version();
default:
usage(1);
}
}
+ argv += optind;
+ argc -= optind;
+ if (argc != 2) usage(1);
+
+ in_filename = argv[1];
+
+ if (!strcmp(argv[0], "train")) {
+ printf("train command\n");
+ printf("in_filename: '%s'\n", in_filename);
+ } else if (!strcmp(argv[0], "predict")) {
+ } else {
+ usage(1);
+ }
return 0;
}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..4a8c3ef
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,33 @@
+/* See LICENSE file for copyright and license details. */
+#include <string.h>
+
+#include "util.h"
+
+void
+die(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ fputc('\n', stderr);
+ }
+
+ exit(1);
+}
+
+void *
+ecalloc(size_t nmemb, size_t size)
+{
+ void *p;
+
+ if (!(p = calloc(nmemb, size)))
+ die("calloc:");
+ return p;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..4068691
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,6 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void die(const char *fmt, ...);
+void *ecalloc(size_t nmemb, size_t size);
Feel free to download, copy and edit any repo