This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict(self, x): | |
return tf.reduce_sum(self.m * x, 1) + self.b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mse(self, true, predicted): | |
return tf.reduce_mean(tf.square(true-predicted)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder