Skip to content

Instantly share code, notes, and snippets.

View StrikingLoo's full-sized avatar
😄

Luciano StrikingLoo

😄
View GitHub Profile
void** concurrent_map(void** things, void* (*f)(void*), int length,
int nthreads){
void** results = malloc(sizeof(void*)*length);
struct map_argument arguments[nthreads];
pthread_t threads[nthreads];
int chunk_size = length/nthreads;
for(int j = 0 ; j < nthreads; j++){
int from = j*chunk_size;
void* chunk_map(void* argument){
struct map_argument* arg = (struct map_argument*) argument;
for(int i = arg->from; i < arg->to; i++){
arg->results[i] = (*(arg->f))(arg->things[i]);
}
return NULL;
}
struct map_argument {
void** things;
void** results;
void* (*f)(void*);
int from;
int to;
};
// returns 1 if the number is a prime, 0 otherwise
void* naivePrime(void* number){
int n = *((int*) number);
int* res = malloc(sizeof(int));
*res = 1;
for(int i = 2; i < n; i++){
if(n%i==0){
*res=0;
return res;
}
// generic single-threaded map implementation using void* and pointer arithmetics.
void** map(void** things, void* (*f)(void*), int length){
void** results = malloc(sizeof(void*)*length);
for(int i = 0; i < length; i++){
void* thing = things[i];
void* result = (*f)(thing);
results[i] = result;
}
chars_np = np.asarray(chars)
def output_idx(i):
return np.argmax(model.predict([[X[i]]])[0], 1)
def output_str(i):
return ''.join(list(chars_np[output_idx(i)]))
output_str(0)
customAdam = keras.optimizers.Adam(lr=0.0001)
model.compile(optimizer=customAdam,
# Loss function to minimize
loss="binary_crossentropy",
# List of metrics to monitor
metrics=["mean_squared_error","binary_crossentropy"])
es = keras.callbacks.EarlyStopping(monitor='loss', mode='min', verbose=1, patience = 5)
print('# Fit model on training data')
UNITS = 100
TOTAL_OUTPUT = SEQ_LENGTH*VOCAB_SIZE
inputs = keras.Input(shape=(SEQ_LENGTH,VOCAB_SIZE), name='sentences')
x = layers.LSTM(units = UNITS, name='LSTM_layer_1', return_sequences=True)(inputs)
x = layers.LSTM(units = UNITS, name='LSTM_layer_2', return_sequences=True)(x)
x = layers.TimeDistributed(layers.Dense(VOCAB_SIZE))(x)
outputs = layers.Dense(VOCAB_SIZE, activation='softmax', name='predicted_sentence')(x)
outputs = layers.Reshape((SEQ_LENGTH,VOCAB_SIZE))(outputs)
model = keras.Model(inputs=inputs, outputs=outputs)
chars = list(set(corpus))
VOCAB_SIZE = len(chars)
char_to_ix = {char:ix for ix, char in enumerate(chars)}
SEQ_LENGTH = 50
slices = len(corpus)//SEQ_LENGTH
X = np.zeros((slices, SEQ_LENGTH, VOCAB_SIZE))
y = np.zeros((slices, SEQ_LENGTH, VOCAB_SIZE))
for i in range(0, slices):
file_names = glob.glob('lovecraft_corpus/*.txt')
corpus = ""
for file_name in file_names:
with open(file_name, 'r') as f:
corpus+=f.read()
valid_chars = valid_chars = [' ', '!', '\"', '#', '&', "\'", '(', ')', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '?', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
corpus = ''.join([c if c in valid_chars else '' for c in corpus])
len(corpus) #2881584 characters