Keras input_shape error - python

I keep receiving this error:
Error when checking target: expected dense_256 to have shape (1,) but got array with shape (10,)
I have check my X_train variable and I get a shape of (576,10). So, I have 576 samples, each with 10 features (all of which have been scaled already).
I now try this:
classifier = Sequential()
classifier.add(Dense(units = 5, kernel_initializer='uniform', activation = 'relu', input_shape=(10,)))
classifier.add(Dense(units = 5, kernel_initializer='uniform', activation = 'relu'))
classifier.add(Dense(units = 5, kernel_initializer='uniform', activation = 'relu'))
classifier.add(Dense(units = 5, kernel_initializer='uniform', activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer='uniform', activation = 'relu'))
classifier.compile(optimizer = 'adam', loss='mean_squared_error', metrics=['mse', 'mae', 'mape'])
classifier.fit(X_train, y_train, batch_size = 10, epochs=100)
Which is when I get the input_shape error referenced above.
So, my question is, when defining input_shape, how do I set this correctly?

Related

Tensorflow Neural Network like MLPClassifier (sklearn)

So, I am creating an AI which predicts how long a user will take to finish exercises. I previously created a NN with Sklearn, but I want to integrate Tensorflow.
I have 6 features as input and 1 output, which is a number.
I tried this but it does not seem to be willing to work:
# Train data
X_train = X[:1500]
y_train = y[:1500]
# Test data
X_test = X[1500:]
y_test = y[1500:]
# Create the TF model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(6,)),
tf.keras.layers.Dense(256, activation='softmax'),
tf.keras.layers.Dense(128, activation='softmax'),
tf.keras.layers.Dense(64, activation='softmax'),
tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10)
With that, it used to work with a simple MLPClassifier.
I also managed to get this nice error which does not seem to be fixed by changing the layers:
Received a label value of 1209638408 which is outside the valid range of [0, 1).
So I changed it a bit and came up with this:
features_train = features[:1500]
output_train = output[:1500]
features_test = features[1500:]
output_test = output[1500:]
classifier = Sequential()
classifier.add(Dense(units = 16, activation = 'relu', input_dim = 6))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 64, activation = 'relu'))
classifier.add(Dense(units = 32, activation = 'relu'))
classifier.add(Dense(units = 8, activation = 'relu'))
classifier.add(Dense(units = 2, activation = 'relu'))
classifier.add(Dense(units = 1))
classifier.compile(optimizer='rmsprop', loss='binary_crossentropy')
classifier.fit(features_train, output_train, batch_size = 1, epochs = 10)
But now I get a loss of 100%.
You should use a smaller network. Try with fewer Dense layers, 2 or 3 maximum. If you use the binary_crossentropy loss, use a sigmoid activation in the last Dense layer. You can also pass metrics=['accuracy'] when compiling the model to monitor the accuracy.

Keras Layers In Multiclass Classification Problem

I am doing a multiclass classification problem and my model is as follows
model = Sequential()
model.add(Dense(50, input_dim = 561, activation = 'relu'))
model.add(Dense(100, activation = 'relu'))
model.add(Dense(150, activation = 'softmax'))
model.add(Dense(75, activation = 'softmax'))
model.add(Dense(50, activation = 'softmax'))
model.add(Dense(6, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
print(model.summary())
and model.fit(X_train, y_train, epochs=80, batch_size=10) to compile the model
When I run it I am getting this error:
Shapes (None, 1) and (None, 6) are incompatible
Any one can guide my how to solve this?

Error while using both sparse_categorical_crossentropy and categorical_crossentropy in keras

I have started training a basic MLP model on MNIST data taken from here. Below is my code for implementing the model.
train = pd.read_csv(r"train.csv")
test = pd.read_csv(r"test.csv")
train_img_path = "./Images/train/"
test_img_path = "./Images/test/"
train_img = []
for img in train['filename']:
img_path = train_img_path+img
image = imread(img_path)
image = image/255
train_img.append(image)
train_img = np.array(train_img)
batch_size = 64
y_train = train['label']
from tensorflow.keras.utils import to_categorical
#y_train = to_categorical(y_train)
model = Sequential()
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_img, y_train, epochs=20, batch_size=batch_size)
While trying to fit my model on this data I get error InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [50176,10] and labels shape [64] with loss='sparse_categorical_crossentropy'.
There were suggestions to try with loss='categorical_crossentropy' after having one-hot encoded values and that also gives error ValueError: Shapes (None, 10) and (None, 28, 28, 10) are incompatible
I am confused on how I am getting the shape [50176,10] (though examples are 49000) in the error.
I guess I am missing something on shape. Can someone guide me where I am doing wrong and how to solve this.
Edit: I have modified my code as below to pick the data from keras for_from_dataframe. But I still get the same error.
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_data = train_datagen.flow_from_dataframe(
dataframe=train,
directory='./Images/train',
x_col='filename',
y_col='label',
weight_col=None,
target_size=(28,28),
color_mode='grayscale',
class_mode='categorical',
batch_size=64
)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
#model.summary()
model.fit(train_data, epochs=20)
The main problem is in your model building code:
model = Sequential()
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
You are trying to feed images and its label to ANN which obviously gives error. Also there is no any inputs given in your model.
For images, CNN should be used instead of ANN.
import tensorflow as tf
model = Sequential()
model.add(tf.keras.layers.Conv2D(32, activation = 'relu', input_shape=(28,28,3)))
model.add(tf.keras.layers.MaxPooling2D((2,2))
model.add(tf.keras.layers.Conv2D(64, activation = 'relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2))
model.add(tf.keras.layers.Conv2D(128, activation = 'relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2))
model.add(tf.keras.layers.Flatten())
model.add(Dense(10, activation = 'relu'))
model.add(Dense(20, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
If you have one-hot encoded your labels, use categorical_crossentropy. If your labels are numbers then use sparse_categorical_crossentropy

Value Error with input_length and shape in Keras

So, I am trying to build a text generator using Keras. I have 35983 sentences of length 50 i.e. my training data x has a shape of (35983,50,1). However, I cannot figure out how to give the input shape to my model ad keep running into errors. The other dimensions in my model are:
Embedding Matrix: (46, 45)
vocab_size = 45
Here is the structure of my model:
model = Sequential()
model.add(TimeDistributed(Embedding(vocab_size + 1, vocab_size, input_length= 50, weights = [embedding_weights], input_shape = (x.shape[1],x.shape[2]))))
model.add(TimeDistributed(Conv1D(64, 3, activation = 'relu', kernel_initializer= 'glorot_normal')))
model.add(Dropout(0.2))
model.add(TimeDistributed(MaxPooling1D(pool_size= 2)))
model.add(TimeDistributed(Conv1D(64, 3, activation = 'relu', kernel_initializer = 'glorot_normal')))
model.add(Dropout(0.2))
model.add(TimeDistributed(MaxPooling1D(pool_size= 2)))
model.add(TimeDistributed(Conv1D(64, 3, activation = 'relu', kernel_initializer = 'glorot_normal')))
model.add(Dropout(0.2))
model.add(TimeDistributed(MaxPooling1D(pool_size= 2)))
model.add(TimeDistributed(GlobalMaxPool1D()))
model.add(Bidirectional(LSTM(256, return_sequences = True)))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(256, return_sequences = True)))
model.add(Dropout(0.2))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'rmsprop', metrics = ['accuracy'])
model.build((None, x.shape[1],x.shape[2]))
model.summary()
Everytime I try to run the above code I get the following error:
ValueError: "input_length" is 50, but received input has shape (None, 1)
I have tried chopping and changing the shapes but I still get a similar error and I can't figure out how to get rid of it. Any help would be appreciated. Thank You.
May this will help you.
I = Input(shape=(50, 50)) # each search result is made of 50 words and 50 docs
emb = Embedding(
max_val,
embedding_dims,
dropout=embedding_dropout
)
right = TimeDistributed(emb)(I)
right = TimeDistributed(Convolution1D(nb_filter=5,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1)(right)
right = TimeDistributed(GlobalMaxPooling1D())(right)

Keras Model: Same array that is used for model.fit is not being processed in model.predict

I have a model:
model.add(Dense(16, input_dim = X.shape[1], activation = 'tanh'))
model.add(Dropout(0.2))
model.add(Dense(8, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(4, activation = 'tanh'))
model.add(Dropout(0.2))
model.add(Dense(2, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae'])
And during Model.evaluvate it works just fine with 'X' s input:
history = model.fit(X, Y, validation_split=0.2, epochs=10, callbacks= [PrintDot()], batch_size=10, verbose=0)
But during prediction as I use X[1] it throws an error:
ValueError: Error when checking input: expected dense_8_input to have shape (500,) but got array with shape (1,)
But X[1].Shape is (500,):
X[1].shape
--> (500,)
How can I mend this error, any help appreciated
Keras model.predict expects to receive input of (amount_of_items, features).
So even when attempting to predict a single sample, you must reshape it to (1, features) , and in your case, (1, 500).

Categories