Skip to content

Instantly share code, notes, and snippets.

View pweids's full-sized avatar

paul weidinger pweids

View GitHub Profile
@pweids
pweids / strip_comments.py
Created August 4, 2018 21:00
Two different methods of stripping /* ... */ style comments. One using a finite state machine, and one using a more pythonic recursive function. The latter is much faster
import unittest
from enum import Enum
def strip_C_comments(code: str) -> str:
"""
This function strips out C style /* comments */ from code
It is Pythonic and 100x faster than strip_by_char
:param code: the code to be parsed for comments
:return: code without comments
"""
class NeuralNet:
"""edited for brevity """
def feedforward(self):
prev_a = self.inputs
for layer in self.layers:
layer.z = layer.W.dot(prev_a) + layer.b
if layer is not self.layers[-1]: # don't apply relu to last layer
layer.a = self.ReLU(layer.z)
prev_a = layer.a
else: # last layer is a prob distribution + a value in [-1, 1]
@pweids
pweids / mcts.py
Last active June 13, 2018 12:07
MCTS class for ConnectZero
class MCTS:
def __init__(self, c=1.0):
self.c = c # level of exploration. Higher for self-play
# "Each edge stores a set of statistics"
# We will use a defaultdict for the mappings
self.Nsa = defaultdict(int) # visit count
self.Wsa = defaultdict(float) # total action value
self.Qsa = defaultdict(float) # mean action value