Skip to content

Instantly share code, notes, and snippets.

View sseveran's full-sized avatar

Steve Severance sseveran

View GitHub Profile
@sseveran
sseveran / gist:3430017
Created August 22, 2012 22:19
Exception Scoping 1
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Exception
import Prelude hiding (catch)
iThrowExceptions :: Int
iThrowExceptions = error "I am a slippery exception"
data Record = Record Int deriving (Show)
main :: IO ()
main = do
@sseveran
sseveran / gist:3430084
Created August 22, 2012 22:28
Exception Scoping 2
data MyRecord = MyRecord{
mrField :: [Text]
}
(do
some operations...
return $ MyRecord $ fmap (decodeUtf8) listOfByteStrings)
`catch`
myHandler
@sseveran
sseveran / gist:3430089
Created August 22, 2012 22:29
Exception Scoping 3
safeCatch :: (Exception e, NFData a) => IO a -> (e -> IO a) -> IO a
safeCatch func handler = catch eval handler where
eval = do
x <- func
rnf x `seq` return $! x
@sseveran
sseveran / gist:3430096
Created August 22, 2012 22:31
Exception Scoping 4
data MyRecord = MyRecord{
mrField :: [Text]
}
(do
some operations...
return $ MyRecord $ fmap (decodeUtf8) listOfByteStrings)
`safeCatch`
myHandler
@sseveran
sseveran / gist:3517817
Created August 29, 2012 19:43
Exception Scoping 5
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.DeepSeq
import Control.DeepSeq.TH
import Control.Exception
import Data.Typeable
import Prelude hiding (catch)
@sseveran
sseveran / gist:3588176
Created September 1, 2012 21:37
Uncaught Exceptions
-- | when no catch frame handles an exception dump core and terminate the process
uncaughtExceptionHandler :: SomeException -> IO ()
{-# NOINLINE uncaughtExceptionHandler #-}
uncaughtExceptionHandler !e = do
syslog Error $ "Unhandled exception: " ++ show e
raiseSignal sigABRT
setDefaultUncaughtExceptionHandler :: IO ()
setDefaultUncaughtExceptionHandler =
setUncaughtExceptionHandler uncaughtExceptionHandler
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Concurrent.STM
newtype Money = Money Int deriving (Show, Num, Ord, Eq)
data Account = Account{
aBalance :: Money,
aAccountId :: Int
}
@sseveran
sseveran / gist:3aa2d9afbbba8bb4883d
Created October 17, 2014 04:34
Cuda Black-Sholes
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Applicative
import Data.Array.Accelerate as A
import Data.Array.Accelerate.CUDA
import qualified Data.Array.Accelerate.Interpreter as I
import Data.Array.Accelerate.Pretty
import Data.Array.Unboxed
import qualified Data.List as L
import System.Random
@sseveran
sseveran / ray_tune_reporter_hook.py
Created June 15, 2018 15:42
A Tensorflow hook for reporting state to ray-tune
import six
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.training import training_util
from tensorflow.python.training.session_run_hook import SessionRunArgs
class RayTuneReportingHook(tf.train.SessionRunHook):
def __init__(self, params, reporter):
self.reporter = reporter
# Compute the minimize_op.
params = model.trainable_variables
grads = optimizer.get_gradients(total_loss, params)
grads_and_vars = list(zip(grads, params))
for gradient, variable in grads_and_vars:
var_name = variable.name.replace(":", "_")
tensorboard.summary.histogram(f"gradients/{var_name}", gradient)
tensorboard.summary.scalar(f"gradient_norm/{var_name}", tf.global_norm([gradient]))
minimize_op = optimizer.apply_gradients(grads_and_vars)