I made a simple model to train my data set which consists of (210 samples and each sample consists of a numpy array of 22 values) and x_trian and y_trian look like:

enter image description here

and this is my simple 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

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffler

from google.colab import files
uploaded = files.upload()

import io
dset = pd.read_csv(io.BytesIO(uploaded['1-210.csv']))

y= dset.Readernumber

#the split ratio of 80:20. The 20% testing data set is represented by the 0.2 at the end.

x_train= np.asarray(x_train).astype('float32')
y_train = np.asarray(y_train).astype('float32')
y_train, x_train = shuffle(y_train, x_train)

#create the model #input_shape=(23,)
model = Sequential([
    Dense(units=4,input_shape=(22,), activation='relu'),
    Dense(units=16, activation='relu'),
    Dense(units=10, activation='softmax')

#get the model ready for training is call the compile() function on it.
model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy']) 

#train it using the fit() function. 
model.fit(x_train, y_train, epochs=5)

And this is what I'm getting for all the epochs :

enter image description here

I will be grateful to anyone who can help me!

  • $\begingroup$ The reason must be there: y_train, x_train = shuffle(y_train, x_train)when doing this you are simply breaking the relationship between your features set and your target. Actually I do not see where you define x_train nor y_train $\endgroup$
    – Multivac
    Aug 25, 2021 at 22:26
  • $\begingroup$ i forgot this line sorry and i undated it above x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2) $\endgroup$
    – Beba.S
    Aug 25, 2021 at 22:39
  • $\begingroup$ great! so any reason for this line y_train, x_train = shuffle(y_train, x_train? from my understanding this is causing your model not to be able to learn anything since it adds literally pure randomness to your X, y mapping $\endgroup$
    – Multivac
    Aug 25, 2021 at 22:40
  • $\begingroup$ thank you julio, unfortunately the code give me another error and i don't know why,how ever it was working will, i gives me "could not convert string to float: '9 0.559884" for this line "x_train= np.asarray(x_train).astype('float32')" $\endgroup$
    – Beba.S
    Aug 25, 2021 at 22:47
  • $\begingroup$ It seems like you have mixed data types in your x matrix, all the inputs should be numerical and in here you have a value that could not be converted into float '9 0.559884, it might be because it seem there are a space between the first 9 and the consecutive 0, try to clean this. Interestings this error was not shown before $\endgroup$
    – Multivac
    Aug 25, 2021 at 22:53

2 Answers 2


I can't comment -- where this would be more applicable -- but your y_train is class encoded (e.g., this sample's label is class 1), which is a single output. When your data are fed into the model w/ 10 output nodes, the model doesn't know what to do considering your y_train has 1 output for each sample.

A solution would be to one-hot encode your outputs (e.g., if your sample's label is class 1, it would be represented as [0,1,0,0,0,0,0,0,0,0]). Sklearn has a convinient OneHotEncoder to make the preprocessing simple. Now you have 10 outputs for each sample and the model can understand what's going on. Hopefully this helps

  • $\begingroup$ did you mean that i should encode the y_trian before i feed it to the fit function ? using this way : enc = preprocessing.OneHotEncoder() enc.fit(y_train) onehotlabels = enc.transform(y_train).toarray() , but it always gives me this error Expected 2D array, got 1D array instead: Regards! Robert $\endgroup$
    – Beba.S
    Aug 26, 2021 at 11:41
  • $\begingroup$ Yes. You need to reshape your arrays. For each sample, it should initially be something like (,10), but it needs to either be reshaped to (1,10) or (10,1), I can't remember off the top of my head right now. Either way, it can be accomplished using the array's reshape command $\endgroup$ Aug 27, 2021 at 2:07
  • $\begingroup$ i tried reshape(10,1) and reshape(1,10) it always gives me " ValueError: cannot reshape array of size 189 into shape (1,10)" then i tried " reshape(189,10) still gives me " ValueError: cannot reshape array of size 189 into shape (189,10) " $\endgroup$
    – Beba.S
    Aug 27, 2021 at 3:33
  • $\begingroup$ That's for each sample. Reshape(189,1,10) or reshape(189,10,1) for all your data $\endgroup$ Aug 27, 2021 at 3:49
  • $\begingroup$ I'm sorry for bothering you Robert, but it still gives me "cannot reshape array of size 189 into shape (189,10,1)" or "cannot reshape array of size 189 into shape (189,1,10)" $\endgroup$
    – Beba.S
    Aug 27, 2021 at 3:54

you should one_hot target(y) before model.fit & give it in training in such a form

y= tf.one_hot(y,10,)

you've got ok, just display results of each batch training:

history= model.fit(x_train, y_train, epochs=100, batch_size=32)

and can evaluate your model

test_loss, test_acc = model.evaluate(x_test, y_test,  verbose=2)
print('\nTest accuracy:', test_acc)

and at the end when predicting - choose from probabilities the best

predicted = model.predict(x)
Y2 = predicted.argmax( axis = 1)

p.s. your 1st Dense better use Dense(units=64,..)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.