This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct map_argument { | |
void** things; | |
void** results; | |
void* (*f)(void*); | |
int from; | |
int to; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |