I need to make a model that has 2 dropout layers and two LSTM layers. Unfortunately I have a problem with input shape that goes to my second LSTM layer. After searching for the problem I found out I need to change the input dimensions but I don't know how to do that. I found an option that requires using Lambda layer but I can't import it to my environmet (it's a coursera environment). Have you got any suggestions how to deal with my error?
model = Sequential()
Layer1 = model.add(Embedding(total_words, 64, input_length=max_sequence_len-1))
Layer2 = model.add(Bidirectional(LSTM(20)))
Layer3 = model.add(Dropout(.03))
Layer4 = model.add(LSTM(20))
Layer5 = model.add(Dense(total_words,
kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4),
bias_regularizer=regularizers.l2(1e-4),
activity_regularizer=regularizers.l2(1e-5)))
# A Dense Layer including regularizers
Layer6 = model.add(Dense(total_words, activation = 'softmax'))
# Pick an optimizer
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.summary()
Error:
ValueError: Input 0 of layer lstm_20 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 40]
Thank you #Marco Cerliani and #Joanna Kastelik for the update.
For the benefit of community providing solution here using sample data as shown below
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Bidirectional, LSTM, Dropout, Dense
from tensorflow.keras.regularizers import l1_l2, l2
total_words = 478
max_sequence_len = 90
model = Sequential()
Layer1 = model.add(Embedding(total_words, 64, input_length=max_sequence_len-1))
Layer2 = model.add(Bidirectional(LSTM(20)))
Layer3 = model.add(Dropout(.03))
Layer4 = model.add(LSTM(20))
Layer5 = model.add(Dense(total_words,
kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4),
bias_regularizer=l2(1e-4),
activity_regularizer=l2(1e-5)))
# A Dense Layer including regularizers
Layer6 = model.add(Dense(total_words, activation = 'softmax'))
# Pick an optimizer
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.summary()
Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-8ce04225c92d> in <module>()
12 Layer2 = model.add(Bidirectional(LSTM(20)))
13 Layer3 = model.add(Dropout(.03))
---> 14 Layer4 = model.add(LSTM(20))
15 Layer5 = model.add(Dense(total_words,
16 kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4),
8 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
221 'expected ndim=' + str(spec.ndim) + ', found ndim=' +
222 str(ndim) + '. Full shape received: ' +
--> 223 str(tuple(shape)))
224 if spec.max_ndim is not None:
225 ndim = x.shape.rank
ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 40)
Fixed code:
Your issue can be resolved once you add return_sequences=True to the LSTM (i.e, Layer2) layer.
model = Sequential()
Layer1 = model.add(Embedding(total_words, 64, input_length=max_sequence_len-1))
Layer2 = model.add(Bidirectional(LSTM(20, return_sequences=True)))
Layer3 = model.add(Dropout(.03))
Layer4 = model.add(LSTM(20))
Layer5 = model.add(Dense(total_words,
kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4),
bias_regularizer=l2(1e-4),
activity_regularizer=l2(1e-5)))
# A Dense Layer including regularizers
Layer6 = model.add(Dense(total_words, activation = 'softmax'))
# Pick an optimizer
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.summary()
Output:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 89, 64) 30592
_________________________________________________________________
bidirectional_1 (Bidirection (None, 89, 40) 13600
_________________________________________________________________
dropout_1 (Dropout) (None, 89, 40) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 20) 4880
_________________________________________________________________
dense (Dense) (None, 478) 10038
_________________________________________________________________
dense_1 (Dense) (None, 478) 228962
=================================================================
Total params: 288,072
Trainable params: 288,072
Non-trainable params: 0
_________________________________________________________________
Related
I've been trying to train a CNN model with facial data for creating emojies using facial expression.I'm actually new to machine learing. The code isn't actually my own but I keep getting this ValueError while trying to train the model.
ValueError: One of the dimensions in the output is <= 0 due to downsampling in conv2d. Consider increasing the input size. Received input shape [None, 100, 100, 1] which would produce output shape with a zero or negative value in a dimension.
The code which I'm trying to run is:
def cnn_model():
num_of_classes = get_num_of_classes()
model = Sequential()
model.add(Conv2D(32, (5,5), input_shape=(image_x, image_y, 1), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(10, 10), strides=(10, 10), padding='same'))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.6))
model.add(Dense(num_of_classes, activation='softmax'))
sgd = optimizers.SGD(lr=1e-2)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
filepath="cnn_model_keras.h5"
checkpoint1 = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint1]
from keras.utils import plot_model
plot_model(model, to_file='model.png', show_shapes=True)
return model, callbacks_list
num_of_classes value = 12
image_x,image_y = 100
I reorganized your code, you are defining a function to build the model, It would be better to keep only what is related to the model architecture and keep the callbacks and model plotting out of the function.
from keras.models import Sequential
from tensorflow.keras.layers import Conv2D, BatchNormalization, MaxPooling2D,Flatten,Dropout,Dense
from tensorflow.keras import optimizers
import numpy as np
def cnn_model():
image_x = 100
image_y = 100
model = Sequential()
model.add(Conv2D(32, (5,5), input_shape=(image_x, image_y, 1), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(10, 10), strides=(10, 10), padding='same'))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.6))
model.add(Dense(12, activation='softmax'))
sgd = optimizers.SGD(lr=1e-2)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
########################
# Model summary
model = cnn_model()
model.summary()
#################### TEST
input = np.ones((1,100,100,1))
print("Output:", model.predict(input))
Output:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 96, 96, 32) 832
_________________________________________________________________
batch_normalization (BatchNo (None, 96, 96, 32) 128
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 10, 10, 32) 0
_________________________________________________________________
flatten (Flatten) (None, 3200) 0
_________________________________________________________________
dense (Dense) (None, 1024) 3277824
_________________________________________________________________
batch_normalization_1 (Batch (None, 1024) 4096
_________________________________________________________________
dropout (Dropout) (None, 1024) 0
_________________________________________________________________
dense_1 (Dense) (None, 12) 12300
=================================================================
Total params: 3,295,180
Trainable params: 3,293,068
Non-trainable params: 2,112
_________________________________________________________________
2022-07-03 15:15:36.927515: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Output: [[0.06614503 0.10535268 0.07621874 0.08486015 0.08070944 0.08046351
0.06786356 0.06059184 0.10280456 0.05683669 0.12510006 0.09305366]]
I am trying to create a Flappy Bird AI with Convolutional Layers and Dense Layers, but at the "Train" step (Function fit()) I get the following error message:
dqn.fit(env, nb_steps=500000, visualize=False, verbose=2)
Training for 500000 steps ...
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-53-e21cf8798454> in <module>()
----> 1 dqn.fit(env, nb_steps=500000, visualize=False, verbose=2) #fit = training, training for 5 Mio, timesteps eig bei 5000000
2 #value's which are important: episode reward, mean reward
7 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training_utils_v1.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
634 ': expected ' + names[i] + ' to have ' +
635 str(len(shape)) + ' dimensions, but got array '
--> 636 'with shape ' + str(data_shape))
637 if not check_batch_axis:
638 data_shape = data_shape[1:]
ValueError: Error when checking input: expected Input_input to have 4 dimensions, but got array with shape (1, 1, 2)
I have found an example on the internet where only Dense Layers were used (Copyright (c) 2020 Gabriel Nogueira (Talendar)). I would like to build a network with Conv2D and Dense Layers, but something doesn't seem to fit.
The code is built as follows:
import sys
import os
import flappy_bird_gym
env = flappy_bird_gym.make("FlappyBird-v0") #greyscale format
env.action_space #Discrete(2)
env.observation_space #Box(-inf, inf, (2,), float32)
actions = env.action_space.n #2
obs = env.observation_space.shape[0] #2
#Network:
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, Input
import numpy as np
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
#build model
def build_model(obs, actions):
model = Sequential()
model.add(Conv2D(32, (8,8), name='Input', padding='same',input_shape=(1,obs,1)))
model.add(MaxPooling2D((2,2), padding='same', name='maxpooling1'))
model.add(Conv2D(64, (4,4), padding='same', activation='relu', name='Conv1'))
model.add(MaxPooling2D((2,2), padding='same', name='maxpooling2'))
model.add(Conv2D(64, (3,3), padding='same', activation='relu', name='Conv2'))
model.add(MaxPooling2D((2,2), padding='same', name='maxpooling3'))
model.add(Flatten())
model.add(Dense(256, activation='relu', name='Dense1'))
model.add(Dense(actions, activation='linear',name='Output'))
return model
model = build_model(obs, actions)
model.summary()
Model: "sequential_15"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Input (Conv2D) (None, 1, 2, 32) 2080
maxpooling1 (MaxPooling2D) (None, 1, 1, 32) 0
Conv1 (Conv2D) (None, 1, 1, 64) 32832
maxpooling2 (MaxPooling2D) (None, 1, 1, 64) 0
Conv2 (Conv2D) (None, 1, 1, 64) 36928
maxpooling3 (MaxPooling2D) (None, 1, 1, 64) 0
flatten_20 (Flatten) (None, 64) 0
Dense1 (Dense) (None, 256) 16640
Output (Dense) (None, 2) 514
=================================================================
Total params: 88,994
Trainable params: 88,994
Non-trainable params: 0
_________________________________________________________________
#RL
from rl.agents import DQNAgent
from rl.memory import SequentialMemory
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy
#build agent:
def build_agent():
policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=0.5, value_min=.0001, value_test=.0, nb_steps=6000000)
memory = SequentialMemory(limit=100000, window_length=1)
dqn = DQNAgent(model=model, memory=memory, policy=policy, #RL Algorithm
enable_dueling_network=True, dueling_type='avg', #technique you use
nb_actions=actions, nb_steps_warmup=5000)
return dqn
dqn = build_agent()
#train:
from tensorflow.keras.optimizers import Adam
dqn.compile(Adam(lr=0.00025))
dqn.fit(env, nb_steps=500000, visualize=False, verbose=2) #here the error occurs
--> in the last line the error occurs
Does anyone know what I am doing wrong or what I need to change?
The error is coming from your input data.
As you can see the first layer is expecting the data to have dimension (None, 1, 2, 32) (The None is just the number of samples in the array). The key thing is your data has shape (1,2,2) and not (1, 2, 32). If you show us your data or maybe the what kind of data we can probably help a bit more on how to reshape it properly in order for the error to disappear.
I'm trying to run keras model as follows:
model = Sequential()
model.add(Dense(10, activation='relu',input_shape=(286,)))
model.add(Dense(1, activation='softmax',input_shape=(324827, 286)))
This code works, but if I'm trying to add an embedding layer:
model = Sequential()
model.add(Embedding(286,64, input_shape=(286,)))
model.add(Dense(10, activation='relu',input_shape=(286,)))
model.add(Dense(1, activation='softmax',input_shape=(324827, 286)))
I'm getting the following error :
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (324827, 1)
My data have 286 features and 324827 rows.
I'm probably doing something wrong with the shape definitions, can you tell me what it is ?
Thanks
You don't need to provide the input_shape in the second Dense layer, and neither the first one, only on the first layer, the following layers shape will be coomputed :
from tensorflow.keras.layers import Embedding, Dense
from tensorflow.keras.models import Sequential
# 286 features and 324827 rows (324827, 286)
model = Sequential()
model.add(Embedding(286,64, input_shape=(286,)))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='softmax'))
model.compile(loss='mse', optimizer='adam')
model.summary()
returns :
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_2 (Embedding) (None, 286, 64) 18304
_________________________________________________________________
dense_2 (Dense) (None, 286, 10) 650
_________________________________________________________________
dense_3 (Dense) (None, 286, 1) 11
=================================================================
Total params: 18,965
Trainable params: 18,965
Non-trainable params: 0
_________________________________________________________________
I hope it's what you're looking for
I am trying to modify the network which is implemented here. This network uses chest x ray images as input and classifies it into 14 categories (13 types of diseases and no finding). The network does not take the patient age and gender as an input. So I want to provide the network with that information too. In short At the last 3 layers of the network is like the following:
bn (BatchNormalization) (None, 7, 7, 1024) 4096 conv5_block16_concat[0][0]
__________________________________________________________________________________________________
avg_pool (GlobalAveragePooling2 (None, 1024) 0 bn[0][0]
__________________________________________________________________________________________________
predictions (Dense) (None, 14) 14350 avg_pool[0][0]
So what I have done so far is the following:
I simply pop the last dense layer using model_vgg16.layers.pop().
Then as expexted the network turns into:
bn (BatchNormalization) (None, 7, 7, 1024) 4096 conv5_block16_concat[0][0]
__________________________________________________________________________________________________
avg_pool (GlobalAveragePooling2 (None, 1024) 0 bn[0][0]
I know that I can add a layer using:
new_layer = Dense(14, activation='softmax', name='my_dense')
inp = model.input
out = new_layer(model.layers[-1].output)
model2 = Model(inp, out)
But I do not know how to add a layer that takes the inputs from previous layer together with 1 scalar value (age [0:100]), and one binary value gender [0:1].
So How can I add a last layer that takes inputs from previous layer together with 1 scalar value and 1 binary value?
Edit: The base model I am using is DenseNet121. The some final layers looks like this:
EDIT
The way I load the model is the following:
cp = ConfigParser()
cp.read(config_file)
# default config
output_dir = cp["DEFAULT"].get("output_dir")
base_model_name = cp["DEFAULT"].get("base_model_name")
class_names = cp["DEFAULT"].get("class_names").split(",")
image_source_dir = cp["DEFAULT"].get("image_source_dir")
image_dimension = cp["TRAIN"].getint("image_dimension")
output_weights_name = cp["TRAIN"].get("output_weights_name")
weights_path = os.path.join(output_dir, output_weights_name)
best_weights_path = os.path.join(output_dir, f"best_{output_weights_name}")
model_weights_path = best_weights_path
model_factory = ModelFactory()
model = model_factory.get_model(
class_names,
model_name=base_model_name,
use_base_weights=False,
weights_path=model_weights_path)
Now the model is in variable model.
Then as suggested I do
x = model.output
flat1 = Flatten()(x)
and get this error:
ValueError: Input 0 is incompatible with layer flatten_27: expected min_ndim=3, found ndim=2
When I repeat the same thing after removing the last layer using model.layers.pop()
I still get the same error? Even though I have spent couple of hours cannot overcome that problem. So how can this be done?
try this
from keras.applications.densenet import DenseNet121
from keras.layers import Dense, GlobalAveragePooling2D,concatenate
input_image = Input(shape=(224, 224, 3))
# normalize age
input_age_and_gender = Input(shape=(2,))
base_model = DenseNet121(input_tensor=input_image, weights='imagenet', include_top=False)
x = base_model.output
encoded_image = GlobalAveragePooling2D()(x)
out = concatenate([encoded_image,input_age_and_gender])
output = Dense(14, activation='softmax')(out)
model = Model([input_image,input_age_and_gender],output)
You can have a multi input model.
So instead of just using this:
img_input = Input(shape=input_shape)
base_model = base_model_class(
include_top=False,
input_tensor=img_input,
input_shape=input_shape,
weights=base_weights,
pooling="avg")
x = base_model.output
predictions = Dense(len(class_names), activation="sigmoid", name="predictions")(x)
model = Model(inputs=img_input, outputs=predictions)
I am not sure what your base_model looks like there. BUT for the sake of it check the following, where the first input is imaginary and the shape of the second input should be the shape of your age_gender_df.values:
input1 = Input(shape=(64,64,1))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(input1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)
# INSTEAD OF THE ABOVE INPUT I WROTE YOU CAN USE YOUR BASE MODEL
input2 = Input(shape=(2,2)) # HERE THIS SHOULD BE THE SHAPE OF YOUR AGE/GENDER DF
layer = Dense(10, activation='relu')(input2)
flat2 = Flatten()(layer)
merge = concatenate([flat1, flat2])
# interpretation model
hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(14, activation='linear')(hidden2)
model = Model(inputs=[input1, input2], outputs=output)
Summary
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_30 (InputLayer) (None, 64, 64, 1) 0
__________________________________________________________________________________________________
conv2d_23 (Conv2D) (None, 61, 61, 32) 544 input_30[0][0]
__________________________________________________________________________________________________
max_pooling2d_23 (MaxPooling2D) (None, 30, 30, 32) 0 conv2d_23[0][0]
__________________________________________________________________________________________________
conv2d_24 (Conv2D) (None, 27, 27, 16) 8208 max_pooling2d_23[0][0]
__________________________________________________________________________________________________
input_31 (InputLayer) (None, 2, 2) 0
__________________________________________________________________________________________________
max_pooling2d_24 (MaxPooling2D) (None, 13, 13, 16) 0 conv2d_24[0][0]
__________________________________________________________________________________________________
dense_38 (Dense) (None, 2, 10) 30 input_31[0][0]
__________________________________________________________________________________________________
flatten_23 (Flatten) (None, 2704) 0 max_pooling2d_24[0][0]
__________________________________________________________________________________________________
flatten_24 (Flatten) (None, 20) 0 dense_38[0][0]
__________________________________________________________________________________________________
concatenate_9 (Concatenate) (None, 2724) 0 flatten_23[0][0]
flatten_24[0][0]
__________________________________________________________________________________________________
dense_39 (Dense) (None, 10) 27250 concatenate_9[0][0]
__________________________________________________________________________________________________
dense_40 (Dense) (None, 10) 110 dense_39[0][0]
__________________________________________________________________________________________________
dense_41 (Dense) (None, 14) 154 dense_40[0][0]
==================================================================================================
Total params: 36,296
Trainable params: 36,296
Non-trainable params: 0
Visualisation:
EDIT:
In your case I suppose the model should look like the following:
img_input = Input(shape=input_shape)
base_model = base_model_class(
include_top=False,
input_tensor=img_input,
input_shape=input_shape,
weights=base_weights,
pooling="avg")
x = base_model.output
flat1 = Flatten()(x)
input2 = Input(shape=(2,2)) # HERE THIS SHOULD BE THE SHAPE OF YOUR AGE/GENDER DF
layer = Dense(10, activation='relu')(input2)
flat2 = Flatten()(layer)
merge = concatenate([flat1, flat2])
# interpretation model
hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(14, activation='linear')(hidden2)
model = Model(inputs=[img_input, input2], outputs=output)
Issue is solved by first removing the last layer (prediction layer) by
model_original.layers.pop()
Then defining another model which is the replica of the original model except the last layer
model2 = keras.Model(model_original.input, model_original.layers[-1].output)
after that the input, which includes the age is defined
age = layers.Input(shape=(1,))
Next, age input and the last layer of the previously defined network is concatenated using
x = model2.output
concatenated = layers.concatenate([x, age])
In th final step, a prediction layer is added after the concatenation to complete the network
output = Dense(14, activation='linear')(concatenated)
model3 = keras.Model(inputs=[model_original.input, age], outputs=output)
So the final layers of the design looks like that:
Please, help to define appropriate Dense input shapes in keras models. Maybe I have to reshape my data first. I have data set with dimensions shown below:
Data shapes are X_train: (2858, 2037) y_train: (2858, 1) X_test: (715, 2037) y_test: (715, 1)
Number of features (input shape) is 2037
I want to define Sequential keras model like that
``
batch_size = 128
num_classes = 2
epochs = 20
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(X_input_shape,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.summary()
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(),
from_logits=True,
metrics=['accuracy'])
``
Model summary:
``
Layer (type) Output Shape Param #
=================================================================
dense_20 (Dense) (None, 512) 1043456
_________________________________________________________________
dropout_12 (Dropout) (None, 512) 0
_________________________________________________________________
dense_21 (Dense) (None, 512) 262656
=================================================================
Total params: 1,306,112
Trainable params: 1,306,112
Non-trainable params: 0
``
And when I try to fit it...
``
history = model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test))
``
I got an error:
``
ValueError: Error when checking target: expected dense_21 to have shape (512,) but got array with shape (1,)
``
Modify
model.add(Dense(512, activation='relu'))
to
model.add(Dense(1, activation='relu'))
The output shape to be of size 1, same as y_train.shape[1].