|
| 1 | +program example_montesinos_uni |
| 2 | + use mod_kinds,only:ik,rk |
| 3 | + use mod_network,only:network_type |
| 4 | + implicit none |
| 5 | + integer(ik)::ny1_tr,ny2_tr,nx1_tr,nx2_tr |
| 6 | + integer(ik)::ny1_ts,ny2_ts,nx1_ts,nx2_ts |
| 7 | + |
| 8 | + integer(ik)::batch_size,num_epochs |
| 9 | + |
| 10 | + real(rk),allocatable::y_tr(:,:),x_tr(:,:) |
| 11 | + real(rk),allocatable::y_ts(:,:),x_ts(:,:) |
| 12 | + |
| 13 | + type(network_type)::net |
| 14 | + |
| 15 | + call readfile('../data/montesinos_uni/y_tr.dat',ny1_tr,ny2_tr,y_tr) |
| 16 | + call readfile('../data/montesinos_uni/x_tr.dat',nx1_tr,nx2_tr,x_tr) |
| 17 | + |
| 18 | + !net=network_type([nx1_tr,50,50,ny1_tr],'relu') |
| 19 | + net=network_type([nx1_tr,50,50,ny1_tr]) |
| 20 | + |
| 21 | + batch_size=30 |
| 22 | + num_epochs=20 |
| 23 | + |
| 24 | + !training |
| 25 | + call net%fit(x_tr,y_tr,3._rk,epochs=num_epochs,batch_size=batch_size) |
| 26 | + |
| 27 | + call net%sync(1) |
| 28 | + |
| 29 | + !validation |
| 30 | + call readfile('../data/montesinos_uni/y_ts.dat',ny1_ts,ny2_ts,y_ts) |
| 31 | + call readfile('../data/montesinos_uni/x_ts.dat',nx1_ts,nx2_ts,x_ts) |
| 32 | + |
| 33 | + if(this_image().eq.1)then |
| 34 | + write(*,*)'Correlation(s): ',corr_array(net%predict(x_ts),y_ts) |
| 35 | + endif |
| 36 | + |
| 37 | +contains |
| 38 | + |
| 39 | +subroutine readfile(filename,n,m,array) |
| 40 | + character(len=*),intent(in)::filename |
| 41 | + integer(ik),intent(out)::n,m |
| 42 | + real(rk),allocatable,intent(out)::array(:,:) |
| 43 | + |
| 44 | + integer(ik)::un,i,io |
| 45 | + |
| 46 | + open(newunit=un,file=filename,status='old',action='read') |
| 47 | + call numlines(un,m) |
| 48 | + call numcol(un,n) |
| 49 | + |
| 50 | + allocate(array(n,m)) |
| 51 | + rewind(un) |
| 52 | + do i=1,m |
| 53 | + read(un,*,iostat=io)array(:,i) |
| 54 | + if(io.ne.0)exit |
| 55 | + enddo |
| 56 | + close(un) |
| 57 | + |
| 58 | +end subroutine |
| 59 | + |
| 60 | +pure function corr_array(array1,array2) result(a) |
| 61 | + real(rk),intent(in)::array1(:,:),array2(:,:) |
| 62 | + real(rk),allocatable::a(:) |
| 63 | + |
| 64 | + integer(ik)::i,n |
| 65 | + |
| 66 | + n=size(array1,dim=1) |
| 67 | + |
| 68 | + allocate(a(n)) |
| 69 | + a=0.0_rk |
| 70 | + do i=1,n |
| 71 | + a(i)=corr(array1(i,:),array2(i,:)) |
| 72 | + enddo |
| 73 | + |
| 74 | +end function |
| 75 | + |
| 76 | +pure real(rk) function corr(array1,array2) |
| 77 | + real(rk),intent(in)::array1(:),array2(:) |
| 78 | + |
| 79 | + real(rk)::mean1,mean2 |
| 80 | + |
| 81 | + !brute force |
| 82 | + |
| 83 | + mean1=sum(array1)/size(array1) |
| 84 | + mean2=sum(array2)/size(array2) |
| 85 | + corr=dot_product(array1-mean1,array2-mean2)/sqrt(sum((array1-mean1)**2)*sum((array2-mean2)**2)) |
| 86 | + |
| 87 | +end function |
| 88 | + |
| 89 | +subroutine numlines(unfile,n) |
| 90 | + implicit none |
| 91 | + integer::io |
| 92 | + integer,intent(in)::unfile |
| 93 | + integer,intent(out)::n |
| 94 | + rewind(unfile) |
| 95 | + n=0 |
| 96 | + do |
| 97 | + read(unfile,*,iostat=io) |
| 98 | + if (io.ne.0) exit |
| 99 | + n=n+1 |
| 100 | + enddo |
| 101 | + rewind(unfile) |
| 102 | +end subroutine |
| 103 | + |
| 104 | +subroutine numcol(unfile,n) |
| 105 | + implicit none |
| 106 | + integer,intent(in)::unfile |
| 107 | + character(len=1000000)::a |
| 108 | + integer,intent(out)::n |
| 109 | + integer::curr,first,last,lena,stat,i |
| 110 | + rewind(unfile) |
| 111 | + read(unfile,"(a)")a |
| 112 | + curr=1;lena=len(a);n=0 |
| 113 | + do |
| 114 | + first=0 |
| 115 | + do i=curr,lena |
| 116 | + if (a(i:i) /= " ") then |
| 117 | + first=i |
| 118 | + exit |
| 119 | + endif |
| 120 | + enddo |
| 121 | + if (first == 0) exit |
| 122 | + curr=first+1 |
| 123 | + last=0 |
| 124 | + do i=curr,lena |
| 125 | + if (a(i:i) == " ") then |
| 126 | + last=i |
| 127 | + exit |
| 128 | + endif |
| 129 | + enddo |
| 130 | + if (last == 0) last=lena |
| 131 | + n=n+1 |
| 132 | + curr=last+1 |
| 133 | + enddo |
| 134 | + rewind(unfile) |
| 135 | +end subroutine |
| 136 | + |
| 137 | +end program |
0 commit comments