Reputation: 1
I'm building my first model and I'm following the step by step from the professor, but I seem to have found an error in a layer in the model. I am a begginner and would appreciate any input. Full code below. I'm trying to build a neural network machine learning model that is feedforward. I'm getting the following error:
Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 784), found shape=(None, 28, 28)
I think something is missing. I checked the professor's code and everything seems to be in check.
I'm learning to create the architecture of the neural network. The goal was to create the following format: an entry layer with 784 knots, one for each pixel of the image. This layer will connect to the second layer, which is occult and dense, with 256 knots. After that, the second layer will connect to the third layer, also occult and dense, with 128 knots. Both with a function of activation sigmoid. The last occult layer will connect to the last layer, with 10 knots and softmax activation.
To train the model I'm using the gradient optmizer SGD, with 0.01. We will use the accuracy metric to track the model, and to calculate the loss, cost function, we will use the categorical cross entropy (categorical_crossentropy), which is the most widely employed in classification problems. For the epochs in our network, we will choose 100 epochs, meaning that the network has 100 iterations to converge and grasp, and we will present batches of 128 images each per iteration.
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
from tensorflow.keras import backend as K
import matplotlib.pyplot as plt
import numpy as np
((trainX, trainY), (testX, testY)) = mnist.load_data()
trainX = trainX.reshape((trainX.shape[0], 28 * 28 * 1))
testX = testX.reshape((testX.shape[0], 28 * 28 * 1))
trainX = trainX.astype('float32') / 255.0
testX = testX.astype('float32') / 255.0
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
model = Sequential()
model.add(Dense(256, input_shape=(784,), activation='sigmoid'))
model.add(Dense(128, activation='sigmoid'))
model.add(Dense(10, activation='softmax'))
#so far all the cells worked fine, but the next part of the code got me the error
sgd = SGD(0.01)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
H = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=100, batch_size=128)
I'm not sure what this code was supposed to generate yet, maybe an image. I hope someone can help understand the problem.
Upvotes: 0
Views: 114
Reputation: 1461
This code has no problem, I ran it in my computer with no erros. Nothing is missing.
Upvotes: 0