CHanged to Julia lang
This commit is contained in:
parent
7c1f5de6d5
commit
5bdb53b360
|
@ -0,0 +1,48 @@
|
|||
using LinearAlgebra
|
||||
using Distributions
|
||||
using Random
|
||||
using Zygote
|
||||
using Plots
|
||||
|
||||
Random.seed!(0);
|
||||
|
||||
function normalgenerator(amount::Number,μ::Number,σ::Number,lowerbound::Number=0,upperbound::Number=1)
|
||||
return rand(Truncated(Normal(μ,σ),lowerbound,upperbound),amount)
|
||||
end
|
||||
|
||||
function noisyline(intercept::Number,slope::Number,samples::Number,μ::Number,σ::Number,lb::Number=0,ub::Number=1)
|
||||
noise = normalgenerator(samples,μ,σ,lb,ub)
|
||||
exes = Array{Float64,1}(undef,samples)
|
||||
for i in 1:samples
|
||||
exes[i] = i
|
||||
end
|
||||
line = slope .* exes .+ intercept
|
||||
y = noise .+ line
|
||||
w = ones(samples)
|
||||
X = hcat(exes,w)
|
||||
return X,y
|
||||
end
|
||||
|
||||
function MSE(X::Array,y::Array,w::Array)
|
||||
return mean((y - X * w).^2)
|
||||
end
|
||||
|
||||
function gradient_descent(X::Array,y::Array,α::Number,w::Array,iter::Number)
|
||||
costs = Array{Float64,1}(undef,iter)
|
||||
for i in 1:iter
|
||||
costs[i] = MSE(X,y,w)
|
||||
∇X, ∇y, ∇w = gradient(MSE,X,y,w)
|
||||
w = w - α * ∇w
|
||||
end
|
||||
return w,costs
|
||||
end
|
||||
|
||||
function get_res_line(X::Array,result::Array)
|
||||
return result[1] .* X[:,1] .+ result[2]
|
||||
end
|
||||
|
||||
|
||||
X,y = noisyline(2,4,100,0,1);
|
||||
N,D = size(X);
|
||||
w = ones(D);
|
||||
pred,cost = gradient_descent(X,y,0.0001,w,6);
|
66
cours_ex1.py
66
cours_ex1.py
|
@ -1,66 +0,0 @@
|
|||
import random
|
||||
import numpy as np
|
||||
from numpy import arange
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
random.seed(0)
|
||||
|
||||
def line_w_gauss_noise(inter,slope,noise,numPoints):
|
||||
x = np.zeros(shape=(numPoints, 2))
|
||||
y = np.zeros(shape=numPoints)
|
||||
for i in range(0, numPoints):
|
||||
x[i][0] = i
|
||||
x[i][1] = 1
|
||||
y[i] = (i + inter) + random.uniform(0, noise) * slope
|
||||
return x, y
|
||||
|
||||
|
||||
def MSE(x,y,weights):
|
||||
N,D = np.shape(x)
|
||||
x_T = x.transpose()
|
||||
y_hat = np.dot(weights,x_T)
|
||||
return np.sum((y - y_hat)**2) / N
|
||||
|
||||
|
||||
def dMSE(x,y,weights):
|
||||
N = len(x)
|
||||
inter = np.sum(np.dot(np.dot(x[:,1],weights[1]) - y,x[:,1])) * (2/N)
|
||||
slope = np.sum(np.dot(x[:,0],weights[0]) - y,) * (2/N)
|
||||
new_weight = np.array([slope,inter])
|
||||
return new_weight
|
||||
|
||||
def gradient_desscent(x,y,alpha,weights,iter):
|
||||
losses = list()
|
||||
costs = list()
|
||||
for n in range(iter):
|
||||
cost = MSE(x,y,weights)
|
||||
loss = np.sum(y - np.dot(x,weights))
|
||||
losses.append(loss)
|
||||
costs.append(cost)
|
||||
print(cost)
|
||||
print(loss)
|
||||
print(n)
|
||||
if np.abs(losses[n]) > np.abs(losses[n-1]):
|
||||
break
|
||||
|
||||
weights = weights - (alpha * dMSE(x,y,weights))
|
||||
return weights,costs,losses
|
||||
|
||||
if __name__ == '__main__':
|
||||
x,y = line_w_gauss_noise(1,2,5,100)
|
||||
num_var = len(x.transpose())
|
||||
w = np.ones(num_var)
|
||||
|
||||
result,cost,loss = gradient_desscent(x,y,0.001,w,80)
|
||||
print(result)
|
||||
y_hat = []
|
||||
for i in x[:,0]:
|
||||
y_hat.append(result[1]+(result[0]*i))
|
||||
|
||||
plt.scatter(x[:,0],y)
|
||||
plt.plot(x[:,0],y_hat)
|
||||
plt.show()
|
||||
|
||||
#plt.plot(cost)
|
||||
#plt.plot(loss)
|
||||
#plt.show()
|
Loading…
Reference in New Issue