Tensorflow input for a series of (1, 512) tensors - python

I have a pandas dataset with a column of tensors of shape TensorShape([1, 512]) which are a result of tf.hub Bert embeddings. I know I can use an embedding layer directly in tensorflow, but is there a way to feed the data as it is in the Input layer?
I've tried with a (1, 512) shape input layer but I have an error: "Failed to convert a Numpy array to a Tensor". I've tried to feed it as a np.array instead of a series, but it's not working...
I would guess that it's a shape problem, but I don't see how to solve it!
Edits : I used USE from tf.hub : https://tfhub.dev/google/universal-sentence-encoder-large/5

Related

LSTM input and output dimensions in Keras

I am confused about LSTM input/output dimensions, specifically in keras library. How do keras return 2D output while its input is 3D? I know it can return 3D output using “return_sequence = Trure,” but if return_sequence = False, how can it deal with 3D and produces 2D output?
For example, if input data of shape (32, 16, 20), 32 batch size, 16 timestep, 20 features, and output of shape (32, 100), 32 batch size, 100 hidden states; how keras processes input of 3d and returns output 2d.
Additionally, how can concatenate input and hidden state if they don’t have the exact dimensions?
I found the answer to my question in the link below:
https://mmuratarat.github.io/2019-01-19/dimensions-of-lstm
it's very helpful!

Using a convolution neural network with a non-image input

I want to use a convolutional neural network, but I have a 2D array for the input, not an image. I am trying to evaluate a board game state where shapes are important.
The board is 5x5 and the values can be between -1 and 1, stored as a list of lists ex:
[[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]]
the first layer of the model is
tf.keras.layers.Conv2D(32, (3,3), input_shape=(5,5,1))
I convert the board to a numpy array
np.array([[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]])
I gather the boards into a list.
Then I convert the list into an array of arrays to fit
model.fit(np.array(x_train_l), y_train, epochs=10)
I get the following error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 5, 5]
Just reshape your numpy array to have shape (5,5,1). Currently it is with shape (5,5).
np.array([[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]]).reshape(5,5,1)

How to restructure/reformat Pandas dataframe containing images to be fed into Tensorflow's model.fit()?

In preprocessing a set of images to be fed into a Tensorflow convolutional neural network, I have created a Pandas dataframe with two columns.
The first contains 13200, 1/255-rescaled images(specifically their file paths to their respective train/test and class directories) filled into the dataframe such that, barring the error I am about to show, should be ready for a traditional sklearn test_train_split. The second column contains the class labels associated with each image. Here is what the dataframe looks like:
(the dataframe)
Now, the error. While in checking the shape of an individual image I get the proper (-,-,-) dimensions, in checking the shape of my X_train dataframe as a whole the output I receive is (10560, ). As is true of the CIFAR-10 dataset, I am expecting a shape of (10560, 150, 150, 3), to fully represent the dimensions of my dataset. No matter what I input as the input_shape for my Conv2D layer, I get the following error:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [32, 1]
So I believe the right question to ask in order to resolve this error is how do I restructure and/or change the image inputs in column 1 so that the correct shape of the dataset is yielded to comply with the model fit?
tf.keras.layers.Conv2D layer expects input of shape 4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format='channels_first' or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format='channels_last'.
Make sure your X_train be shape of 4D.

Tensorflow Keras Conv2D error with 2D numpy array input

I would like to train a CNN using a 2D numpy array as input, but I am receiving this error: ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (21, 21).
My input is indeed a 21x21 numpy array of floats. The first layer of the network is defined as Conv2D(32, (3, 3), input_shape=(21, 21, 1)) to match the shape of the input array.
I have found some similar questions but none pertaining to a 2D input array, they mostly deal with images. According to the documentation, Conv2D is expecting an input of a 4D tensor containing (samples, channels, rows, cols), but I cannot find any documentation explaining the meaning of these values. Similar questions pertaining to image inputs suggest reshaping the input array using np.ndarray.reshape(), but when trying to do that I receive an input error.
How can I train a CNN on such an input array? Should input_shape be a different size tuple?
Your current numpy array has dimensions (21, 21). However, TensorFlow expects input tensors to have dimensions in the format (batch_size, height, width, channels) or BHWC implying that you need to convert your numpy input array to 4 dimensions (from the current 2 dimensions). One way to do so is as follows:
input = np.expand_dims(input, axis=0)
input = np.expand_dims(input, axis=-1)
Now, the numpy input array has dimensions: (1, 21, 21, 1) which can be passed to a TF Conv2D operation.
Hope this helps! :)

3 dimensional array as input for Keras

I got a 3 dimensional array and would like to use it as an input for a sequential model in Keras. The shape of the input array is (32, 32, 4). I want to get an array with the same shape as output. How should i make a feed forward neuronal network with one input, one output and one hidden layer, to make it work with such an array as input?

Categories

Resources