How to train 3D array in TensorFlow

Question:

Code:

X_train_=np.random.rand(100,200,16)
y_train_=np.random.rand(100,1)

batch=32

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(batch,200,16)),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
    
num_epochs = 10
model.fit(X_train_, y_train_, epochs=num_epochs,batch_size=batch)

Error:

ValueError: Input 0 of layer dense_4 is incompatible with the layer: expected axis -1 of input shape to have value 102400 but received input with shape (None, 3200)
Asked By: Majd Alhafi

||

Answers:

There are 2 ways you can use your 3D array with neural Network, First by flattening your 3d array in a 1D array. Second use Convoltional Layer.

In your case you just don’t have to include input_shape()

import numpy as np
import tensorflow as tf
X_train_=np.random.rand(100,200,16)
y_train_=np.random.rand(100,1)

batch=32

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
    
num_epochs = 10
model.fit(X_train_, y_train_, epochs=num_epochs,batch_size=batch)

Output:

Epoch 1/10
4/4 [==============================] - 1s 3ms/step - loss: 0.7472 - accuracy: 0.0000e+00
Epoch 2/10
4/4 [==============================] - 0s 6ms/step - loss: 0.6948 - accuracy: 0.0000e+00
Epoch 3/10
4/4 [==============================] - 0s 3ms/step - loss: 0.6730 - accuracy: 0.0000e+00
Epoch 4/10
4/4 [==============================] - 0s 4ms/step - loss: 0.6502 - accuracy: 0.0000e+00
Epoch 5/10
4/4 [==============================] - 0s 3ms/step - loss: 0.6521 - accuracy: 0.0000e+00
Epoch 6/10
4/4 [==============================] - 0s 4ms/step - loss: 0.6450 - accuracy: 0.0000e+00
Epoch 7/10
4/4 [==============================] - 0s 4ms/step - loss: 0.6276 - accuracy: 0.0000e+00
Epoch 8/10
4/4 [==============================] - 0s 4ms/step - loss: 0.6343 - accuracy: 0.0000e+00
Epoch 9/10
4/4 [==============================] - 0s 4ms/step - loss: 0.6933 - accuracy: 0.0000e+00
Epoch 10/10
4/4 [==============================] - 0s 6ms/step - loss: 0.6119 - accuracy: 0.0000e+00
<tensorflow.python.keras.callbacks.History at 0x7fc63f589090>
Answered By: ASLAN

Here is an example of how to transform 2D .csv data to 3D (gives them 3 dimensions , add TIME ).
And with that 3D .csv, It use 3D array to make models and predictions in TensorFlow.
https://github.com/Leci37/stocks-prediction-Machine-learning-RealTime-telegram/blob/develop/Data_multidimension.py
in these code steps is your answer


STEPS OF THE EXAMPLE

1.0 ADD MULTIDIMENSION  Get 2D array , with BACHT_SIZE_LOOKBACK from "backward glances".
    1.1 validate the structure of the data, this can be improved by
2.0 SCALER  scaling the data before, save a .scal file (it will be used to know how to scale the model for future predictions )
    2.1 Let's put real groound True Y_TARGET  in a copy of scaled dataset
3.0 SPLIT Ok we should split in 3 train val and test
    3.1 Create a array 2d form dfs . Remove Y_target from train_df, because that's we want to predict and that would be    cheating
4.0 SMOTE train_df to balance the data since there are few positive inputs, you have to generate "neighbors" of positive inputs.    only in the df_train. according to the documentation of the imblearn    pipeline:
    4.1 Let's put real groound True Y_TARGET  in a copy of scaled dataset
5 PREPARE the data to be entered in TF with the correct dimensions
    5.1 pass Y_TARGET labels to 2D array required for TF
    5.2 all array windows that were in 2D format (to overcome the SCALER and SMOTE methods),
6 DISPLAY show the df format before accessing TF
Answered By: Luis l
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.