Using Tensorflow and Keras in an Azure Function

Scott Billington 0 Reputation points
2024-07-26T13:40:59.2766667+00:00

I am trying to do a very small machine learning model training in a python function. I just don't want to take the next step of learning everything about machine learning models in Azure to do something this simple.

My core issue is that I can't seem to import the Sequential and Dense parts of keras with the following statements:

#from tensorflow.python.keras.models import Sequential

#from tensorflow.python.keras.layers import Dense

from keras.models import Sequential
from keras.layers import Dense

I can get my function to run right up until the model fit command where it fails for these reasons that were well articulated in a stack overflow link:

https://stackoverflow.com/questions/77733277/attributeerror-module-tensorflow-python-distribute-input-lib-has-no-attribute

The stackoverflow answer is here, but I still can't get this to work no way, no how.

I have tried the v1, v2 azure functions.

I have tried the "from tensorflow.python.keras.models import Sequential" that works for the VScode linter, but will cause the "distribute-input-lib-has-no-attribute" error when I run it in Azure. It runs perfect in the debugger on the desktop.

I have tried reducing the version of tensorflow down to 2.12 and importing keras separately.

I have tried the 'data adapter' trick in the stackoverflow post.

I never get any errors on the deployment or in the logs that give me something to further investigate.

I realize I'm missing something stupid, but I'm not an expert in how the library structures are imported/work.

tensorflow/python/keras code is a legacy copy of Keras since the TensorFlow v2.7 release, and will be deleted in the v2.12 release.

Please remove any import of tensorflow.python.keras and use the public API with

from

or

import

I tried to execute the mentioned code on tensorflow v2.13 by using below imports and it was executed without any issues.

from

I have tried forcing versions of Tensorflow down to 2.10.1, but that causes other issues with numpy, etc...

So, I have just a few questions:

  1. Is there an easy way in azure to train a model without a 10 page setup process? I'm almost tempted to just spin up a VM and be done with it. So, far no "easy button" for Azure ML--or I just haven't found the right thread to follow.
  2. What I am I missing on the library importing with Tensorflow and how can I resolve it?
  3. Is there another library that works more simply to train a multi-layer perceptron style neural net? tensorflow is a huge library to load for this. On my PC, it takes about 30 seconds to train this model.
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,101 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.