I am really new to numpy. I have a numpy vector that when I run y.shape returns (4000,). Is there a way, I can have it return (4000, 1)?
np.reshape(y,(4000,1))
Reshape function can be used to do this
Related
How one can get the smallest positive float value in TensorFLow?
In numpy one can use np.nextafter and do something like:
>>> import numpy as np
>>> np.nextafter(0, 1)
4.9406564584124654e-324
For this, Tensorflow has tf.math.nexafter. See here: TensorFlow doc
(It's actually unsurprising that TensoFlow is very similar to Numpy here, since the mathematics of Tensorflow are based on those of Numpy, and you can easily use np arrays as data input in tf.)
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
if I have a numpy array of shape
(16, 224, 224, 6)
how can I reshape it to
(224, 224, 6*16)
so that all elements are still in the new shape?
P.S.:I need a numpy answer and not an answere derived from pytorch please :)
I believe you can use transpose and reshape:
a.transpose([1,2,3,0]).reshape(244,244,-1)
Probably the most elegant and easy to understand way is to use einops:
from einops import rearrange
a = rearrange(a, 'b w h c -> w h (b c)')
Plus this code works on Numpy, Pytorch, Jax and Tensorflow without changes.
I have a numpy array list something like the follows:
a=np.array([tf.convert_to_tensor(1),tf.convert_to_tensor(2)])
I want to convert this list into a tensor.
My real list is not like the constant example but some complex tensor, so does anyone know how to do this?
I assume all of the tensors have the same shape. Then you can just call tf.stack:
>>> print(tf.stack([tf.convert_to_tensor(1), tf.convert_to_tensor(2)]))
Tensor("stack:0", shape=(2,), dtype=int32)
Note that it accepts the list, not numpy array.
It's possible to read dense data by this way:
# tf - tensorflow, np - numpy, sess - session
m = np.ones((2, 3))
placeholder = tf.placeholder(tf.int32, shape=m.shape)
sess.run(placeholder, feed_dict={placeholder: m})
How to read scipy sparse matrix (for example scipy.sparse.csr_matrix) into tf.placeholder or maybe tf.sparse_placeholder ?
I think that currently TF does not have a good way to read from sparse data. If you do not want to convert a your sparse matrix into a dense one, you can try to construct a sparse tensor..
Here is what official tutorial tells you:
SparseTensors don't play well with queues. If you use SparseTensors
you have to decode the string records using tf.parse_example after
batching (instead of using tf.parse_single_example before batching).
To feed SciPy sparse matrix to TF placeholder
Option 1: you need to use tf.sparse_placeholder. In Use coo_matrix in TensorFlow shows the way to feed data to a sparse_placeholder
Option 2: you need to convert sparse matrix to NumPy dense matrix and feed to tf.place_holder (of course, this way is impossible when the converted dense matrix is out of memory)