0
$\begingroup$

Not new to Python or programming but I'm fairly new to machine learning so I was hoping I'm just overlooking something simple. At the moment I keep running into an issue where Keras spits out the following Value Error:

ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (483, 1)

So to provide some context and show you all the bit of code I'm working with, please refer to the example below.

# for the sake of argument, let's say we are feeding in an array of floats labeled 'getReturns'
# in this case, lets say 'getReturns' contains a total of 569 values

trainingLen = int(float(len(getReturns)) * 0.85) # reserves 85 percent for training

trainingData = getReturns[:trainingLen]
testingData = getReturns[trainingLen:-1]

model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape = (569, 1)))
model.add(keras.layers.Dense(59, activation = "relu"))
model.add(keras.layers.Dense(1, activation = "softmax"))

model.compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = ["accuracy"])

model.fit(trainingData, testingData, epochs = 5)

Though it may be obvious, the "model.fit" line is the one causing the actual error but I suspect something is wrong with the way I am bringing in data? I'm not sure if I need to reshape my array somehow or if the problem is stemming from something even more fundamental? Any help would be appreciated.

Thanks, -B

$\endgroup$
  • $\begingroup$ if your input shape 'getReturns' contains a total of 569 values, then the input shape parameter in keras should be "input_shape=(1,)", we don't include number of instances/observations when setting up the model (afterall Keras shouldn't know whether we have 1 million or 100 observations) $\endgroup$ – chappers Apr 18 at 22:14
  • $\begingroup$ Thanks, chappers. So after taking your advice I ran into another error and had to split the training and testing samples equally. Luckily that worked and I am now getting no errors! Only issue is when I run the code now, I end up with a '0.00' accuracy after training. Any thoughts? $\endgroup$ – bg07 Apr 18 at 23:42
1
$\begingroup$

In the code of your model, you have split up the dataset into 85% training and 15% testing dataset. However, model.fit() takes as input the features and the target variable. In your case, you have not defined the target variable for either the training or the test case. Also, as pointed out by @chappers in the comment section, you don't have to specify the batch size in the input_size. The correct way to do it is as follows:

# for the sake of argument, let's say we are feeding in an array of floats labeled 'getReturns'
# in this case, lets say 'getReturns' contains a total of 569 values
# in this case, consider 'getReturnsLabels' contains a total of 569 values with each value representing a specific class.

trainingLen = int(float(len(getReturns)) * 0.85) # reserves 85 percent for training

trainingData = getReturns[:trainingLen]
testingData = getReturns[trainingLen:-1]

trainingDataLabels = getReturnsLabels[:trainingLen]
testingDataLabels = getReturnsLabels[trainingLen:-1]

model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape = (1)))
model.add(keras.layers.Dense(59, activation = "relu"))
model.add(keras.layers.Dense(1, activation = "softmax"))

model.compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = ["accuracy"])

model.fit(trainingData, trainingDataLabels, epochs = 5)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks very much for the response, Rajdeep! I can see where I went wrong now. However, over the past couple days I've been trying to learn why we need training / testing labels in the first place. So forgive the awkward question, but to clarify is this an example of Supervised Learning (therefore we require labels)? $\endgroup$ – bg07 Apr 26 at 12:30

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.