def load_sentiment_data(max_len):
    sentences = []
    data = pd.read_csv("./data/sentiment-tweets.csv")
    sentences = list(data['text'])
    sentiment_labels = data['airline_sentiment']

    sentences_of_words = [split_into_words(sentence) for sentence in sentences]
    word_counts_initial = get_word_counts(sentences_of_words)
    sentences_of_words = filter_words_by_count(sentences_of_words, word_counts_initial, MIN_WORD_COUNT)
    word_counts = get_word_counts(sentences_of_words)
    all_words = word_counts.keys()
    vocab_size = len(all_words)
    index_to_word = {index:word for index, word in enumerate(all_words)}
    word_to_index = {word: index for index, word in enumerate(all_words)}
    n_sentences = len(sentences_of_words)

    classes = set(sentiment_labels)
    n_classes = len(classes)

    X = np.zeros(shape=(n_sentences, max_len, vocab_size), dtype='float32')
    y = np.zeros(shape=(n_sentences, n_classes), dtype='float32')

    label_to_index = {'negative': 0, 'neutral': 1, 'positive':2}
    for sentence_index in range(n_sentences):
        current_sentence = sentences_of_words[sentence_index]

        for current_word_position in range(min(max_len, len(current_sentence))):
            word = sentences_of_words[sentence_index][current_word_position]
            token_index = word_to_index[word]
            X[sentence_index][current_word_position][token_index] = 1
        sentiment_label = sentiment_labels[sentence_index]
        sentiment_label_index = label_to_index[sentiment_label]
        y[sentence_index][sentiment_label_index] = 1
    return X, y, index_to_word, sentences
def load_arithmetic_data():
    data = pd.read_csv("./data/arithmetic-data.csv")
    equations = list(data['input'])
    answers = list(data['output'])
    print(answers[0])

    equations_of_chars = [[c for c in str(equation)] for equation in equations]
    answers_of_chars = [[c for c in str(answer)] for answer in answers]
    index_to_char = {i: str(i) for i in range(1, 10)}
    index_to_char[len(index_to_char.keys())+1] = '+'
    index_to_char[len(index_to_char.keys())+1] = '*'
    index_to_char[len(index_to_char.keys())+1] = '-'
    vocab_size = len(index_to_char.keys())
    char_to_index = {v:k for k,v in index_to_char.iteritems()}

    max_len = max(np.max([len(equation_of_chars) for equation_of_chars in equations_of_chars]), np.max([len(answer_of_chars) for answer_of_chars in answers_of_chars]))
    n_equations = len(equations_of_chars)

    X = np.zeros(shape=(n_equations, max_len, vocab_size), dtype='float32')
    y = np.zeros(shape=(n_equations, max_len, vocab_size), dtype='float32')

    label_to_index = {'negative': 0, 'neutral': 1, 'positive':2}
    for sentence_index in range(n_equations):
        current_sentence = equations_of_chars[sentence_index]
        for current_char_position in range(len(current_sentence)):
            char = equations_of_chars[sentence_index][current_char_position]
            token_index = char_to_index[char]
            X[sentence_index][current_char_position][token_index] = 1

        current_answer = answers_of_chars[sentence_index]
        for current_char_position in range(len(current_answer)):
            char = answers_of_chars[sentence_index][current_char_position]
            token_index = char_to_index[char]
            y[sentence_index][current_char_position][token_index] = 1
    return X, y, index_to_char, equations, answers, max_len
def recenter(targetArray, sourceArray):
    mean = np.zeros(targetArray.shape[0])
    for i in np.transpose(sourceArray):
        mean += i
    mean = mean / sourceArray.shape[1]
    a = np.zeros(targetArray.shape)
    for i in range(targetArray.shape[1]):
        a[:, i] = targetArray[:, i] - mean
    return a
def sample(sSet, n):
    size = len(sSet.x)
    l_sample = list()
    sample_size = size
    for i in range(n):
        S_feature = np.zeros(shape=(sample_size, feat_size))
        S_label = np.zeros(shape=(sample_size, 1))
        for j in range(sample_size):
            rand_index = randrange(0, size)
            S_feature[j] = sSet.feature[rand_index]
            S_label[j] = sSet.label[rand_index]
        l_sample.append(theSet(S_feature, S_label))
    return l_sample
def sample(sSet, n):
  size = len(sSet.x)
  l_sample = list()
  sample_size = size
  for i in range(n):
    S_feature = np.zeros(shape = (sample_size, feat_size))
    S_label = np.zeros(shape = (sample_size, 1))
    for j in range(sample_size):
      rand_index = randrange(0, size)
      S_feature[j] = sSet.feature[rand_index]
      S_label[j] = sSet.label[rand_index]
    l_sample.append(theSet(S_feature, S_label))
  return l_sample
def read_asdf(tfile):

    with pyasdf.ASDFDataSet(tfile, MPI=False, mode='r') as ds:
        data_types = ds.auxiliary_data.list()
        path = ds.auxiliary_data[data_types[0]].list()
        Nfft = ds.auxiliary_data[data_types[0]][path[0]].parameters['nfft']
        Nseg = ds.auxiliary_data[data_types[0]][path[0]].parameters['nseg']
    ncomp = len(data_types)
    nsta = len(path)

    memory_size = nsta * ncomp * Nfft // 2 * Nseg * 8 / 1024 / 1024 / 1024

    cc_array = np.zeros((nsta * ncomp, Nseg * Nfft // 2), dtype=np.complex64)
    cc_std = np.zeros((nsta * ncomp))
def spec(windowSize, stepSize, input, fftMag, sr):

    windowSize = int(windowSize)
    stepSize = int(stepSize)
    numberOfSteps = int(math.floor((len(input) - windowSize) / stepSize) + 1)
    spectrogram = np.zeros((int(fftMag / 2), numberOfSteps))
    timeArray = np.zeros((numberOfSteps))
    for i in range(numberOfSteps):
        currentWindow = input[stepSize * i:(stepSize * i) + windowSize]
        transformedWindow = scipy.fft.fft(currentWindow, n=fftMag)
        truncatedWindow = transformedWindow[:int(fftMag / 2)]
        spectrogram[:, i] = np.log(np.abs(truncatedWindow) + 1)
        timeArray[i] = (stepSize * i + (stepSize * i + windowSize)) / (2 * sr)
    return spectrogram, timeArray
Example #8
0
 def __init__(self, subagent, n):
     self.i = subagent()
     self.u = subagent()
     self.n = n
     self.L = np.zeros((2 * n, 6))
     #each agent reports sucess predicting self and others here the next takes
     #first is A0 and E0 predicting him,
     #then A0 and E0 predicting me
     self.perlast = np.zeros(2 * n + 2, dtype=int)
     #laset A0, E0
     #last choice shifted by 1 including agents excl last since no judge
     self.perlast -= 1
     self.ilast = -1
     self.ulast = -1
     self.temp = []
Example #9
0
    def __OneHotEncoding(self, correct_class):
        num_classes = len(self.labels)
        one_hot = np.zeros(num_classes)
        one_hot[correct_class] = 1
        # print("total: ", num_classes, "c: ", correct, "one hot: ", one_hot)

        return one_hot
Example #10
0
def entries2():
    B = np.zeros((3, 3), dtype=int)
    for i in range(0, 3):
        for j in range(0, 3):
            k = input("Entries:")
            B[i][j] = k
    return B
Example #11
0
def mine(blockData, nonce, address):
    # print(blockData)
    blockDataHash = blockData['blockDataHash']
    difficulty = blockData['difficulty']
    prevBlockHash = blockData['prevBlockHash']
    transactions = blockData['transactions']
    index = blockData['index']
    timestamp = time()/1000
    block = Block(blockDataHash, 
        difficulty,
        nonce, 
        timestamp, 
        prevBlockHash, 
        transactions, 
        index,
        address)
    block.calculateHash()
    timeStart = time()
    while(block.blockHash[0:block.difficulty] != ''.join(str(x) for x in np.zeros(block.difficulty, int))):
        block.nonce = block.nonce + 1
        block.timestamp = datetime.fromtimestamp(time() - 28800).isoformat() + "Z"
        block.calculateHash()
        # print(str(block.nonce) + "\t=>\t" + str(block.blockHash))
    minedBlock = block.minedBlock()
    return minedBlock
Example #12
0
def main(argv):


    #for i in range(4):
    imageName = 'image0.png'
    src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR)  # Load an image
    # Check if image is loaded fine
    if src is None:
        print('Error opening image')
        return -1
    print("ne")
    laplacian(src, imageName)
    gaussian(src, imageName)
    sobel(src,imageName)

    img = cv.imread('input.jpg')
#    cv.imshow('Original', img)

    size = 15

    # generating the kernel
    kernel_motion_blur = np.zeros((size, size))
    kernel_motion_blur[int((size - 1) / 2), :] = np.ones(size)
    kernel_motion_blur = kernel_motion_blur / size

    # applying the kernel to the input image
    output = cv.filter2D(img, -1, kernel_motion_blur)

    cv.imshow('Motion Blur', output)
    cv.waitKey(0)


    return 0
Example #13
0
def applyFilter(image, kernel):
    output = np.zeros(image.shape, dtype=np.float32)
    channel_count = image.shape[2]
    image_height = image.shape[0]
    image_width = image.shape[1]
    kernel_height = kernel.shape[0]
    kernel_halfh = kernel_height // 2
    kernel_width = kernel.shape[1]
    kernel_halfw = kernel_width // 2
    for y in range(image_height):
        for x in range(image_width):
            x_min = max(0, x - kernel_halfw)
            x_max = min(image_width - 1, x + kernel_halfw)
            y_min = max(0, y - kernel_halfh)
            y_max = min(image_height - 1, y + kernel_halfh)
            for c in range(channel_count):
                value = 0
                for u in range(x_min, x_max + 1):
                    for v in range(y_min, y_max + 1):
                        tmp = kernel[v - y + kernel_halfh, u - x + kernel_halfw]
                        value += image[v, u, c] * tmp  
                if value < 0:
                    value = 0
                elif value > 255:
                    value = 255
                output[y, x, c] = value
    return output
Example #14
0
def crop(img, center, scale, rot, res):
    # Crop def tailored to the needs of our system. Provide a center
    # and scale value and the image will be cropped and resized to the output
    # resolution determined by res. 'rot' will also rotate the image as needed.

    ul = transform((1,1), center, scale, 0, res, true)
    br = transform((res,res), center, scale, 0, res, true)

    pad = math.floor(np.linalg.norm((ul - br).astype(float))/2 - (br[1]-ul[1])/2)
    if rot != 0:
        ul = ul - pad
        br = br + pad
    
    ht, wd, channels = img.shape

    newDim = (channels, br[1] - ul[1], br[0] - ul[0])
    newImg = np.zeros(newDim[2],newDim[1],newDim[0])

    newX = (max(1, -ul[1]+1), min(br[1], wd) - ul[1])
    newY = (max(1, -ul[2]+1), min(br[2], ht) - ul[2])
    oldX = (max(1, ul[1]+1), min(br[1], wd))
    oldY = (max(1, ul[2]+1), min(br[2], ht))

    if newDim:size()[1] > 2:
        newImg:sub(1,newDim[1],newY[1],newY[2],newX[1],newX[2]):copy(img:sub(1,newDim[1],oldY[1],oldY[2],oldX[1],oldX[2]))
Example #15
0
 def __init__(self, x, p, Nrl=1000):
     self.x = x
     self.pdf = p / p.sum()
     self.cdf = self.pdf.cumsum()
     self.inversecdfbins = Nrl
     self.Nrl = Nrl
     y = np.arange(Nrl) / float(Nrl)
     _delta = 1.0 / Nrl
     self.inversecdf = np.zeros(Nrl)
     self.inversecdf[0] = self.x[0]
     cdf_idx = 0
     for n in range(1, self.inversecdfbins):
         while self.cdf[cdf_idx] < y[n] and cdf_idx < Nrl:
             cdf_idx += 1
         # Seems a linear interpolation
         self.inversecdf[n] = (self.x[cdf_idx - 1] +
                               (self.x[cdf_idx] -
                                self.x[cdf_idx - 1]) *
                               (y[n] - self.cdf[cdf_idx - 1]) /
                               (self.cdf[cdf_idx] -
                                self.cdf[cdf_idx - 1])
                               )
         if cdf_idx >= Nrl:
             break
     self.delta_inversecdf = np.concatenate(
         (np.diff(self.inversecdf), [0])
         )
Example #16
0
 def __init__(self, world):
     # call base
     Metric.__init__(self, world)
     self.name = "exploration"
     self.value = 0.0
     self.format = "%.2f%%"
     # dictionary mapping hashes to ticks used for cycle detection
     self.rule_count = np.zeros(self.world.rule.size, dtype=int)
Example #17
0
def transform(picture):
    histogram_counts = separateOtsu(picture)
    equalize_indexes = equalize(histogram_counts)
    transformedpicture = np.zeros(picture.shape)
    for x in range(len(picture)):
        for y in range(len(picture[0])):
            transformedpicture[x][y] = equalize_indexes[picture[x][y]]
    return transformedpicture
Example #18
0
def relu(feature_map):
    #Preparing the output of the ReLU activation function.
    relu_out = np.zeros(feature_map.shape)
    for map_num in range(feature_map.shape[-1]):
        for r in np.arange(0,feature_map.shape[0]):
            for c in np.arange(0, feature_map.shape[1]):
                relu_out[r, c, map_num] = np.max([feature_map[r, c, map_num], 0])
    return relu_out
  def intializeBiases(cls, data, nrHidden):
    # get the procentage of data points that have the i'th unit on
    # and set the visible vias to log (p/(1-p))
    percentages = data.mean(axis=0, dtype='float')
    vectorized = np.vectorize(safeLogFraction, otypes=[np.float])
    visibleBiases = vectorized(percentages)

    hiddenBiases = np.zeros(nrHidden)
    return np.array([visibleBiases, hiddenBiases])
def get_random_minibatch_indices(n_examples, batch_size):
    indices = range(n_examples)
    random.shuffle(indices)
    num_batches = n_examples/batch_size
    minibatch_indices = np.zeros(shape=(num_batches, batch_size), dtype='int32')
    for b_i in range(num_batches):
        for ex_i in range(batch_size):
            minibatch_indices[b_i] = indices[b_i*batch_size:(b_i+1)*batch_size]
    return minibatch_indices
Example #21
0
    def calc_info_gain(self, data, classes, feature):
        gain = 0
        nData = len(data)
        # List the values that feature can take
        values = []
        for datapoint in data:
            if datapoint[feature] not in values:
                values.append(datapoint[feature])

        featureCounts = np.zeros(len(values))
        entropy = np.zeros(len(values))
        valueIndex = 0
        #Find where those values appear in data[feature] and the corresponding
        # class
        for value in values:
            dataIndex = 0
            newClasses = []
            for datapoint in data:
                if datapoint[feature] == value:
                    featureCounts[valueIndex] += 1
                    newClasses.append(classes[dataIndex])
                dataIndex += 1

            # Get the values in newClasses
            classValues = []
            for aclass in newClasses:
                if classValues.count(aclass) == 0:
                    classValues.append(aclass)
            classCounts = np.zeros(len(classValues))
            classIndex = 0
            for classValue in classValues:
                for aclass in newClasses:
                    if aclass == classValue:
                        classCounts[classIndex] += 1
                classIndex += 1

            for classIndex in range(len(classValues)):
                entropy[valueIndex] += self.calc_entropy(float(
                classCounts[classIndex]) / sum(classCounts))
            gain += float(featureCounts[valueIndex]) / \
            nData * entropy[valueIndex]
            valueIndex += 1
        return gain
Example #22
0
def colorFiler(image, color):
    output = np.zeros(image.shape, dtype=np.float32)
    channel_count = image.shape[2]
    image_height = image.shape[0]
    image_width = image.shape[1]
    for y in range(image_height):
        for x in range(image_width):
            for c in range(channel_count):
                output[y, x, c] = (color[c] + image[y, x, c]) / 2
    return output
Example #23
0
    def calc_info_gain(self, data, classes, feature):
        gain = 0
        nData = len(data)
        # List the values that feature can take
        values = []
        for datapoint in data:
            if datapoint[feature] not in values:
                values.append(datapoint[feature])

        featureCounts = np.zeros(len(values))
        entropy = np.zeros(len(values))
        valueIndex = 0
        #Find where those values appear in data[feature] and the corresponding
        # class
        for value in values:
            dataIndex = 0
            newClasses = []
            for datapoint in data:
                if datapoint[feature] == value:
                    featureCounts[valueIndex] += 1
                    newClasses.append(classes[dataIndex])
                dataIndex += 1

            # Get the values in newClasses
            classValues = []
            for aclass in newClasses:
                if classValues.count(aclass) == 0:
                    classValues.append(aclass)
            classCounts = np.zeros(len(classValues))
            classIndex = 0
            for classValue in classValues:
                for aclass in newClasses:
                    if aclass == classValue:
                        classCounts[classIndex] += 1
                classIndex += 1

            for classIndex in range(len(classValues)):
                entropy[valueIndex] += self.calc_entropy(
                    float(classCounts[classIndex]) / sum(classCounts))
            gain += float(featureCounts[valueIndex]) / \
            nData * entropy[valueIndex]
            valueIndex += 1
        return gain
Example #24
0
 def current_image(self):
     if self.test_mode:
         height = 480
         width = 640
         dummy_image = np.zeros((height, width, 3), np.uint8)
         dummy_image[height // 3:2 * height // 3,
                     width // 3:2 * width // 3] = (0, 147, 255)
         return dummy_image
     else:
         return self.get_video_cap().read()[-1]
Example #25
0
def build_grid(matrix, no_iter):
    grid_size = len(matrix) + 2 * no_iter
    grid = np.zeros((grid_size, grid_size, grid_size), dtype=int)

    k = grid_size // 2 + 1
    for i in range(0, len(matrix)):
        for j in range(0, len(matrix)):
            grid[k, i + no_iter, j + no_iter] = matrix[i][j]

    return grid, (k, no_iter, no_iter)
Example #26
0
def invertColors(image):
    output = np.zeros(image.shape, dtype=np.float32)
    channel_count = image.shape[2]
    image_height = image.shape[0]
    image_width = image.shape[1]
    for y in range(image_height):
        for x in range(image_width):
            for c in range(channel_count):
                output[y, x, c] = 255 - image[y, x, c]
    return output
Example #27
0
 def reset(self):
     # big bang initial conditions
     self.time = 0
     self.cells = np.zeros((self.diameter, self.diameter),
                           dtype=int)  # TODO type affects performace?
     self.cells[self.radius, self.radius] = 1
     # matrix with indices into rule array of last tick
     self.trans_idx = None
     # statistics
     self.tick_stat = TimingStat()
Example #28
0
def mul(a,b):
  if(len(a[0]) != len(b)):
    print("The matrices to be multiplied have the wrong sizes")
    return None
  else: 
    prod = np.zeros((len(a), len(b[0]))).tolist()
    for i in range(len(a)):
      for j in range(len(b[0])):
        prod[i][j] = sum([a[i][k]*b[k][j] for k in range(len(a[0]))])
    return prod
Example #29
0
def get_glove(word_index, embedding_dim, embedding_file):
    vocab_size = len(
        word_index) + 1  # Adding again 1 because of reserved 0 index
    embedding_matrix = np.zeros((vocab_size, embedding_dim))
    with open(embedding_file) as f:
        for line in f:
            word, *vector = line.split()
            if word in word_index:
                idx = word_index[word]
                embedding_matrix[idx] = np.array(
                    vector, dtype=np.float32)[:embedding_dim]
    return embedding_matrix
Example #30
0
def grayscaleFilter(image):
    output = np.zeros(image.shape, dtype=np.float32)
    channel_count = image.shape[2]
    image_height = image.shape[0]
    image_width = image.shape[1]
    for y in range(image_height):
        for x in range(image_width):
            value = 0
            for c in range(channel_count):
                value += image[y, x, c]
            value /= 3
            output[y, x] = [value, value, value]
    return output
Example #31
0
 def _read(self, g, f):
     fi = self.ds.field_info[f]
     if fi.particle_type and g.NumberOfParticles == 0:
         # because this gets upcast to float
         return np.array([],dtype='float64')
     try:
         temp = self.ds.index.io._read_data_set(g, f)
     except:# self.ds.index.io._read_exception as exc:
         if fi.not_in_all:
             temp = np.zeros(g.ActiveDimensions, dtype='float64')
         else:
             raise
     return temp
Example #32
0
    def __init__(self, rng, input, filter_shape, image_shape, poolsize=(1, 1)):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: np.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """
        assert image_shape[1] == filter_shape[1]
        self.input = input

        # initialize weight values: the fan-in of each hidden neuron is
        # restricted by the size of the receptive fields.
        fan_in =  np.prod(filter_shape[1:])
        W_values = np%.asarray(rng.uniform(
              low=-np%.sqrt(3./fan_in),
              high=np.sqrt(3./fan_in),
              size=filter_shape), dtype=theano.config.floatX)
        self.W = theano.shared(value=W_values, name='W')

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, name='b')

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input, self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = downsample.max_pool_2d(conv_out, poolsize, ignore_border=True)

        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will thus
        # be broadcasted across mini-batches and feature map width & height
        self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
Example #33
0
def pooling(feature_map, size=2, stride=2):
    #Preparing the output of the pooling operation.
    pool_out = np.zeros((np.uint16((feature_map.shape[0]-size+1)/stride+1),
                            np.uint16((feature_map.shape[1]-size+1)/stride+1),
                            feature_map.shape[-1]))
    for map_num in range(feature_map.shape[-1]):
        r2 = 0
        for r in np.arange(0,feature_map.shape[0]-size+1, stride):
            c2 = 0
            for c in np.arange(0, feature_map.shape[1]-size+1, stride):
                pool_out[r2, c2, map_num] = np.max([feature_map[r:r+size,  c:c+size, map_num]])
                c2 = c2 + 1
            r2 = r2 +1
    return pool_out
Example #34
0
 def load(self, vectorizer):
     print('===== Glove Embeddings loading from %s =====' %
           (self._glove_file))
     embedding_dim = self._embedding_dim
     self._vocab_size = len(vectorizer.vocabulary_) + 1
     self._embedding_matrix = np.zeros((self._vocab_size, embedding_dim))
     with open(self._glove_file) as f:
         for line in f:
             word, *vector = line.split()
             if word in vectorizer.vocabulary_:
                 idx = vectorizer.vocabulary_[word]
                 self._embedding_matrix[idx] = np.array(
                     vector, dtype=np.float32)[:embedding_dim]
     return self._embedding_matrix
Example #35
0
    def capture_output(sys, instance, args, kwargs):
        try:
            frame = inspect.currentframe()
            tb = inspect.getframeinfo(sys)
            log.info("system '{}' found in context '{code_context}' line "
                     "'{lineno}'".format(sys.__name__, **tb))
            # call system and append result to tracking array
            out = sys(*args, **kwargs)
            contexts.setdefault(tb.code_context,
                np.zeros(sys._context.max_buf_len)
            )[:] = out
            # np.append(contexts.setdefault(tb.code_context, np.array([])), out)

        finally:
            del frame
        return out
 def initialize_data(self):
     ds = self.ds
     fields = [f for f in ds.field_list
               if not ds.field_info[f].particle_type]
     pfields = [f for f in ds.field_list
                if ds.field_info[f].particle_type]
     # Preload is only defined for Enzo ...
     if ds.index.io._dataset_type == "enzo_packed_3d":
         self.queue = ds.index.io.queue
         ds.index.io.preload(self.grids, fields)
         for g in self.grids:
             for f in fields:
                 if f not in self.queue[g.id]:
                     d = np.zeros(g.ActiveDimensions, dtype='float64')
                     self.queue[g.id][f] = d
             for f in pfields:
                 self.queue[g.id][f] = self._read(g, f)
     else:
         self.queue = {}
         for g in self.grids:
             for f in fields + pfields:
                 self.queue[g.id][f] = ds.index.io._read(g, f)
Example #37
0
def gaussiannoise(mean, var, data):
    if var == 0:
        return np.zeros(data.shape)
    else:
        return np.random.normal(mean, np.sqrt(var), data.shape)
Example #38
0
training_data = []
with open(training_data_file) as f:
    for line in f:
        training_data.append(json.loads(line))

test_data = []
with open(test_data_file) as f:
    for line in f:
        test_data.append(json.loads(line))


words = []
idx = 0
word2idx = {}
vectors = bcolz.carray(np.zeros(1), rootdir=f'./data/6B.50.dat', mode='w')

with open(glove_embeddings_file, 'rb') as f:
    for l in f:
        line = l.decode().split()
        word = line[0]
        words.append(word)
        word2idx[word] = idx
        idx += 1
        vect = np.array(line[1:]).astype(np.float)
        vectors.append(vect)

vectors = bcolz.carray(vectors[1:].reshape((400000, 50)), rootdir=f'./data/6B.50.dat', mode='w')
vectors.flush()
pickle.dump(words, open(f'./data/6B.50_words.pkl', 'wb'))
pickle.dump(word2idx, open(f'./data/6B.50_idx.pkl', 'wb'))
import np

d = 128
points = np.random.randint(0, d, (32, 3))
hull_space=np.zeros([d,d,d], dtype=np.int8)
for x in range(d):
    for y in range(d):
        for z in range(d):
            coord = np.array([x,y,z])
            diff = points - coord
            dist = diff[:,0]**2 + diff[:,1]**2 + diff[:,2]**2
            closest = np.argmin(dist)
            hull_space[x][y][z] = closest * 8
 def build_count_matrix(self):
     self.keys = [k for k in self.wdict.keys() if len(self.wdict[k]) > 1]
     self.A = zeros([len(self.keys), self.dcount])
     for i, k in enumerate(self.keys):
         for d in self.wdict[k]:
             self.A[i, d] += 1
Example #41
0
def tile_raster_images(X, img_shape, tile_shape, tile_spacing=(0, 0),
                       scale_rows_to_unit_interval=True,
                       output_pixel_vals=True):
    """
    Transform an array with one flattened image per row, into an array in
    which images are reshaped and layed out like tiles on a floor.

    This function is useful for visualizing datasets whose rows are images,
    and also columns of matrices for transforming those rows
    (such as the first layer of a neural net).

    :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can
    be 2-D ndarrays or None;
    :param X: a 2-D array in which every row is a flattened image.

    :type img_shape: tuple; (height, width)
    :param img_shape: the original shape of each image

    :type tile_shape: tuple; (rows, cols)
    :param tile_shape: the number of images to tile (rows, cols)

    :param output_pixel_vals: if output should be pixel values (i.e. int8
    values) or floats

    :param scale_rows_to_unit_interval: if the values need to be scaled before
    being plotted to [0,1] or not


    :returns: array suitable for viewing as an image.
    (See:`PIL.Image.fromarray`.)
    :rtype: a 2-d array with same dtype as X.

    """

    assert len(img_shape) == 2
    assert len(tile_shape) == 2
    assert len(tile_spacing) == 2

    # The expression below can be re-written in a more C style as
    # follows :
    #
    # out_shape    = [0,0]
    # out_shape[0] = (img_shape[0]+tile_spacing[0])*tile_shape[0] -
    #                tile_spacing[0]
    # out_shape[1] = (img_shape[1]+tile_spacing[1])*tile_shape[1] -
    #                tile_spacing[1]
    out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp
                        in zip(img_shape, tile_shape, tile_spacing)]

    if isinstance(X, tuple):
        assert len(X) == 4
        # Create an output np ndarray to store the image
        if output_pixel_vals:
            out_array = np.zeros((out_shape[0], out_shape[1], 4),
                                    dtype='uint8')
        else:
            out_array = np.zeros((out_shape[0], out_shape[1], 4),
                                    dtype=X.dtype)

        #colors default to 0, alpha defaults to 1 (opaque)
        if output_pixel_vals:
            channel_defaults = [0, 0, 0, 255]
        else:
            channel_defaults = [0., 0., 0., 1.]

        for i in xrange(4):
            if X[i] is None:
                # if channel is None, fill it with zeros of the correct
                # dtype
                dt = out_array.dtype
                if output_pixel_vals:
                    dt = 'uint8'
                out_array[:, :, i] = np.zeros(out_shape,
                        dtype=dt) + channel_defaults[i]
            else:
                # use a recurrent call to compute the channel and store it
                # in the output
                out_array[:, :, i] = tile_raster_images(
                    X[i], img_shape, tile_shape, tile_spacing,
                    scale_rows_to_unit_interval, output_pixel_vals)
        return out_array

    else:
        # if we are dealing with only one channel
        H, W = img_shape
        Hs, Ws = tile_spacing

        # generate a matrix to store the output
        dt = X.dtype
        if output_pixel_vals:
            dt = 'uint8'
        out_array = np.zeros(out_shape, dtype=dt)

        for tile_row in xrange(tile_shape[0]):
            for tile_col in xrange(tile_shape[1]):
                if tile_row * tile_shape[1] + tile_col < X.shape[0]:
                    this_x = X[tile_row * tile_shape[1] + tile_col]
                    if scale_rows_to_unit_interval:
                        # if we should scale values to be between 0 and 1
                        # do this by calling the `scale_to_unit_interval`
                        # function
                        this_img = scale_to_unit_interval(
                            this_x.reshape(img_shape))
                    else:
                        this_img = this_x.reshape(img_shape)
                    # add the slice to the corresponding position in the
                    # output array
                    c = 1
                    if output_pixel_vals:
                        c = 255
                    out_array[
                        tile_row * (H + Hs): tile_row * (H + Hs) + H,
                        tile_col * (W + Ws): tile_col * (W + Ws) + W
                        ] = this_img * c
        return out_array
def contrastiveDivergence(data, biases, weights, activationFun, dropout,
                          visibleDropout, miniBatchSize=10):
  N = len(data)
  epochs = N / miniBatchSize

  # sample the probabily distributions allow you to chose from the
  # visible units for dropout
  on = sample(visibleDropout, data.shape)
  dropoutData = data * on  
  
  epsilon = 0.01
  decayFactor = 0.0002
  weightDecay = True
  reconstructionStep = 50

  oldDeltaWeights = np.zeros(weights.shape)
  oldDeltaVisible = np.zeros(biases[0].shape)
  oldDeltaHidden = np.zeros(biases[1].shape)

  batchLearningRate = epsilon / miniBatchSize
  print "batchLearningRate"
  print batchLearningRate

  for epoch in xrange(epochs):
    batchData = dropoutData[epoch * miniBatchSize: (epoch + 1) * miniBatchSize, :]
    if epoch < epochs / 100:
      momentum = 0.5
    else:
      momentum = 0.95

    if epoch < (N/7) * 10:
      cdSteps = 3
    elif epoch < (N/9) * 10:
      cdSteps = 5
    else:
      cdSteps = 10

    if EXPENSIVE_CHECKS_ON:
      if epoch % reconstructionStep == 0:
        print "reconstructionError"
        print reconstructionError(biases, weights, data, activationFun)

    weightsDiff, visibleBiasDiff, hiddenBiasDiff =\
            modelAndDataSampleDiffs(batchData, biases, weights,
            activationFun, dropout, cdSteps)
    # Update the weights
    # data - model
    # Positive phase - negative
    # Weight decay factor
    deltaWeights = (batchLearningRate * weightsDiff
                    - epsilon * weightDecay * decayFactor * weights)

    deltaVisible = batchLearningRate * visibleBiasDiff
    deltaHidden  = batchLearningRate * hiddenBiasDiff

    deltaWeights += momentum * oldDeltaWeights
    deltaVisible += momentum * oldDeltaVisible
    deltaHidden += momentum * oldDeltaHidden

    oldDeltaWeights = deltaWeights
    oldDeltaVisible = deltaVisible
    oldDeltaHidden = deltaHidden

    # Update the weighths
    weights += deltaWeights
    # Update the visible biases
    biases[0] += deltaVisible

    # Update the hidden biases
    biases[1] += deltaHidden

  print reconstructionError(biases, weights, data, activationFun)
  return biases, weights
def PCD(data, biases, weights, activationFun, dropout,
                          visibleDropout, miniBatchSize=10):
  N = len(data)
  epochs = N / miniBatchSize

  # sample the probabily distributions allow you to chose from the
  # visible units for dropout
  # on = sample(visibleDropout, data.shape)
  # dropoutData = data * on
  dropoutData = data

  epsilon = 0.01
  decayFactor = 0.0002
  weightDecay = True
  reconstructionStep = 50

  oldDeltaWeights = np.zeros(weights.shape)
  oldDeltaVisible = np.zeros(biases[0].shape)
  oldDeltaHidden = np.zeros(biases[1].shape)

  batchLearningRate = epsilon / miniBatchSize
  print "batchLearningRate"
  print batchLearningRate

  # make this an argument or something
  nrFantasyParticles = miniBatchSize

  fantVisible = np.random.randint(2, size=(nrFantasyParticles, weights.shape[0]))
  fantHidden = np.random.randint(2, size=(nrFantasyParticles, weights.shape[1]))

  fantasyParticles = (fantVisible, fantHidden)
  steps = 10

  for epoch in xrange(epochs):
    batchData = dropoutData[epoch * miniBatchSize: (epoch + 1) * miniBatchSize, :]
    if epoch < epochs / 100:
      momentum = 0.5
    else:
      momentum = 0.95

    if EXPENSIVE_CHECKS_ON:
      if epoch % reconstructionStep == 0:
        print "reconstructionError"
        print reconstructionError(biases, weights, data, activationFun)

    print fantasyParticles[0]
    print fantasyParticles[1]
    weightsDiff, visibleBiasDiff, hiddenBiasDiff, fantasyParticles =\
            modelAndDataSampleDiffsPCD(batchData, biases, weights,
            activationFun, dropout, steps, fantasyParticles)

    # Update the weights
    # data - model
    # Positive phase - negative
    # Weight decay factor
    deltaWeights = (batchLearningRate * weightsDiff
                    - epsilon * weightDecay * decayFactor * weights)

    deltaVisible = batchLearningRate * visibleBiasDiff
    deltaHidden  = batchLearningRate * hiddenBiasDiff

    deltaWeights += momentum * oldDeltaWeights
    deltaVisible += momentum * oldDeltaVisible
    deltaHidden += momentum * oldDeltaHidden

    oldDeltaWeights = deltaWeights
    oldDeltaVisible = deltaVisible
    oldDeltaHidden = deltaHidden

    # Update the weighths
    weights += deltaWeights
    # Update the visible biases
    biases[0] += deltaVisible

    # Update the hidden biases
    biases[1] += deltaHidden

  print reconstructionError(biases, weights, data, activationFun)
  return biases, weights