Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
@mratsim
mratsim / README.md
Last active November 7, 2018 11:48
Convolution optimisation resources figures
@mratsim
mratsim / lagrangeprop.py
Created October 1, 2018 12:41 — forked from timvieira/lagrangeprop.py
Automatic differentiation as the method of Lagrange multipliers. Code accompanies this blog post: http://timvieira.github.io/blog/post/2017/08/18/backprop-is-not-just-the-chain-rule/
# -*- coding: utf-8 -*-
"""
Backprop as the method of Lagrange multiplers (and even the implicit function
theorem).
"""
from __future__ import division
import numpy as np
from arsenal.alphabet import Alphabet
from arsenal.math.checkgrad import finite_difference
@mratsim
mratsim / sequence_train_test_split.py
Created September 28, 2018 18:28
train_test_split for sequence predictions
def make_input_seqs(data, seq_len, train_split=0.9):
seq_len = seq_len + 1
result = []
for index in range(len(data) - seq_len):
result.append(data[index: index + seq_len, :])
result = np.array(result)
train_ind = round(train_split * result.shape[0])
train = result[:int(train_ind), :, :]
x_train = train[:, :-1, :]
y_train = train[:, -1, :]
@mratsim
mratsim / setting_bits.nim
Created September 28, 2018 17:07
Constant time bit setter for nim-bn-curve
var a: array[2, uint64]
func toBinString*[N: static int](x: array[N, uint64]): string =
const binChar = ['0', '1']
const byte_size = N*8
let bytes = cast[ptr array[byte_size, byte]](x.unsafeaddr)
result = newStringOfCap(byte_size*8)
@mratsim
mratsim / autograd_shapeshifting.nim
Created September 28, 2018 13:34
Stack gradient: TODO - how to setup tests with loss.backward and numerical gradient
# Copyright 2017-2018 Mamy André-Ratsimbazafy & the Arraymancer contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# MIT License
# Copyright (c) 2018 Mamy André-Ratsimbazafy
## This files gives basic tensor library functionality, because yes we can
import strformat, macros, sequtils, random
type
Tensor[Rank: static[int], T] = object
## Tensor data structure stored on Cpu
## - ``shape``: Dimensions of the tensor
@mratsim
mratsim / ec_bls12_381_generator.nim
Created August 8, 2018 17:01
BLS12-381 Elliptic curve generator
import stint, math, algorithm
# config_big_384_29.h
# #define MODBYTES_384_29 48 /**< Number of bytes in Modulus */
# #define BASEBITS_384_29 29 /**< Numbers represented to base 2*BASEBITS */
# big_384_29.h
# #define BIGBITS_384_29 (8*MODBYTES_384_29) /**< Length in bits */
# #define NLEN_384_29 (1+((8*MODBYTES_384_29-1)/BASEBITS_384_29)) /**< length in bytes */
# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import random, sequtils, strformat
type
@mratsim
mratsim / stint_indexing.nim
Last active June 17, 2018 10:46
Compile-time indexing of binary tree representation of multiprecision ints
import ../stint/private/datatypes
import macros
var a: UintImpl[UintImpl[uint64]]
proc asWordsImpl(x: NimNode, current_path: NimNode, result: var NimNode) =
## Transforms an UintImpl/IntImpl into an array of words
## at compile-time. Recursve implementation.
## Result is from most significant word to least significant
@mratsim
mratsim / opcode_add.nim
Created June 15, 2018 19:05
EVM opcode refactor test
import
stint,
../nimbus/constants,
../nimbus/vm/interpreter/[opcodes_impl, vm_forks],
../nimbus/vm/[message, computation, stack],
../nimbus/utils/header,
../nimbus/db/[db_chain, state_db, backends/memory_backend]
let msg = newMessage(
to=ZERO_ADDRESS, #fixture{"exec"}{"address"}.getStr,