Advertisement
Guest User

loss

a guest
Nov 11th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.23 KB | None | 0 0
  1. import random
  2. import time
  3. import math
  4. #import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. from matplotlib import cm
  7. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  8. import matplotlib.pyplot as plt
  9. import numpy as np
  10.  
  11.  
  12. def loss(w0,w1, data):
  13.         dataSum=0.0
  14.         for d in data:
  15.             xd = d[0]
  16.             yd = d[1]
  17.             hwxd=float(w0)+float(w1*xd)
  18.             tmp = pow((yd-float(hwxd)),2)
  19.             dataSum+= tmp
  20.         #return(math.sqrt(1.0/(2.0*len(data))*dataSum))
  21.         return(1.0/(2.0*len(data))*dataSum)
  22.         #return(rmse(w0,w1, data))
  23.  
  24. def rmse(w0,w1, data):
  25.         w0= w0 *1000
  26.         w1=0.3047073401501218
  27.         dataSum=0.0
  28.         for d in data:
  29.             xd = d[0]*1000
  30.             yd = d[1]*1000
  31.             hwxd=float(w0)+float(w1*xd)
  32.             tmp = pow((yd-float(hwxd)),2)
  33.             dataSum+= tmp
  34.         return(math.sqrt(1.0/(2.0*len(data))*dataSum))
  35.         #return(1.0/(2.0*len(data))*dataSum)
  36.  
  37. def gradDesc(eta,maxit,data):
  38.  
  39.     # wi := wi - eta(deriv(w))
  40.  
  41.     w0 = random.random()
  42.     w1 = random.random()
  43.     w0 = 0
  44.     w1=0
  45.  
  46.     # w0=-32.632
  47.     # w1=0.304315
  48.     #
  49.     # nw0 = -32.632
  50.  
  51.     threshold = 0.1
  52.     iterations =0
  53.     lossfunc=loss(w0,w1,data)
  54.     print('initial loss =', loss(w0,w1,data))
  55.  
  56.  
  57.     # fig = plt.figure()
  58.     # ax = fig.gca(projection='3d')
  59.  
  60.     # plt.ion()
  61.     # plt.show()
  62. #    graph.plot(x,y,'ro',markersize=5)
  63.  
  64.     xarr=[]
  65.     yarr=[]
  66.     lossf=[]
  67.     w0arr=[]
  68.     w1arr=[]
  69.     while(iterations < maxit):
  70.         iterations+=1;
  71.         print('iter:',iterations)
  72.  
  73.         w0sum=0.0
  74.         for d in data:
  75.             xd = d[0]
  76.             yd = d[1]
  77.             hwxd=float(w0)+float(w1*xd)
  78.             tmp = (float(hwxd)-yd)
  79.             w0sum+= tmp
  80.         w0sum = (1.0/len(data))*w0sum
  81.         nw0=w0-(eta*w0sum)
  82.  
  83.  
  84.         w1sum=0.0
  85.         for d in data:
  86.             xd = d[0]
  87.             yd = d[1]
  88.             hwxd=float(w0)+float(w1*xd)
  89.             tmp = (float(hwxd)-yd)*xd
  90.             w1sum+= tmp
  91.         w1sum = (1.0/len(data))*w1sum
  92.         nw1=w1-(eta*w1sum)
  93.  
  94.         w0=nw0
  95.         w1=nw1
  96.  
  97.         prevlossfunc = lossfunc
  98.         lossfunc=loss(w0,w1,data)
  99.         w0arr.append(w0)
  100.         w1arr.append(w1)
  101.         lossf.append(lossfunc)
  102.         # plt.scatter(w1,lossfunc)
  103.         #
  104.         # plt.draw()
  105.         #time.sleep(0.05)
  106.  
  107.     print(iterations,w0,w1,lossfunc, rmse(w0,w1,data))
  108.  
  109.  
  110.  
  111.     xmin = 1000000000000
  112.     xmax =0
  113.     for d in data:
  114.         xarr.append(d[0])
  115.         yarr.append(d[1])
  116.         if (d[0] < xmin):
  117.             xmin=d[0]
  118.         if (d[0] > xmax):
  119.             xmax=d[0]
  120.  
  121.     fig=plt.figure()
  122.     graph= fig.add_subplot(111)
  123.     graph.plot(xarr,yarr,'ro',markersize=5)
  124.  
  125.     #(w0+(xarr[0]*w1))
  126.     graph.plot([xmin,xmax],[(w0+(xmin*w1)),(w0+(xmax*w1))])
  127.  
  128.     fig2=plt.figure()
  129.     graph2= fig2.add_subplot(111)
  130.     graph2.plot(w0arr,lossf,'r-',label='w0')
  131.  
  132.     graph2.plot(w1arr,lossf,'g-',label='w1')
  133.     graph2.legend(loc=0)
  134.     plt.show()
  135.  
  136.  
  137.     # for d in data:
  138.     #     xarr.append(d[0])
  139.     #     yarr.append(d[1])
  140.     # plt.scatter(xarr,yarr)
  141.     #
  142.  
  143. #    plt.plot([200,1000], [((w1*200)+w0),((w1*1000)+w0)] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement