I have 7 categories to classify into, i have used label encoder on my y_train even then i am getting this error and have also converted this to float. Pls look into this problem.Added the picture of all the shapes required
le = LabelEncoder()
yy_train=le.fit_transform(y_train)
yy_train=yy_train.astype(float)
model = Sequential()
model.add(Dense(186, input_shape=(180,), activation = 'relu'))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.6))
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
history = model.fit(X_train, yy_train, batch_size=64, epochs=30)
You should convert yy_train from an array of categories
to an array of binary values indicating the category.
e.g.
[1,3,10,6]
-->
[
[1,0,0,0,0,0,0,0,0,0]
[0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,1,0,0,0,0]
]
.
n = len(yy_train)
YY_train = np.zeros(n,10)
for i in range(n):
YY_train[i,yy_train[i]-1] = 1
Related
I am trying to run a simple RNN with some data extracted from a csv file. I have already preprocessed my data and split them into train set and validation set, but I get the error above.
This is my network structure and what I tryied so far. My shapes are (33714,12) for x_train, (33714,) for y_train, (3745,12) for x_val and (3745,) for y_val.
model = Sequential()
# LSTM LAYER IS ADDED TO MODEL WITH 128 CELLS IN IT
model.add(LSTM(128, input_shape=x_train.shape, activation='tanh', return_sequences=True))
model.add(Dropout(0.2)) # 20% DROPOUT ADDED FOR REGULARIZATION
model.add(BatchNormalization())
model.add(LSTM(128, input_shape=x_train.shape, activation='tanh', return_sequences=True)) # ADD ANOTHER LAYER
model.add(Dropout(0.1))
model.add(BatchNormalization())
model.add(LSTM(128, input_shape=x_train.shape, activation='tanh', return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(32, activation='relu')) # ADD A DENSE LAYER
model.add(Dropout(0.2))
model.add(Dense(2, activation='softmax')) # FINAL CLASSIFICATION LAYER WITH 2 CLASSES AND SOFTMAX
# ---------------------------------------------------------------------------------------------------
# OPTIMIZER SETTINGS
opt = tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE, decay=DECAY)
# MODEL COMPILE
model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
# CALLBACKS
tensorboard = TensorBoard(log_dir=f"logs/{NAME}")
filepath = "RNN_Final-{epoch:02d}-{val_acc:.3f}"
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor='val_acc', verbose=1,
save_best_only=True, mode='max')) # save only the best ones
# RUN THE MODEL
history = model.fit(x_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE,
validation_data=(x_val, y_val), callbacks=[tensorboard, checkpoint])
Though it will give you a large value, what may be best to do would be to flatten the one with the larger dimension.
A tensorflow.keras.layers.Flatten() will basically make your output shape the values multiplied, i.e. input: (None, 5, 5) -> Flatten() -> (None, 25)
For your example, this will give you:
(None, 33714,12) -> (None, 404568).
I'm not entirely sure if this will work when you change the shape sizes, but that is how I overcame my issue with incompatible shapes: expected: (None, x), got: (None, y, x).
I have a rnn and want to feed in a sentence with a length of 50, and have the output be the same length. (For a chatbot). Does anyone know why this error:
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 5000]
keeps appearing? Here is the code:
def model():
model = Sequential()
model.add(Embedding(vocab_size, 100, input_length=l))
model.add(Flatten())
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(100))
model.add(Dense(100, activation='relu'))
model.add(Dense(vocab_size, activation='softmax'))
model.summary()
return model
model = model()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(padded_x, padded_y, batch_size=128, epochs=100)
The shape of both arrays are 5000, 50....5000 sentences with 50 words each. They are already encoded. I first though it was because I was flattening...but this is the error before flattening:
ValueError: A target array with shape (5000, 50) was passed for an output of shape (None, 12097) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
##BTW the vocab_size is 12097##
Don't flatten. The size of the output you're expecting is 50, so you need 50 neurons in your last dense layer.
def model():
model = Sequential()
model.add(Embedding(vocab_size, 100, input_length=l))
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(100))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='softmax')) # 50 here, not 12,097
model.summary()
return model
I am trying to create a multi-channel 1D CNN for analyzing ECG signals. I have 258 12 lead ECGs with length 300 samples, so my input dimension is (258, 300, 12).
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', input_shape=(n_timesteps,n_features), padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=10, batch_size=20, verbose=1, validation_split = 0.2)
I'm running the code above, and getting the following error
ValueError: Error when checking target: expected dense_8 to have shape (2,) but got array with shape (1,)
Thanks for any help!
You are trying to train the model like this,
model.fit(X_train, y_train, epochs=10, batch_size=20, verbose=1, validation_split = 0.2)
The shape of y_train is something like (n, 1), where n is the number of samples used to train.
Now, you are building a model with last layer like this,
model.add(Dense(num_classes, activation='softmax'))
From the error message, it can be deduced that you are setting num_classes=2. So, the last layer will have 2 nodes. Such a model expects y_train to be of shape (n,2). But you are using y_train of shape (n,1).
In order to fix the error, you can change the last layer as,
num_classes = 1
model.add(Dense(num_classes, activation='sigmoid'))
Note that, the activation function should be changed to sigmoid.
So, you are solving a binary classification problem.
The error message indicates our model expects label with shape (2,) and i assume you are using num_classes=2. However, Your label is either 1 or 0 as the provided shape of label is (1,). To solve this error, you have yo change the output dense layer of your model, and the layer should have one neuron with sigmoid activation function.
model.add(Dense(num_classes, activation='sigmoid')) # num_classes=1
I get the error:
"Error when checking input: expected conv1d_41_input to have 3 dimensions, but got array with shape (1920, 5000)"
when trying to compile a CNN model in Keras.
My input data is 1920 samples with 5000 features.
I have tried adding a Flatten layer before the first Dense layer.
# Parameters
BATCH_SIZE = 16
DROP_OUT = 0.25
N_EPOCHS = 100
N_FILTERS = 128
TRAINABLE = False
LEARNING_RATE = 0.001
N_DIM = 32
KERNEL_SIZE = 7
# Create model
model = Sequential()
model.add(Conv1D(N_FILTERS, KERNEL_SIZE, activation='relu', padding='same',input_shape=(5000,1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(N_FILTERS, KERNEL_SIZE, activation='relu', padding='same'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(DROP_OUT))
model.add(Dense(N_DIM, activation='relu', kernel_regularizer=regularizers.l2(1e-4)))
model.add(Dense(N_LABELS, activation='sigmoid'))
adam = optimizers.Adam(lr=LEARNING_RATE, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
model.summary()
If you declare your input shape as input_shape=(5000,1), then your data should have shape (None, 5000,1), where the first dimension corresponds to samples, so in this case you just need to add the channels dimension with a value of one by reshaping:
X_train = X_train.reshape((-1, 5000, 1))
And do the same for any test or validation data.
I'm trying to run a few different models, which ran fine until I added a GlobalAveragePooling2D() which now throws the following error:
ValueError: Input 0 is incompatible with layer flatten_105: expected min_ndim=3, found ndim=2
I feel it has something to do with a GlobalAveragePooling2D() layer not being compatible with a flatten() layer and my understand is lacking, but I'm not sure.
My code is below. Could anyone enlighten me on what they think is going on? It ran fine without the GlobalAveragePooling2D layer. I was hoping to experiment with it though.
dense_layers = [1,2,3]
layer_sizes = [32, 64, 128]
con_layers = [1,2,3]
con_layer_sizes = [32, 64, 128, 512]
for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in con_layers:
for con_layer_size in con_layer_sizes:
img_size = 125
batch_size = 16
K.input_shape = (img_size, img_size)
NAME = "{}-conv-{}-con_layer_sizes-{}-nodes-{}-dense-{}".format(conv_layer, con_layer_size, layer_size, dense_layer, int(time.time()))
print(NAME)
tensorboard = TensorBoard(log_dir= 'logs/{}'.format(NAME))
mcp = ModelCheckpoint(filepath='C:\\Users\\jordan.howell\\models\\'+NAME+'_model.h5',monitor="val_loss"
, save_best_only=True, save_weights_only=False)
reduce_learning_rate = ReduceLROnPlateau(monitor='val_loss', factor=0.3,patience=2,cooldown=2
, min_lr=0.00001, verbose=1)
#start model build
model = Sequential()
model.add(Conv2D(con_layer_size, (3, 3), activation="relu", padding = 'same', input_shape=input_shape))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.15))
for l in range(conv_layer-1):
#Convolution
model.add(Conv2D(con_layer_size, (3, 3), activation="relu", padding = 'same'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.15))
model.add(GlobalAveragePooling2D())
# Flatten the layer
model.add(Flatten())
for l in range(dense_layer):
model.add(Dense(layer_size, activation = 'relu'))
model.add(Dense(activation = 'sigmoid', units = 1))
model.compile(loss ='binary_crossentropy', optimizer = 'adam'
, metrics=[km.binary_precision(), km.binary_recall()])
#generators = Generators(TRAIN_DATA_DIR, VALIDATION_DATA_DIR, TEST_DATA_DIR)
#train_generator = generators.train_generator(150, batch_size=32)
#validation_generator = generators.validation_generator(150, batch_size=16)
model.fit_generator(train_generator, steps_per_epoch=5216 // batch_size
,validation_data=validation_generator, validation_steps=1
, epochs = 50, callbacks = [reduce_learning_rate, tensorboard, mcp])
From Keras docs, GlobalAveragePooling2D input shape is 4D tensor, output shape is 2D tensor. In this case, Flatten is redundant.