# Tensorflow operations to perform the following numpy equivalent

I am modifying `identity_3d` which is initialized as an `n` by `n` by `n` numpy array per the following operations:

``````     identity_3d = np.zeros((n, n, n))
``````
``````        idx = np.arange(n)
identity_3d[:, idx, idx] = 1
I,J = np.nonzero(wR==0)
identity_3d[I,:,J]=0
identity_3d[I,J,:]=0
``````

If `identity_3d` was an Tensor instead, is there a way to perform the equivalent operation?

Do you have a complete numpy running example?

1 Like

import numpy as np

n = 5
wR = np.random.choice(a=[0, 1, 2], size=(n, n), p=[0.5, 0.25,0.25])
identity_3d = np.zeros((n, n, n))

idx = np.arange(n)
identity_3d[:, idx, idx] = 1
I,J = np.nonzero(wR==0)
identity_3d[I,:,J]=0
identity_3d[I,J,:]=0
identity_3d

There is not a direct slice assigment for Tensor that maps the numpy syntax.
As you can see is currently not available also in the TF experimental numpy API:

https://www.tensorflow.org/api_docs/python/tf/experimental/numpy#mutation_and_variables

But It Is a very frequent topic, take a look at:

1 Like