Skip to content

Instantly share code, notes, and snippets.

View cedricconol's full-sized avatar

Cedric Conol cedricconol

  • Philippines
View GitHub Profile
@cedricconol
cedricconol / linregtf.py
Created May 23, 2020 03:48
Linear regression in TF2
class SimpleLinearRegression:
def __init__(self, initializer='random'):
if initializer=='ones':
self.var = 1.
elif initializer=='zeros':
self.var = 0.
elif initializer=='random':
selfx.var = tf.random.uniform(shape=[], minval=0., maxval=1.)
self.m = tf.Variable(1., shape=tf.TensorShape(None))
@cedricconol
cedricconol / linregtftrain.py
Created May 23, 2020 03:43
Linear regression TF2 train
def train(self, X, y, learning_rate=0.01, epochs=5):
if len(X.shape)==1:
X=tf.reshape(X,[X.shape[0],1])
self.m.assign([self.var]*X.shape[-1])
for i in range(epochs):
print("Epoch: ", i)
@cedricconol
cedricconol / linregtfpredict.py
Created May 23, 2020 03:10
Linear regression TF2 predict
def predict(self, x):
return tf.reduce_sum(self.m * x, 1) + self.b
@cedricconol
cedricconol / linregtfupdate.py
Created May 23, 2020 03:10
Linear regression TF2 update params
def update(self, X, y, learning_rate):
with tf.GradientTape(persistent=True) as g:
loss = self.mse(y, self.predict(X))
print("Loss: ", loss)
dy_dm = g.gradient(loss, self.m)
dy_db = g.gradient(loss, self.b)
self.m.assign_sub(learning_rate * dy_dm)
@cedricconol
cedricconol / linregtfmse.py
Created May 23, 2020 03:09
Linear regression TF2 MSE
def mse(self, true, predicted):
return tf.reduce_mean(tf.square(true-predicted))
@cedricconol
cedricconol / linregtfinit.py
Created May 23, 2020 03:08
Linear regression TF2 init
class SimpleLinearRegression:
def __init__(self, initializer='random'):
if initializer=='ones':
self.var = 1.
elif initializer=='zeros':
self.var = 0.
elif initializer=='random':
self.var = tf.random.uniform(shape=[], minval=0., maxval=1.)
self.m = tf.Variable(1., shape=tf.TensorShape(None))
@cedricconol
cedricconol / optimizedbubble.go
Created May 6, 2020 04:21
optimized bubble sort in go
func bubblesort(list []int, ascending bool) {
var N int = len(list)
var count int
for i := 1; i < N; i++ {
swapCount := pass(list, ascending)
count++
if swapCount == 1 {
break
}
@cedricconol
cedricconol / modifiedswap.go
Created May 6, 2020 04:13
optimized swap in bubble sort
func pass(list []int, ascending bool) int {
var N int = len(list)
var swapCount int
for i := 0; i < N-1; i++ {
var firstElement int = list[i]
var secondElement int = list[i+1]
if ascending == true {
if firstElement > secondElement {
@cedricconol
cedricconol / bubblemain.go
Created May 6, 2020 04:07
main function for bubble sort go
func main() {
var numbers []int = []int{33, 91, 76, 8, 22}
var ascending bool = true
fmt.Println("Original list: ", numbers)
bubblesort(numbers, ascending)
fmt.Println("Sorted list: ", numbers)
}
@cedricconol
cedricconol / bubblesort.go
Created May 5, 2020 09:02
bubblesort function in go
func bubblesort(list []int, ascending bool) {
var N int = len(list)
var count int
for i := 1; i < N; i++ {
pass(list, ascending)
count++
}
fmt.Println("Number of iterations: ", count)