This file contains 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
from multiprocessing import Queue, cpu_count, Process | |
def mp_data_generator(): | |
def producer(q): | |
for _ in range(10): | |
# Simulating fetching | |
# from disk / network | |
time.sleep(0.5) | |
# Simulate computation | |
for _ in range(10000000): |
This file contains 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
// importing Python C API Header | |
#include <Python.h> | |
#include <vector> | |
static PyObject *count(PyObject *self, PyObject *args){ | |
long num; | |
if (!PyArg_ParseTuple(args, "l", &num)) | |
return NULL; | |
long result = 0L; |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
tf_train_step_signature = tf.function(train_step, | |
input_signature=[ | |
tf.TensorSpec(shape=(None, None, 20), dtype=tf.float32), | |
tf.TensorSpec(shape=(None,), dtype=tf.float32) | |
] | |
) |
This file contains 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
def train_step(inputs, targets): | |
with tf.GradientTape() as tape: | |
preds = model(inputs, training=True) | |
loss_value = loss(preds, targets) | |
grad = tape.gradient(loss_value, model.trainable_variables) | |
optimizer.apply_gradients(zip(grad, model.trainable_variables)) | |
def train(dataset, train_step): | |
for inputs, target in dataset: | |
train_step(inputs, target) |
This file contains 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
import numpy as np | |
def data_gen(max_len, n_dim): | |
def gen(): | |
while True: | |
seq_len = np.random.randint(1, max_len+1) | |
yield np.random.random((seq_len, n_dim)).astype(np.float32), np.random.random() | |
return gen | |
dataset = tf.data.Dataset.from_generator( |
This file contains 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
import tensorflow as tf | |
class MyCustomModel(tf.keras.Model): | |
def __init__(self): | |
super(MyCustomModel, self).__init__() | |
num_hidden_layer_units = [10, 20, 40, 20, 10] | |
self.hidden_layers = [tf.keras.layers.Dense(unit, activation='relu') for unit in num_hidden_layer_units] | |
self.final = tf.keras.layers.Dense(1) | |
def call(self, inputs): | |
net = tf.math.reduce_max(inputs, axis=1, keepdims=True) |
This file contains 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
def run(tasks): | |
# List of sockets which are waiting for data to arrive | |
# along with their corresponding task | |
waiting_to_read = {} | |
# Main event loop, continue to process events while they exist. | |
while any([tasks, waiting_to_read]): | |
# If there are no tasks in the queue, check for sockets | |
# on which data has arrived. | |
while not tasks: | |
ready_to_read, _, _ = select.select( |
This file contains 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
import time | |
import urllib.request | |
from concurrent.futures import ThreadPoolExecutor | |
def call_downstream(): | |
return urllib.request.urlopen('http://localhost:8080/get_resource').read() | |
executor = ThreadPoolExecutor() |
This file contains 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
import urllib.request | |
import time | |
def call_downstream(): | |
return urllib.request.urlopen('http://localhost:8080/get_resource').read() | |
start = time.time() | |
call_downstream() |
NewerOlder