I have a [128x128x128] array. From this I need to plot 1 single plane, i.e., the central plane along the z-axis, so I will have to use the array in the form A[:,:,64].
Do you know which commands should I type in order to get this contour plot?
Let's say you have an array like this one:
import numpy as np
import matplotlib.pyplot as plt
A = np.random.rand(128, 128, 128)
A.shape
#Output:
(128, 128, 128)
Then you take out one plane by index:
A[:, :, 64].shape
#Output:
(128, 128)
When plotting with plt.contourf(), [X, Y] arguments are optional, so if you want to plot just one array along Z-axis, just pass the array you selected as an argument:
plt.contourf(ndarray[:, :, 64])
plt.show()
And you get this as an output:
Related
So I was trying to multiply two tensors (deltasTimesWeights, self.weightedInputs) both of shapes (24,24,3,2), but for some reason numpy thinks that the latter of them is of shape (24,24,3,10). Specifically it gives the error ValueError: could not broadcast input array from shape (24,24,2) into shape (24,24,10)
print(self.weightedInputs.shape , deltasTimesWeights.shape)
for index in range(c[2]):
self.errors[:, :, index, :] = self.activationFuncs[index].prime(
self.weightedInputs[:, :, index, :] ) * deltasTimesWeights[:, :, index, :]
Note that self.activationFuncs[index].prime just returns the same tensor shape back. This code is part of a bigger loop in which previous iterations, both tensors were in fact both tensors were (24,24,3,10).
Are there any known reasons why this happens ? Does numpy keep like cache about the shape previously ?
I am currently trying to work with CIFAR10 images. I have the following snippet
import tensorflow as tf
from tensorflow.keras import datasets,layers,models
import matplotlib.pyplot as plt
(train_images,train_labels),(test_images,test_labels)=datasets.cifar10.load_data()
#train_images,test_images=train_images/,test_images
when I print print(train_images[0]) I get 32*32*3 matrix, when I print print(train_images[0][0) I get 32*3 matrix, however I thought it should be 32*32 matrix. How does slicing work with this image, which dimension come first. Any insight and recommendation on reading material will be highly appreciated
train_images variable have batch of images and images are numpy metrics and slicing works same for all metrics in numpy.
Dimensions comes as [batch, rows, columns, channels].
To get first image you will print: print(train_images[0].shape) and it will output (32, 32, 3).
To get first channel of image you will print: print(train_images[0, :, :, 0]) and it will output (32, 32) first channel and so on print(train_images[0, :, :, 1]) for second channel, print(train_images[0, :, :, 2]) for third channel.
Where ':' implies all values.
train_images[0, 0] will output values from first row of first image from batch (32, 3)
More on: basics indexing,arrays indexing
I have a tensor T of size [None, 4], and I want to slice it along the second dimension to give me a size [None] tensor. In numpy this would be T[:, DIMENSION], is there a fast way to do it with tensorflow commands?
You can use the same operation as numpy.
a = tf.constant([[1,2,3,4],[5,6,7,8],[7,8,9,0]])
a.shape
the shape of a is
Out[]:TensorShape([Dimension(3), Dimension(4)])
using the slicing operation, get values in the second dimension:
a[:, 2].eval()
and the output is
array([3, 7, 9])
does anyone know how to use map_fn or any other tensorflow-func to do a computation on every combination of two input-tensors?
So what i want is something like this:
Having two arrays ([1,2] and [4,5]) i want as a result a matrix with the output of the computation (e.g. add) on every possible combination of the two arrays. So the result would be:
[[5,6],
[6,7]]
I used map_fn but this only takes the elements index-wise:
[[5]
[7]]
Has anyone an idea how implement this?
Thanks
You can add new unit dimensions to each Tensor, then rely on broadcasting addition:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
first = tf.constant([1, 2])
second = tf.constant([4, 5])
print(first[None, :] + second[:, None])
Prints:
tf.Tensor(
[[5 6]
[6 7]], shape=(2, 2), dtype=int32)
I have an incredible simple algorithm that is erroring with, "ValueError: Error when checking input: expected dense_4_input to have shape (None, 5) but got array with shape (5, 1)"....
Here is the code I am running.
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
x = np.array([[1],[2],[3],[4],[5]])
y = np.array([[1],[2],[3],[4],[5]])
x_val = np.array([[6],[7]])
x_val = np.array([[6],[7]])
model = Sequential()
model.add(Dense(1, input_dim=5))
model.compile(optimizer='rmsprop', loss='mse')
model.fit(x, y, epochs=2, validation_data=(x_val, y_val))
There are two problems:
First: As the output already says: "ValueError: Error when checking input: expected dense_4_input to have shape (None, 5) but got array with shape (5, 1)" This means, that the Neural Network expects an array of shape (*, 5). With the asterisk I want to indicate that the dimensions is free to choose by the user. Say if you have tons of data and every example is a vector of shape (1, 5) you can stack them all underneath and pass one big chunk of data to the neural net, it will know how to handle it. Therefore you have to make x a row vector as follows:
x = np.array([[1,2,3,4,5]])
See also in the Keras docs- Specifying the input shape.
Second: You specify the output of the first Layer to be one. This means, the 5 dimensional input will be connected to only one neuron. Your output vector y however has 5 values. So your output vector dimension and your neural net output don't fit together.
So you have to go with a scalar y:
y = np.array([1])
Furthermore, your validation data and training data should have the same dimensions. Additionaly there is a typo in your code: y_val is never defined.