Skip to content

Support for tensorflow or keras? #35

@xushanthu-2014

Description

@xushanthu-2014

Hi I want to use forpy to import tensorflow or keras in one program. The code of fortran90 simply follows:

program keras_demo
    use forpy_mod
    implicit none
    
    integer :: ierror
    type(module_py) :: tensorflow

    ierror = forpy_initialize()
    ierror = import_py(tensorflow, "tensorflow")

    write(*,*) "tensorflow imported return value ", ierror
    call tensorflow%destroy
    call forpy_finalize
  
  end program

Then I run the command gfortran -c forpy_mod.F90 and gfortran intro_to_forpy.F90 forpy_mod.o `python3-config --ldflags --embed` . The ./a.out shows tensorflow imported return value -1. When I run gfortran intro_to_forpy.F90 forpy_mod.o `python3-config --ldflags` , it returns a long error message which ends with

ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status 

In your code it was said that the value of ierror should be 0 if the import py_module is successful. And I also checked the python version on my machine. In my VS Code terminal, I ran following codes and got returned messages as following:

$~ python --version
Python 2.7.16

$~ which python
/usr/bin/python

$~ which python-config
/usr/bin/python-config

$~ python-config --ldflags
 -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 -ldl -framework CoreFoundation

$~ ls /usr/lib/libpython*
/usr/lib/libpython.dylib        /usr/lib/libpython2.7.dylib

But when I tried the same commands in terminal of my own Mac, it returned a different message:

$~ python --version
Python 3.7.10

$~ which python
/Users/xushan/opt/anaconda3/bin/python

$~ which python-config
/usr/bin/python-config

$~ python-config --ldflags
 -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 -ldl -framework CoreFoundation

$~ ls /usr/lib/libpython*
/usr/lib/libpython.dylib        /usr/lib/libpython2.7.dylib

For the keras path, it's

from tensorflow import keras
keras.__path__
['/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorflow/keras']

import tensorflow as tf
tf.__path__
['/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorflow',
 '/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/api/_v2',
 '/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorboard/summary/_tf',
 '/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorflow',
 '/Users/xushan/opt/anaconda3/lib/python3.7/site-packages/tensorflow/_api/v2']

So does that mean my tensorflow is installed on my anoconda and forpy is not able to import it? I guess I need to do something to specify the python version? Thanks!

(btw, if I want to write something in fortran using your forpy to from keras.models import load_model, how can I write that? do I need to firstly import tensorflow in fortran90 subroutine, then import models, finally import load_model? is there any way to simply it? Thanks!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions