TransWikia.com

input shape of keras Sequential model

Data Science Asked by ammar on February 20, 2021

i am new to neural networks using keras,
i have the following train samples input shape (150528, 1235) and output shape is (154457, 1235) where 1235 is the training examples,
how to put the input shape, i tried below but gave me a

ValueError: Data cardinality is ambiguous:
  x sizes: 150528
  y sizes: 154457
Please provide data which shares the same first dimension.

code:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy

physical_devices = tf.config.experimental.list_physical_devices('GPU')
print("Num GPUs Available: ", len(physical_devices))
tf.config.experimental.set_memory_growth(physical_devices[0], True)

model = Sequential([
    Dense(16, input_shape=(150528,), activation='relu'),
    Dense(32, activation='relu'),
    Dense(154457)
])

model.compile(optimizer='sgd', loss='mse', metrics=[tf.keras.metrics.MeanSquaredError()])
model.fit(im_features, amp_vo_features, batch_size=10, epochs=30, shuffle=True, verbose=2)

One Answer

You just need to make sure that your x and y values have the same row dimension. This can be done using numpy.transpose or numpy.swapaxes.

import numpy as np

t = np.random.rand(150528, 1235)
np.transpose(t).shape # (1235, 150528)
np.swapaxes(t, 0, 1).shape # (1235, 150528)

This should give the shapes of (1235, 150528) for x and (1235, 154457) for y.

Correct answer by Oxbowerce on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP