Abraham River
Abraham River

Reputation: 41

Python keras - getting error with model layers

I am new on classification problems using artificial neural networks

I have a classification problem where the input data are 8 columns with decimal values Which are measures and the output data are 8 columns with integer values which are objects

INPUTS

785.39 6.30 782.75 771.82 7.53 -94.86 378.66 771.82
.
.
.

OUTPUTS

  8     9     5      7     3     1      6      2
  .
  .
  .

The records for the training data are 800 and for the test data are 200

This is the code

import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils

seed = 7
numpy.random.seed(seed)
datasetTrain = numpy.loadtxt("train.csv", delimiter=",")
datasetTest = numpy.loadtxt("test.csv", delimiter=",")

X_train = datasetTrain[:,0:7]
y_train = datasetTrain[:,8:15]

X_test = datasetTest[:,0:7]
y_test = datasetTest[:,8:15]

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(7, input_dim=7, kernel_initializer='normal', activation='relu'))
    model.add(Dense((5593, 785), kernel_initializer='normal', activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    return model

model = baseline_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=400, 
batch_size=200, verbose=25)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))

I get this error

Traceback (most recent call last):
  File "proyecto.py", line 29, in <module>
    model = baseline_model()
  File "proyecto.py", line 24, in baseline_model
    model.add(Dense((5593, 785), kernel_initializer='normal', activation='softmax'))
ValueError: setting an array element with a sequence.

Which is the best model for this data?

Upvotes: 1

Views: 281

Answers (3)

thefifthjack005
thefifthjack005

Reputation: 638

model = Sequential()

num_pixels=img_height*img_width

model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', 
activation='relu'))
model.add(Dense(num_classes, kernel_initializer='normal', 
activation='softmax'))

Upvotes: 0

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

This part:

model.add(Dense((5593, 785), kernel_initializer='normal', activation='softmax'))

Is wrong, the first parameter to Dense is the number of output neurons, which should be a scalar, not a tuple or vector. If you want a 2D-shaped output, then you can use a Reshape layer to reshape the output and do the following:

model.add(Dense(5593 * 785, kernel_initializer='normal', activation='softmax'))
model.add(Reshape((5593, 785)))

Upvotes: 1

Max Power
Max Power

Reputation: 8954

After your first layer model.add(Dense(7..., the output of that layer has dimension 7 (7 output neurons). The next layer can handle that 7-neuron layer as input automatically. But you are then telling Keras that the next layer should have output neurons of (5593, 785) when it is looking for another single number.

Do you get what you want by changing model.add(Dense((5593, 785)...

to either

model.add(Dense(1...

or

model.add(Dense(n...

(where n is the number of possible levels of the categorical variable you're trying to classify?

Upvotes: 0

Related Questions