Forums

Switched Keras backend to "theano" in json but the backend remains Tensorflow.

As the title states, I am trying to switch the Keras backend to Theano in order to run a machine learning model but am having some issues. I started by switching the Keras backend to Theano in the .json file as shown below:

{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "theano",
"image_data_format": "channels_last"
 }

I also switched the Keras to import directly instead of from Tensorflow and am also importing Theano even though I am unsure if I have to.

import theano
from plivo import plivoxml
import keras

I am reading in my model but when I check the Keras backend remains Tensorflow and is unable to load.

print("Reading in the model now")
print("Keras backend is: ", keras.backend.backend())
model = keras.models.load_model('/home/NudgeStudyChatbot/mysite/lstm')

I get the normal server log messages as if I were to run Keras from Tensorflow and the backend remains Tensorflow.

2021-02-17 22:42:55 Reading in the model now
2021-02-17 22:42:55 Keras backend is:  tensorflow
...
2021-02-17 22:42:55 2021-02-17 22:42:55.262148: F tensorflow/core/platform/env.cc:351] Check failed: -1 != path_length (-1 vs. -1)
2021-02-17 22:42:55 !!! uWSGI process 1 got Segmentation Fault !!!

I am hosting the code using a Flask web app with python 3.8. The model being loaded is trained using Keras from Tensorflow, but it takes a while to train so I would prefer not to retrain if possible. Any help or guidance would be greatly appreciated! If more information is needed just let me know!

I'm not ML specialist, but it looks like when you unpickle your model when you load it, it comes with whatever dependencies it was pickled with.

That's what I thought might be an issue as well, but in my code, I check the Keras backend before reading in my model and it remains as TensorFlow. So the model itself is not influencing the backend, I'm just not sure outside of changing the JSON how to actually change the backend of Keras to Theano.

Which JSON file did you put that change into? It should be /home/NudgeStudyChatbot/.keras/keras.json.

Another thing to try would be putting this just before the line where you import keras:

import os; os.environ['KERAS_BACKEND'] = 'theano'

Yes the JSON file path is /home/NudgeStudyChatbot/.keras/keras.json. In addition, I tried the code you provided before and after importing Keras and the backend remained TensorFlow. I also tried the following code I found just in case and got an assertion error:

from keras import backend as K
import os
from importlib import reload
def set_keras_backend(backend):

    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        reload(K)
        assert K.backend() == backend

set_keras_backend("theano")

That has been confirmed to work, but we just learned that only for Keras up to version 2.3.1. Unfortunately Keras is TensorFlow-only since 2.4.

Ah, that makes sense thank you, we will have to downgrade back to Keras 2.3.1.

We may want to eventually update to a new version, are there any plans to integrate Keras' future versions into PythonAnywhere?

It is possible, but we do not know when or whether we will do it.