Skip to content

Instantly share code, notes, and snippets.

@att288
att288 / load_keras_django_naive.py
Created May 4, 2019 17:06
load_keras_django_naive.py
import json
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from keras import backend as K
from keras.models import load_model
from rest_framework.decorators import api_view
@att288
att288 / load_single_keras_django.py
Created May 4, 2019 17:08
load_single_keras_django.py
def _load_model_from_path(path):
global gGraph
global gModel
gGraph = tf.get_default_graph()
gModel = load_model(path) # keras function
_load_model_from_path('path_to_model')
@att288
att288 / load_multiple_keras_models_django.py
Created May 4, 2019 17:24
load_multiple_keras_models_django.py
###################################################
# settings.py/the file where to load the models
###################################################
def load_model_from_path(path):
graph = tf.get_default_graph()
model = load_model(path)
return graph, model
def load_all_models():
global gModelObjs # each object is a tuple of graph, model
@att288
att288 / fibonacci_sequence_generator.py
Last active May 5, 2019 20:36
fibonacci_sequence_generator.py
def fibonacci_sequence(n):
curr_val = 0
next_val = 1
for i in range(n):
yield curr_val
curr_val, next_val = next_val, curr_val + next_val
if __name__ == '__main__':
fib_seq = []
for fn in fibonacci_sequence(10):
@att288
att288 / generator_send.py
Last active May 5, 2019 21:19
generator_send.py
def multiplier(n):
step = None
result = 1
for i in range(1, n+1):
if step is None:
step = 1
result *= (i*step)
print(f"i:{i}, step={step}")
step = yield result
@att288
att288 / connect_generators.py
Last active May 5, 2019 21:49
connect_generators.py
def my_gen_1(n):
for i in range(n):
yield f'g1: {i}'
def my_gen_2(n):
for j in range(n):
yield f'g2: {-j}'
def my_gen_master(n):
yield from my_gen_1(n)
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
fn_2 = 0
fn_1 = 1
for i in range(n-1):
# recursion
def fib(n):
if n==0:
return 0
if n==1:
return 1
return fib(n-1) + fib(n-2)
# DP
fib_seq = [0, 1]
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
if len(fib_seq) == n-1:
class Example:
@classmethod
def foo(cls, arg1, arg2, ...):
pass