Skip to content

Instantly share code, notes, and snippets.

View gdetor's full-sized avatar

Georgios Is. Detorakis gdetor

View GitHub Profile
@gdetor
gdetor / mnist_benchmark.py
Created December 29, 2024 19:16
Execution time benchmark MNIST for Pytorch
import time
from functools import wraps
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import numpy as np
import matplotlib.pylab as plt
import matplotlib.style as style
style.use('tableau-colorblind10')
params = {'font.size': 14,
}
plt.rcParams.update(params)
"""
This script shows how the all-prefix-sums algorithm works for an array of integers.
You can find all the details on the algorithms used here in "Chapter 1, Prefix Sums
and Their Applications, Guy E. Blelloch."
Even though we refer to the second algorithm as Parallel, we do not provide an
actual parallel implementation since we intend to show how the core computation works.
"""
import math
@gdetor
gdetor / som.py
Created January 4, 2024 02:59
Self-organizing map online algorithm
import numpy as np
import matplotlib.pylab as plt
import torch
from torch import nn
from sklearn.metrics.pairwise import euclidean_distances
from som_measures import topographic_error, quantization_error
@gdetor
gdetor / minimal_ev_algo.py
Last active July 25, 2023 21:16
A minimal evolutionary computing algorithm
import numpy as np
import matplotlib.pylab as plt
def f(x):
return 50 - x**2
if __name__ == '__main__':
np.random.seed(12345)
@gdetor
gdetor / generic_fun.c
Created February 7, 2023 00:42
Create generic type functions in C
#include <stdio.h>
#define GENERIC_MAX( type ) \
type type##_MAX(type a, type b) \
{ \
return (a > b) ? a : b; \
}
@gdetor
gdetor / sym2actual.c
Created February 7, 2023 00:40
Converts a symbolic path to an absolute one (C, bash)
#include <stdio.h>
#include <stdlib.h>
#define PATH_MAX 3000
/* Compile with -D_BSD_SOURCE */
/* sym2actual converts a path to an absolute one
*
* Args:
* symlinkpath (pointer to char): The input path string
*
@gdetor
gdetor / viewbin.c
Created February 7, 2023 00:38
View the content of a binary file
#include <stdio.h>
#include <stdlib.h>
void print_buffer(void *, int);
void catbin(char *);
int main(int argc, char **argv) {
if (argc == 2){ viewbin(argv[1]); }
else{
printf("Please type the name of the binary input file!\n");
printf("Example: viewbin foo.dat \n");
@gdetor
gdetor / close_raii.c
Created February 7, 2023 00:33
RAII in C (close a file)
#include <stdio.h>
/* #include <stdlib.h> */
static inline void fclosep(FILE **fp) { if (*fp) fclose(*fp); *fp=NULL; }
#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
void example_usage(void)
{
_cleanup_fclose_ FILE *logfile = fopen("logfile.txt", "w+");
fputs("hello logfile!", logfile);
@gdetor
gdetor / closure.c
Created February 7, 2023 00:32
Closure in C
#include <stdio.h>
typedef struct TClosure {
int (*code)(struct TClosure *env, int);
int state;
} Closure;
int call(Closure *c, int x) {
return c->code(c, x);