How to convert a sequential object into numpy array in TensorFlow? - tensorflow

Here I have to convert the generated object in to numpy array but it is in sequential format. How can I do that in Tensorflow 2?enter image description here

Related

How to convert KerasTensor to numpy array

In Tensorflow 2 the output of a layer in functional API is a KerasTensor. How can I convert this KerasTensor to a numpy array? Methods such as tf.make_ndarray() or x.numpy() (x is a tensor) does not work. Both return the error that the KerasTensor does not have a numpy method.

Pytorch: Numpy Arrays

Can I use numpy arrays when using pytorch?
I am converting a code from tensorflow to pytorch and the code uses numpy arrays during the computation. Can I keep my inputs as numpy arrays during the computation or do I have to convert them to torch tensors?
If that array is being passed to a Pytorch model with pytorch nn layers, then it MUST be a <torch.tensor> and NOT a numpy array.
Depending on the Pytorch layer, the tensor has to be in a specific shape like for nn.Conv2d layers you must have a 4d torch tensor and for nn.Linear you must have a 2d torch tensor.
This is among many reasons, it cannot be a numpy array.
Sarthak

What does the .numpy() function do?

I tried searching for the documentation online but I can't find anything that gives me an answer. What does .numpy() function do? The example code given is:
y_true = []
for X_batch, y_batch in mnist_test:
y_true.append(y_batch.numpy()[0].tolist())
Both in Pytorch and Tensorflow, the .numpy() method is pretty much straightforward. It converts a tensor object into an numpy.ndarray object. This implicitly means that the converted tensor will be now processed on the CPU.
Ever getting a problem understanding some PyTorch function you may ask help().
import torch
t = torch.tensor([1,2,3])
help(t.numpy)
Out:
Help on built-in function numpy:
numpy(...) method of torch.Tensor instance
numpy() -> numpy.ndarray
Returns :attr:`self` tensor as a NumPy :class:`ndarray`. This tensor and the
returned :class:`ndarray` share the same underlying storage. Changes to
:attr:`self` tensor will be reflected in the :class:`ndarray` and vice versa.
This numpy() function is the converter form torch.Tensor to numpy array.
If we look at this code below, we see a simple example where the .numpy() convert Tensors to numpy arrays automatically.
import numpy as np
ndarray = np.ones([3, 3])
print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.multiply(ndarray, 42)
print(tensor)
print("And NumPy operations convert Tensors to numpy arrays automatically")
print(np.add(tensor, 1))
print("The .numpy() method explicitly converts a Tensor to a numpy array")
print(tensor.numpy())
In the 2nd last line of code, we see that the tensorflow officials declared it as the converter of Tensor to a numpy array.
You may check it out here

Tensorflow - Tensorboard Event Accumulator get Tensor from TensorEvent

I am working with Tensorflow and Tensorboard version 1.14.
I would like to perform some off-line analysis starting from Data I have saved during training using the tf.summary.tensor_summary()
I am not able to recover the data saved with the method described here, using the tf.train.summary_iterator which does recover scalar data but not the data I saved with the tensor_summary method.
Though with the EventAccumulator object I am able to recover the data I have saved, that it is returned as a TensorEvent Object which has the following attributes:
step
wall_time
tensor_proto
tensor_content
Thing is that I would like to convert this data into numpy array, the TensorEvent object sure has all the information needed (tensor_proto for type and shape, tensor_content for values), but not being a Tensor does not have a .value or a .numpy() method. So I do I trasform a TensorEvent Object into a numpy array? or equivalently into a Tensor object then into a numpy array?
You can use tf.make_ndarray to convert a TensorProto into a NumPy array:
tensor_np = tf.make_ndarray(tensor_event.tensor_proto)

Loading a NumPy array into a Tensor

According to the TensorFlow webpage at (https://www.tensorflow.org/versions/r1.3/programmers_guide/datasets), the tf.read_file can be used to load an image file from a given filename, and convert it to a Tensor:
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_image(image_string)
In my case however, I want to load a NumPy array rather than an image. So the filename above points to a NumPy array on my machine.
If I were to use tf.read_file(filename) on this filename, then according to the documentation, this function returns a string Tensor (a byte array). How can I convert this into a Tensor representing the data in the NumPy array? Is there an equivalent function to tf.image.decode_image() for decoding a NumPy array?