What's the difference between tensorflow.python.keras and tensorflow.keras? - tensorflow

As the title says, Are they the same api? When I print the layers module in keras, the result are shown as follow:
from tensorflow.keras import layers
print(layers)
from tensorflow.python.keras import layers
print(layers)
result
<module 'tensorflow.python.keras.api._v1.keras.layers' from '/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/api/_v1/keras/layers/__init__.py'>
<module 'tensorflow.python.keras.layers' from '/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/__init__.py'>
We can see that two modules come from different source.
And I find the api module from source code, there is only a BUILD file.
Is there a relation between two modules, what is the mechanism of the api generator?

Anything under tf.python.* is private, intended for development only, rather than for public use.
Importing from tensorflow.python or any other modules (including import tensorflow_core...) is not supported, and can break unannounced.
So, it is suggested not to use anything with tf.python.*.

Related

Why do we use keras back-end command in codes?

import tensorflow as tf
from tensorflow import keras
from keras import backend as K
What is the reason behind using the command—>
from keras import backend as K
What does it do? I would appreciate it if anyone explains it the simple way so that it does not get complicated in the mind.
You can find more information on what Keras backend actually is here or here.
In simpler terms to understand what Keras backend actually is
Keras is a model-level library that provides high-level building blocks for developing deep learning models. Keras does not provide low-level operations such as tensor multiplication and convolution. Instead, it relies on a specialized, well-optimized tensor library that serves as Keras' "backend engine". Instead of choosing one single tensor library and tying your Keras implementation to that library, Keras handles the problem in a modular way, allowing you to seamlessly connect multiple different backend engines to Keras.
Keras backend will allow you to write custom code or in a particular case a new "Keras module" for your use case that can support Theano and/or Tensorflow both. Like instead of tf.placeholder() you could write keras.backend.placeholder() which will work across both the libraries mentioned earlier.

Understand relation between different tensorflow AdagradOptimizer APIs

I am new to tf, and during reading a model code, I noticed it used 1), but most document I can find are using 2) and 3). So what is the tensorflow.python library used for,seems it is not in official document? And what is the relation between 1 to 2,3?
from tensorflow.python.training.adagrad import AdagradOptimizer
from tf.compat.v1.train import AdagradOptimizer
from tf.keras.optimizers import Adagrad
Basically:
tensorflow.python is essentially "internal" code and not part of the public API. You should never use anything in there directly. It may work, but it can also lead to instabilities, break completely if you update your TF version, etc.
This is a hold-over from old TF versions, before Keras was tightly integrated with it. IMHO you should just forget that this exists and they should have just removed it completely. This would be used with the outdated layers interface (tf.compat.v1.layers).
This is what you should use if using tf.keras (models and/or layers) and should be your go-to interface (not necessarily Adagrad specifically, but all the optimizers in tf.keras.optimizers).

How to use tensorflow library with sagemaker preprocessing

I want to use TensorFlow for preprocessing in sagemaker pipelines.
But, I haven't been able to find a way to use it.
Right now, I'm using this library for preprocessing:
from sagemaker.sklearn.processing import SKLearnProcessor
framework_version = "0.23-1"
sklearn_processor = SKLearnProcessor(
framework_version=framework_version,
instance_type=processing_instance_type,
instance_count=processing_instance_count,
base_job_name="abcd",
role=role,
)
Now, I need to use TensorFlow in preprocessing but the python module cant import TensorFlow.
Any help would be much appreciated. Thanks.
So there's no TensorFlow Processor module, the list of available processing modules are in the following doc and can be seen at the bottom of the page.
You can use a Processor and ScriptProcessor class and pass in your TF preprocessing as a script with a requirements.txt for the necessary modules.
I work for AWS and my opinions are my own.

what does it take to convert theano backend to tensorflow backend with Keras?

I'm trying to convert https://github.com/hexiangnan/neural_collaborative_filtering
The library uses theano as a backend, how involved is it to change to tensorflow backend?
It uses keras as well
I only see import theano.tensor as T when I search for theano and import
Would it be as simple as changing the import statement to tensorflow?
some of import statements that look relavant are:
import theano
import theano.tensor as T
import keras
from keras import backend as K
You also need to change the keras backend parameters. This can be done in two ways as described in the documentation one is changing the keras configuration file and the other is changing the environment variable at the top of your script
import os
os.environ['KERAS_BACKEND']='tensorflow'
Probably also worth checking if theano and theano.tensor are used anywhere else, but that should do the trick!

Fix no module 'layer' in current Google Colab TF version

I use Google colab (python3 GPU)
I want to run for example this repo codes but I have an error when running demo ipynb in these lines:
import tensorflow as tf
from layers import (_causal_linear, _output_linear, conv1d, dilated_conv1d)
When I run these two lines I have an error "no module layer"
I don't think this is a bug or something, because this repo has over 1000 stars.
I think this is rather a tf version problem.
Any ideas how to solve this?
"Layers" is a module in the package that you linked NOT in Tensorflow. See here.
BTW if you wanted to import from a submodule of tensor flow you would have to do from tensorflow.package import ....