import tensorflow as tf
import pandas as pd
import numpy as np
from show import show
data = pd.read_csv('test.csv', header=None)
data = np.array(data)
print(data)
X_np = np.array(data[:,[0,1]])
y_np = np.array(data[:,[2]])
[[0.28488 0.52142 1. ] [0.27633 0.21264 1. ] [0.39748 0.31902 1. ] [0.5533 1. 0. ] [0.44274 0.59205 0. ] [0.85176 0.6612 0. ] [0.60436 0.86605 0. ]]
x = tf.constant( X_np, dtype=tf.float64 )
y = tf.constant( y_np, dtype=tf.float64 )
w_tensor = tf.constant([[-1, 1]], dtype=tf.float64)
b_tensor = tf.constant([[0]], dtype=tf.float64)
w = tf.Variable(w_tensor)
b = tf.Variable(b_tensor)
alpha = 0.1
epochs = 500
wHistory = []
lossHistory = []
def train():
global w, b
#record forward operations
with tf.GradientTape() as tape:
z = x @ tf.transpose(w) + b
y_hat = tf.math.sigmoid(z)
loss_points = -y * tf.math.log(y_hat) - (1-y) * tf.math.log(1-y_hat)
loss = tf.math.reduce_mean(loss_points)
#backward: compute gradients
[dl_dw, dl_db] = tape.gradient(loss, [w, b])
#update weights
w.assign(w - alpha * dl_dw)
b.assign(b - alpha * dl_db)
wHistory.append([w.numpy()[0][0],w.numpy()[0][1],b.numpy()[0][0]])
lossHistory.append(loss.numpy())
for e in range(epochs):
train()
show(data, epochs, wHistory, lossHistory,[-1,1])