Error in building multi classification

Hi,



I made a 3 output classification model,but I can not run it and it give an error as below, please help me to pass it: I attached the files. thanks.

  raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (None, 1) and (None, 3) are incompatible

You need to import to_categorical

 

from tensorflow.keras.utils import to_categorical


Then you need to add the following:

 

y_train = to_categorical(y_train, 3)
y_test = to_categorical(y_test, 3)

Thank you,

I will try.

It works for me , now I have to analyze and improve the accuracy. Thanks.

 

 

Hi Amir,



Please send us your code file in .ipynb format. Currently, it's in .py format. You can get file in this format by following these steps:

  1. Open the notebook you want to download
  2. Click File
  3. Click Download As
  4. Choose Notebook (.ipynb) format.

Hi ,

Thanks for the reply. this is ipynb format.



https://drive.google.com/file/d/1w7K37PuDKcc3H0NR95VszA2I2que4NLD/view?usp=sharing

Hi Amir,



Your y_train variable is 1d but you units of your last dense layer are 3. Try setting units to 1 and that will solve your problem. You can add one more dense layer with unit=1 at the end.

Hi again , Thanks for early reply.

I check it , the problem is that in this case y_pred=[1,1,1,1,…,1,1,1] . means all the prediction is 1.now code is like this :



model = Sequential()

model.add(Dense(n_features ,activation='relu'))

model.add(Dense(n_features, activation='relu'))

model.add(Dropout(0.2))

model.add(Dense(50, activation='relu'))

model.add(Dense(1, activation='softmax'))



model.fit(X_train,y_train,epochs=500,validation_data=(X_test, y_test))

y_pred = model.predict_classes(X_test)



for this reason I change the last layer to model.add(Dense(1, activation='softmax')).

but I have the error that I sent above.


Hi Amir,

I looked at your notebook. There are a few reasons you are getting inaccurate predicted values for example underfitting, exploding gradients etc. There are a few ways you can try to improve your model.
  1. Looking at your notebook, I can say that the data you are using is quite less. You can try using more data.
  2. Your ml model is likely underfitting if all the values your model is predicting are the same. You can make your model more complex to avoid this problem by adding more layers, trying out different parameters. Also, you can check the correlation between your features and target values and add uncorrelated features. You can check this article to learn more about underfitting and overfitting.
  3. When you are using "train_test_split()" function to split data set "shuffle=False". By doing this you won't lose the time-series nature of the data. 
  4. Exploding and vanishing gradients is likely another problem in your model. You can read about exploding and vanishing gradients and how to avoid them from here.
  5. You can also try other models from our Neural Networks in Trading course and try them out on different currencies to find the best results.

hi.Thanks.I will try.