Skip to content

Commit 2c30887

Browse files
author
Vandenplas, Jeremie
committed
addition of the montesinos_uni example
1 parent 16f6eea commit 2c30887

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ foreach(execid mnist network_save network_sync)
7878
add_test(test_${execid} bin/test_${execid})
7979
endforeach()
8080

81-
foreach(execid mnist simple sine)
81+
foreach(execid mnist montesinos_uni simple sine)
8282
add_executable(example_${execid} src/tests/example_${execid}.f90)
8383
target_link_libraries(example_${execid} neural ${LIBS})
8484
add_test(example_${execid} bin/example_${execid})

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,26 @@ for example on 16 cores using [OpenCoarrays](https://github.com/sourceryinstitut
309309
$ cafrun -n 16 ./example_mnist
310310
```
311311

312+
### Montesinos-Lopez et al. (2018) example
313+
314+
The Montesinos-Lopez et al. (2018) example is extracted from the study:
315+
316+
```
317+
Montesinos-Lopez et al. 2018. Multi-environment genomic prediction of plant traits using deep learners with dense architecture. G3, 8, 3813-3828.
318+
```
319+
320+
This example uses the data from the dataset "Data\_Maize\_1to3", and was extracted using the R code in the Appendix.
321+
322+
323+
The Montesinos-Lopez data is included with the repo and you will have to unpack it first:
324+
325+
```
326+
cd data/montesinos_uni
327+
tar xzvf montesinos_uni.tar.gz
328+
cd -
329+
```
330+
331+
312332
## Contributing
313333

314334
neural-fortran is currently a proof-of-concept with potential for

data/montesinos_uni.tar.gz

1.73 MB
Binary file not shown.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)