Skip to content

Instantly share code, notes, and snippets.

@braingineer
braingineer / sieve.py
Created January 14, 2016 20:42
Sieve of Eratosthenes
"""
Sieve of Eratosthenes (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) using list comprehensions
"""
upper_bound = 100
noprimes = {j for i in range(2,8) for j in range(i*2, upper_bound, i)}
primes = [x for x in range(2, upper_bound) if x not in noprimes]
@braingineer
braingineer / maxtime_process.py
Created January 17, 2016 01:06
Wraps a function in a process that terminates after s seconds. returns None if process terminates else returns result.
"""
Inspired from [1] and [2], but neither fit my need.
The following lets you decorate a function with a time limit, kills it when it doesn't finish in that time.
The needed part for me was that I had to get the result if it didn't get killed and None if it did.
The following accomplished this.
[1] http://stackoverflow.com/a/26664130
[2] https://gist.github.com/aaronchall/6331661fe0185c30a0b4
@braingineer
braingineer / accmask_mwe.py
Created April 11, 2016 15:16
Tensor Accuracy w/ Mask
import numpy as np
import theano.tensor as T
pred = np.arange(30, dtype=np.float32).reshape(2,3,5)
pred /= pred.sum(axis=-1, keepdims=True)
### target matrix to match pred
target1 = np.ones_like(pred)
target1[:,:,:-1] = 0
@braingineer
braingineer / embedding_dim_mwe.py
Last active April 14, 2016 16:24
minimum working example for how an embedding re-use can mess things up
from __future__ import print_function
from keras.layers import Layer, Input, Embedding, RepeatVector, Flatten, Dense
from keras.layers import TimeDistributed as Distribute
from keras.activations import softmax
from keras.engine import merge, InputSpec
import keras.backend as K
class ProbabilityTensor(Layer):
""" function for turning 3d tensor to 2d probability matrix """
@braingineer
braingineer / distribute_mask_issues_mwe.py
Created April 15, 2016 06:42
minimum working example for an issue with TimeDistributed's current compute_mask
from __future__ import print_function
from keras.layers import Input, Embedding, LSTM, Activation
from keras.layers import TimeDistributed as Distribute
import numpy as np
def outmask(x):
return x._keras_history[0].outbound_nodes[0].output_masks[0]
def inmask(x):
return x._keras_history[0].inbound_nodes[0].output_masks[0]
@braingineer
braingineer / ptb_lm_model.py
Last active April 28, 2016 02:02
setup for ptb language model w/ keras (not a working example; missing personal libraries)
B = self.igor.batch_size
R = self.igor.rnn_size
S = self.igor.max_sequence_len
V = self.igor.vocab_size
E = self.igor.embedding_size
### loaded from glove
emb_W = self.igor.embeddings.astype(theano.config.floatX)
## dropout parameters
p_emb = self.igor.p_emb_dropout
@braingineer
braingineer / keras_dense_rebuild.py
Created April 20, 2016 15:10
Dense layer being rebuilt by TimeDistributed
from __future__ import print_function
from keras.layers import Dense, TimeDistributed
import numpy as np
import theano
def shared(x):
x_ = theano.shared(x)
x_._keras_shape = x.shape
return x_
dense_func = Dense(1)
@braingineer
braingineer / readme.md
Created April 21, 2016 13:33 — forked from baraldilorenzo/readme.md
VGG-16 pre-trained model for Keras

##VGG16 model for Keras

This is the Keras model of the 16-layer network used by the VGG team in the ILSVRC-2014 competition.

It has been obtained by directly converting the Caffe model provived by the authors.

Details about the network architecture can be found in the following arXiv paper:

Very Deep Convolutional Networks for Large-Scale Image Recognition

K. Simonyan, A. Zisserman

@braingineer
braingineer / data_server.py
Created April 22, 2016 19:48
serving data
from __future__ import print_function, division
import yaml
import time
import sys
import numpy as np
import itertools
from keras.utils.np_utils import to_categorical
from baal.utils import loggers
try:
@braingineer
braingineer / reversing_tensors.py
Last active April 27, 2016 19:59
reversing a tensor for rnn stuff
from __future__ import print_function
import theano
import theano.tensor as T
import numpy as np
import random
A = np.arange(1,31).reshape(5,6).astype(np.float32)
sizes = np.zeros(5, dtype='int16')
for i in range(5):
sizes[i] = random.randint(1,5)