mxnet import nd or np to use arrays - python-3.x

I'm starting to study mxnet and gluon, but I'm getting somewhat confused about the usage of np/nd arrays.
As suggested on the gluon website, I installed mxnet and gluon by running:
pip install --upgrade mxnet gluoncv
which installs mxnet version 1.5.1.post0. In this case, to use arrays I need:
from mxnet import ndarray as nd
On the other hand, I found a deep learning book based on mxnet, where they have you install a later mxnet version by:
pip install mxnet==1.6.0b20190915
and in this case, you can either import ndarray or directly np, so:
from mxnet import ndarray as nd
from mxnet import np
both work (while, with mxnet 1.5.1, from mxnet import np fails).
Why is it possible to import np in the new version, if we had nd already? Are there any differences between arrays created from nd or np?
It seems that I can use mxnet features (such as attach_grad()) in both cases...for example, the following works:
from mxnet import np
array = np.array([1,2,3)
array.attach_grad()
Thanks!

Here is the RFC explaining the motivation of introducing mx.np module. To give a couple of highlighted differences between mx.np and mx.nd modules:
mx.np module adopts the official NumPy operator API, which has evolved for almost twenty years, to provide an easy transitioning experience for users coming from the NumPy world to deep learning, while the development of mx.nd module did not follow a well-established spec to define operator signatures, which sometimes caused confusions, e.g., dim vs axis.
Operators registered in mx.np have highly compatible behaviors with the official NumPy operators, while mx.nd operators do not share such aspects. For example, zero-dim/scalar, zero-size, boolean tensors, and boolean indexing, are supported in mx.np, but not in mx.nd.
You can consider mx.np as an enhanced version of mx.nd in terms of usability, functionality, and performance. mx.nd will be gradually deprecated in the future releases.

Related

Import "tensorflow.keras" could not be resolved after upgrading to TensorFlow 2.8.0

TensorFlow 2.8 was recently released and I installed it as soon as it was out. I really need it for support of higher NumPy versions and a few new features. However, after installing it in my conda environment with
python3 -m pip install --upgrade tensorflow
neither PyCharm nor VSCode can no longer resolve the import from tensorflow.keras import ....
The imports themselves seem to work at runtime, but because the import cannot be resolved I can't make use of code completion, visualizing signatures of functions and etc. Has anybody encountered a similar issue?
everything was working with TF 2.7 - the version I had before.
Note: I'm using Python 3.8
Vs Code
PyCharm
I tried to check the versions through the PyCharm interpreter tab and this is what I saw. For some reason PyCharm isn't aware that there are versions after 2.0 (I have the latest version of pip installed in that environment). I'm guessing this is related, but not sure what to do with that.
I had the same problem and resolved it by importing it as
from tensorflow.python.keras.layers import Dense
This is a bug in the current version of tensorflow, as discussed in this issue.
You can work around it by either
modifying the file site-packages/tensorflow/__init__.py as described in this answer from the referenced issue or
using import keras.api._v2.keras as keras since this seems to be the exact package tensorflow loads itself. (Though you need to reference the protected member _v2 here, which is against python conventions.)
The reason here is that tensorflow tries to load the keras module in a lazy fashion, which means that it holds only a reference to the module until the module is used. Only then the keras module will be actually loaded. Therefore IDEs only know about the reference tensorflow holds to the keras module and not its content.
I see the problem in Google Colab as well. Although running the code works just fine. It's just an IDE complaint that supposedly it cannot find the imports. Very strange. I hope someone from the TensorFlow team gives feedback soon.
Resolving
import tensorflow
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this will fail
and
from tensorflow.keras import foo
# if foo is an attribute, this is (roughly) equivalent to
import tensorflow.keras
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this is (roughly) equivalent to
import tensorflow.keras.foo as foo
are different.
The first one need tensorflow has keras attribute with correct type statically during type checking.
But the second one need tensorflow.__path__ contains keras module statically during type checking.
BTW, for from tensorflow import keras:
If tensorflow has keras attribute, then it uses the attribute, otherwise it import keras as a submodule.
Theoretically, the second one should only work for 2.2.0 <= TF < 2.6.0, which has tensorflow/keras folder. Because tensorflow/keras should be removed in TF 2.6 according to TF 2.6 Release Log, otherwise from tensorflow import keras(tensorflow.keras backed by the keras PIP package) will be different from import tensorflow.keras as keras(tensorflow/keras backed by tensorflow/python/keras).
In fact, however, the second one works for 2.2.0 <= TF < 2.8.0, since tensorflow/keras is not removed until TF 2.8. Interestingly, tensorflow/python/keras is not removed yet (Release 2.9.1), violating the claim in TF 2.6 Release Log that "... will be removed in future release (2.7)".
The first one is broken for TF >= 2.5.0 because of keras lazy loading introduced in TF 2.5, and haven't been fixed yet (Release 2.9.1) though related commits have been merged into master branch.
See
https://github.com/tensorflow/tensorflow/pull/54104
and
https://github.com/tensorflow/tensorflow/commit/e65b68a0914408118995d2f8b55c4286859362f8
See also https://github.com/tensorflow/tensorflow/pull/54104#issuecomment-1067102133
This is happening to me in tensorflow-macos 2.10.0.
This has been a pattern as this post in GitHub shows. I'm getting the same. Ignoring it since the code still runs, but would rather not have the yellow. I hope someone from tensorflow can chime in. :)
This one worked for me:
from keras.utils.np_utils import to_categorical
You can create a symlink in tensorflow directory pointing to keras sources like below:
cd ./virtualenvs/myenv/lib/python3.x/site-packages/tensorflow
ln -s ../keras/api/_v2/keras/ keras

Alternative to partial dependence plot?

as far as I can see, sklearn has deprecated the partial dependence functionality. I tried to run a simple example:
from sklearn.datasets import make_friedman1
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.inspection import partial_dependence
from sklearn.inspection import plot_partial_dependence
X, y = make_friedman1()
clf = GradientBoostingRegressor(n_estimators=10).fit(X, y)
plot_partial_dependence(clf, X, [0, (0, 1)])
But I am returned the following error message: ImportError: No module named 'sklearn.inspection'
To me, partial dependence (and marginal effects) plots a very important (in combination with relative importances) to better understand machine learning results and predictions.
Is there an alternative available? Respectively, how can I plot the partial dependence?
I think there might be a confusion with versions of sklearn. Just as a suggestion -- I would check yours (e.g., import sklearn; sklearn.__version__). For example, if it is v.0.20.3, by chance -- aren't you looking for partial_dependence and plot_partial_dependence from sklearn.ensemble.partial_dependence instead of sklearn.inspection?
I had the same issue, I resolved it by simply updating sklearn, which now contains sklearn.inspection. I'm using Anaconda, if you're also using Anaconda, simply type this in the Anaconda Propmt:
conda update --all
to update all packages. Restart your jupyter notebook and now it should work.

module 'sklearn.metrics' has no attribute 'davies_bouldin_score'

I am trying to evaluate a clustering kmeans model using sklearn.metrics.davies_bouldin_score. I am using google colab with runtime Python 3 and GPU accelerator.
I got this error:
module 'sklearn.metrics' has no attribute 'davies_bouldin_score'.
I have tried to import metrics package in different ways as it was suggested by some people as from sklearn import metrics and import sklearn.metrics. It made no difference.
I have also updated sklearn package !pip install --upgrade sklearn and it did not solve the problem.
Is it google-colaboratory? How can I solve it?
You need to pip install scikit-learn, not sklearn, though the import sklearn.metrics is correct. It looks like it's also a recently added feature, so it may not be available in earlier versions of scikit-learn.
It is in version 0.20. Make sure you are using the right version of Sklearn.
"conda update sklearn"

Tensorflow causes errors in scikit-learn

When I import scikit-learn before importing tensorflow I don't have any issues. Running this block of code produces an output of 1.7766212763101197e-12.
import numpy as np
np.random.seed(123)
import numpy.random as rand
from sklearn.decomposition import PCA
import tensorflow as tf
X = rand.randn(100,15)
X = X - X.mean(axis=0)
mod = PCA()
w = mod.fit_transform(X)
h = mod.components_
print(np.sum(np.abs(X-np.dot(w,h))))
However, if I import tensorflow before importing scikit-learn my code no longer functions. When I run this code-block
import tensorflow as tf
import numpy as np
np.random.seed(123)
import numpy.random as rand
from sklearn.decomposition import PCA
X = rand.randn(100,15)
X = X - X.mean(axis=0)
mod = PCA()
w = mod.fit_transform(X)
h = mod.components_
print(np.sum(np.abs(X-np.dot(w,h))))
I get an output of 130091393261440.25.
Why is that? My versions for the packages are:
numpy - 1.13.1
sklearn - 0.19.0
tensorflow - 1.3.0
Import order should not affect output, as python modules are self-contained, except in the case of dependencies.
I was unable to reproduce your error, and get an output of 1.7951539777252834e-12 for both code blocks.
This is an interesting problem and I am curious to see if others can provide a better response for why you are seeing this issue.
Note: the present answer is an answer to the title for the ones looking for using TensorFlow within Scikit-Learn, and does not just regards some import errors as you've had.
You can use TensorFlow within Scikit-Learn pipelines using Neuraxle.
Neuraxle is an extension of Scikit-Learn to make it more compatible with all deep learning libraries.
Problem: You can’t Parallelize nor Save Pipelines Using Steps that Can’t be Serialized “as-is” by Joblib (e.g.: a TensorFlow step)
Whereas a step is a transformer or estimator in a scikit-learn Pipeline.
This problem will only surface past some point of using Scikit-Learn. This is the point of no-return: you’ve coded your entire production pipeline, but once you trained it and selected the best model, you realize that what you’ve just coded can’t be serialized.
This means once trained, your pipeline can’t be saved to disks because one of its steps imports things from a weird python library coded in another language and/or uses GPU resources. Your code smells weird and you start panicking over what was a full year of research development.
Solution with Code Examples:
Here is a full project example from A to Z where TensorFlow is used with Neuraxle as if it was used with Scikit-Learn.
Here is another practical example where TensorFlow is used within a scikit-learn-like pipeline
The trick is performed by using Neuraxle-TensorFlow.
This is to make use of Neuraxle's savers.
Read also: https://stackoverflow.com/a/60557192/2476920

Why can I import linspace from scipy but in the doc it is from numpy?

I tried to understand it by myself using the doc , but I don't get why, from the module scipy, I can import linspace which is a function of the module numpy. They both work well.
Also, I can't find it in the scipy doc.
Scipy imports all Numpy functions in its namespace.
Granted, it is not easy to know where to find this in the documentation, but it is in the first page of the tutorial that is also the first section of the documentation.
In addition, many basic array functions from numpy are also available
at the top-level of the scipy package.

Resources