Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. program svd
  2. ! find best matrix w and vector b in equation w*p +b = t
  3. ! where p is matrix of column vector of inputs
  4. ! t is matrix of column vector of targets
  5. ! (i suppose b should be a matrix of column vector of b)
  6. ! uses numerical recipes subroutines
  7. ! based on pseudo-inverse obtained form singular value decomposition
  8. ! pages 431-436 in book "learning from data"
  9. use precis
  10. use nrtype; use nrutil; use nr
  11. use utilmod
  12.  
  13. implicit none
  14.  
  15. integer km,im,jm,iseed,irec
  16. real(mrl), allocatable, dimension(:,:) :: wb,wbt,ps,p,t,pt,pn,u,v,w,s
  17. real(mrl), allocatable, dimension(:) :: b,e,p1,t1,sig
  18. integer, allocatable, dimension(:) :: irr
  19. real(mrl), allocatable, dimension(:) :: arr
  20. integer nread,nbatch,nalpha,verbose,init,ima(0:3),ifuna(3),idr
  21. real(mrl) alpha0,alphai,avgerror,funcoa(3),dummy(1),rmstarget
  22. character(80) filea,fileb,filec,netin,netout
  23. character(1000) aline
  24. namelist/params/ima,ifuna,funcoa,nread,nbatch,iseed,&
  25. alpha0,nalpha,alphai,filea,fileb,filec,verbose,init,netin,netout
  26.  
  27. print *,' usage: svd < namelist_input_file > summary_output_file'
  28. read (*,nml=params)
  29. jm=ima(0)
  30. im=ima(1)
  31. if (ima(2)>0) im=ima(2)
  32. if (ima(3)>0) im=ima(3)
  33. km=jm+1
  34.  
  35. print *,'will "train" svd with ',trim(filea),','
  36. open (unit=89,file=trim(filea),status='old')
  37.  
  38. irec=0
  39. 5 call read_input_target_file(89,dummy,dummy,aline,idr)
  40. if (idr>2) goto 10
  41. if (idr>1) goto 5
  42. irec=irec+1
  43. goto 5
  44. 10 print *,' counted irec=',irec
  45.  
  46. rewind (89)
  47.  
  48. allocate(ps(jm,irec),t(im,irec),pt(irec,km),pn(irec,km),p(km,irec))
  49. allocate(w(im,jm),b(im),e(im),p1(jm),t1(im),wb(im,km),wbt(km,im))
  50. allocate(s(km,km),u(irec,km),v(km,km),sig(km))
  51.  
  52. i=1
  53. 15 call read_input_target_file(89,ps(:,i),t(:,i),aline,idr)
  54. if (idr>2) goto 20
  55. if (idr>1) goto 15
  56. p(1:jm,i)=ps(1:jm,i)
  57. p(km,i)=1._mrl
  58. i=i+1
  59. goto 15
  60. 20 print *,' finished reading in p and t'
  61.  
  62. print *,'p:'
  63. if (verbose >= 1) call printmat(p)
  64.  
  65. pt=transpose(p)
  66. print *,'pt:'
  67. if (verbose >1) call printmat(pt)
  68.  
  69. u=pt
  70.  
  71. !print *, u(i-1,:)
  72. call svdcmp(u,sig,v)
  73. !print *, u(i-1,:)
  74. !stop
  75.  
  76. print *,'singular value decomp:'
  77. if (verbose >= 1) call printmat(u)
  78. if (verbose >= 1) call printcol(sig)
  79. if (verbose >= 1) call printmat(v)
  80.  
  81. s=0._mrl
  82. do i=1,km
  83. s(i,i)=sig(i)
  84. end do
  85. if (verbose >= 1) call printmat(s)
  86.  
  87. pn=matmul(u,matmul(s,transpose(v)))
  88. print *,'test svd reconstruction:'
  89. print *, pn(1,:)-p(:,1)
  90. if (verbose >= 1) call printmat(pn)
  91.  
  92. s=0._mrl
  93. do i=1,km
  94. s(i,i)=1./sig(i)
  95. end do
  96.  
  97. print *, 'sig+:'
  98. if (verbose >= 1) call printmat(s)
  99.  
  100. p=matmul(v,matmul(s,transpose(u)))
  101. print *,'x+:'
  102. if (verbose >= 1) call printmat(p)
  103. wb=transpose(matmul(p,transpose(t)))
  104.  
  105. print *, 'wb'
  106. if (verbose >= 1) call printmat(wb)
  107.  
  108. w=wb(:,1:jm)
  109. b=wb(:,km)
  110. print *, 'w:'
  111. if (verbose >= 0) call printmat(w)
  112. print *, 'b:'
  113. if (verbose >= 0) call printcol(b)
  114.  
  115. close (unit=89)
  116.  
  117. call make_pred_file(filea,'svd_a.dat')
  118. call make_pred_file(fileb,'svd_b.dat')
  119. call make_pred_file(filec,'svd_c.dat')
  120.  
  121. print *,'sorted absolute values of first row of weights:'
  122. allocate(arr(jm),irr(jm))
  123. do i=1,jm
  124. arr(i)=abs(w(1,i))
  125. irr(i)=i
  126. end do
  127.  
  128. call sort_down2(arr,irr)
  129. do i=1,jm
  130. print *,irr(i),arr(i)
  131. end do
  132.  
  133. contains
  134. subroutine make_pred_file(infile,outfile)
  135. character(*) infile, outfile
  136. print *,'will verify performance of svd with ',trim(infile),','
  137. open (unit=89,file=trim(infile),status='old',err=101)
  138. print *,'prediction vs. target is output in file ',trim(outfile)
  139. open (unit=91,file=trim(outfile),status='unknown')
  140. write (91,*) '# svd predicted vector, target vector for ',trim(infile)
  141. write (91,*) '# ',trim(infile),' had these comments:'
  142. avgerror=0.
  143. rmstarget=0.
  144. irec=0
  145. 25 call read_input_target_file(89,p1,t1,aline,idr)
  146. if (idr>2) goto 30
  147. if (idr == 2) then
  148.  
  149.  
  150.  
  151. write(91,'(a)') trim(aline)
  152. else
  153.  
  154.  
  155.  
  156. irec=irec+1
  157.  
  158.  
  159.  
  160. e=matmul(w,p1)+b
  161.  
  162.  
  163.  
  164. write (91,'(30e17.8)') e,t1
  165.  
  166.  
  167.  
  168. e=e-t1
  169.  
  170.  
  171.  
  172. avgerror=avgerror+dot_product(e,e)
  173.  
  174.  
  175.  
  176. rmstarget=rmstarget+dot_product(t1,t1)
  177. end if
  178. goto 25
  179. 30 close (unit=89)
  180. avgerror=avgerror/irec
  181. rmstarget=rmstarget/irec
  182. write (91,*) '#',trim(infile),' mse=',avgerror,' mst=',rmstarget
  183. close (unit=91)
  184.  
  185. print *, ' average square error with ',trim(infile),' is mse=',avgerror
  186. print *, ' average square of target in ',trim(infile),' is mst=',rmstarget
  187. 101 return
  188. end subroutine make_pred_file
  189.  
  190. end program svd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement