Input array shape (not same sample)

I’m trying to input 13 integer numbers to a neural network and output 1 number depending on the 13 input numbers so I made a Flatten of input shape 13 and a Dense layer of 64 and output Dense layer with 1 neuron without any activation on any layer and I have 7 examples with 13 number for every example and 7 numbers for the corresponding output how to make the array of inputs and outputs

here is my code

`import numpy as np

from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout, GlobalMaxPooling2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

xs = np.array([[2,9,5,0,5,0,6,2,4,0,3,0,8],
[2,9,6,0,9,0,7,2,4,0,3,6,9],
[2,9,7,0,5,0,2,1,3,0,0,4,2],
[2,7,0,1,0,3,1,2,7,0,0,7,0],
[2,7,0,0,9,2,7,3,1,0,0,0,6],
[2,6,9,0,6,1,7,2,7,0,1,5,5],
[2,4,6,0,4,2,3,2,4,0,0,4,8]], dtype=float).T
ys = np.array([[3,3,2,8,6,5,3]], dtype=float)

print('xs shape = ', xs.shape)
print('ys shape = ', ys.shape)

model = Sequential([
Flatten(input_shape=[xs.shape[0]]),
Dense(64),
Dense(64),
Dense(1)
])

model.compile (optimizer=‘sgd’, loss=‘mean_squared_error’)

early_stopping = EarlyStopping(monitor=‘val_loss’, patience=50)

model.summary()

model.fit(xs, ys, epochs = 1000, validation_split=0.2, callbacks=[early_stopping])

model.save(‘keras_model.h5’)`

and I got this error

First, both your input and target shapes are incorrect. From what you’re describing you have 7 examples with 13 input features, so your input shape should be (7, 13). There’s no need to transpose xs:

xs = np.array([[2,9,5,0,5,0,6,2,4,0,3,0,8],
[2,9,6,0,9,0,7,2,4,0,3,6,9],
[2,9,7,0,5,0,2,1,3,0,0,4,2],
[2,7,0,1,0,3,1,2,7,0,0,7,0].
[2,7,0,0,9,2,7,3,1,0,0,0,6],
[2,6,9,0,6,1,7,2,7,0,1,5,5],
[2,4,6,0,4,2,3,2,4,0,0,4,8]], dtype=float)

Your target shape should then be (7, 1). So ys should be transposed:

ys = np.array([[3,3,2,8,6,5,3]], dtype=float).T

Your neural network input shape is also incorrect, as you don’t need to specify batch size but instead specify input features:

Flatten(input_shape=[xs.shape[1]]),

Finally, you should add some non-linear activation functions, otherwise your neural network just reduces to a basic linear model:

model = Sequential([
  Flatten(input_shape=[xs.shape[1]]),
  Dense(64, activation='relu'),
  Dense(64, activation='relu'),
  Dense(1)
])

As a final warning, your model will severely overfit to your input data. You should try to collect more training data if possible, or use something different altogether.

3 Likes