Home > Mobile >  Right way of inputting data on Tensorflow Neural Network
Right way of inputting data on Tensorflow Neural Network

Time:08-25

I am working on a model for time series forecasting.

I have two arrays xtrain and ytrain with shapes:

  • xtrain = (961, 4, 1865)
  • yrain = (961, 1, 1865)

The model is:

model = Sequential(
[
  tf.keras.layers.Conv1D(filters=64,
                         kernel_size=3,
                         strides=1,
                         activation="relu",
                         padding='causal'),
  tf.keras.layers.LSTM(64)
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(1)
])
  • 961 is the value given some dates
  • 4 and 1 are the dependent and indpendet variables
  • 1865 are the different companies that registered the data

How should I input the data and make the model so it treats xtrain as one traditional CNN will behave with a dataset with shape (64, 64, 3)?

CodePudding user response:

You can do this 2 ways.

  1. By defining a model that outputs a (1,1865) tensor. You can define the model like this
x = np.random.randn(961,4,1865)
y = np.random.randn(961,1,1865)

model = tf.keras.models.Sequential(
[
  tf.keras.layers.Conv1D(filters=64,
                         kernel_size=3,
                         strides=1,
                         activation="relu",
                         padding='causal',
                         input_shape=(4,1865)),
  tf.keras.layers.LSTM(64),
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(1865),
  tf.keras.layers.Lambda(lambda x: tf.expand_dims(x,1))
])

model.compile(loss='mse',optimizer='adam')

model.fit(x,y,batch_size=16,epochs=10,validation_split=0.2)

>>>
Epoch 1/10
48/48 [==============================] - 3s 22ms/step - loss: 0.9862 - val_loss: 1.0033
Epoch 2/10
48/48 [==============================] - 1s 12ms/step - loss: 0.9856 - val_loss: 1.0036
  1. The other way to remove the additional dimension in y. Since it's 1, you don't actually need it. That way you can skip the lambda function
x = np.random.randn(961,4,1865)
y = np.random.randn(961,1,1865)
y = np.squeeze(y,1)

model = tf.keras.models.Sequential(
[
  tf.keras.layers.Conv1D(filters=64,
                         kernel_size=3,
                         strides=1,
                         activation="relu",
                         padding='causal',
                         input_shape=(4,1865)),
  tf.keras.layers.LSTM(64),
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(1865)
  
])

model.compile(loss='mse',optimizer='adam')

model.fit(x,y,batch_size=16,epochs=10,validation_split=0.2)
  • Related