I'm new to deep learning and tried to tune hyperparameters using kerastuner. Here is my code:
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch
from kerastuner import HyperModel
class MyHyperModel(HyperModel):
def __init__(self, input_shape):
self.input_shape = input_shape
def build(self, hp):
model = keras.Sequential()
model.add(layers.Dense(units=hp.Int('units',
min_value=10,
max_value=120,
step=10),
activation='relu',
input_shape=self.input_shape))
model.add(layers.Dense(units=hp.Int('units',min_value=10,max_value=20,step=2),
activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(
optimizer=keras.optimizers.Adam(
hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
loss='mse',
metrics=['mse'])
return model
input_shape = (X_train.shape[1],)
hypermodel = MyHyperModel(input_shape)
tuner = RandomSearch(
hypermodel,
objective='mse',
max_trials=10,
)
tuner.search(X_train, y_train,
epochs=50,
validation_data=(X_test, y_test),verbose=0)
When I tried to retrieve the best model using
tuner.get_best_models(num_models=1)[0]
I got the following error
ValueError: Shapes (320,) and (1,) are incompatible
I don't have this error when I deleted this hidden layer
model.add(layers.Dense(units=hp.Int('units',min_value=10,max_value=20,step=2),
activation='relu'))
Anyone knows how to fix this? Thanks in advance.
Related
I am trying to train my model using Keras and TensorFlow.
Code where I'm getting the error.
def build_model():
# define the model, use pre-trained weights for image_net
base_model = InceptionV3(input_shape=(resized_height, resized_width, num_channel), weights='imagenet', include_top=False, pooling='avg')
x = base_model.output
# x = Dense(100, activation='relu')(x)
# predictions = Dense(6, activation='sigmoid', name='final_classifier')(x)
# model = Model(inputs = base_model.input, outputs= predictions)
model = Sequential()
# # model.add(LSTM(1024, return_sequences=False, kernel_initializer='he_normal', dropout=0.15, recurrent_dropout=0.15, implementation=2))
model = Sequential()
model.add(Dense(1024, activation='relu', input_shape=(51200,)))(x)
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))(x)
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))(x)
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))(x)
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))(x)
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax', name='final_classifier'))(x)
return model
Build and Run the Model
model = build_model()
model_checkpoint = ModelCheckpoint(weight_file, monitor='val_loss', save_weights_only=False, save_best_only=True)
num_workers = 2
model.compile(optimizer=Adam(lr=initial_lr), loss='categorical_crossentropy', metrics=['accuracy'])
callbacks = [model_checkpoint, reduce_lr_on_plateau, tensor_board]
labels = labels_all
partition = partition_dict
model.fit_generator(generator=DataGenSequence(labels, partition['train'], current_state='train'),
steps_per_epoch=100,
epochs = 200,
verbose=1,
workers = num_workers,
callbacks=callbacks,
shuffle=False,
# maz_queue_size=32,
validation_data=DataGenSequence(labels, partition['valid'], current_state='validation'),
validation_steps=5
)
ERROR
Note: I am suffering from this error , I can't solved it and advanced thanks who are try to solve it and comment here for sharing the answer
Plz change the code to
def build_model():
# define the model, use pre-trained weights for image_net
base_model = InceptionV3(input_shape=(resized_height, resized_width, num_channel), weights='imagenet', include_top=False, pooling='avg')
#x = base_model.output
# x = Dense(100, activation='relu')(x)
# predictions = Dense(6, activation='sigmoid', name='final_classifier')(x)
# model = Model(inputs = base_model.input, outputs= predictions)
#model = Sequential()
# # model.add(LSTM(1024, return_sequences=False, kernel_initializer='he_normal', dropout=0.15, recurrent_dropout=0.15, implementation=2))
model = Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax', name='final_classifier'))
return model
Since you cannot pass x on sequential object
Sequentail().add() does not have a return value or if speaking in Python: return None wich is an object of the Type NoneType. So when you are calling like this: Sequential().add()(x) you are calling the method .add() from the class Sequential and then you are trying to call its return value. This does not work since the return value is not a function but None from the NoneType.
Hello I am trying to classify grayscale images (224x224) and I am trying to use LSTM for that but I keep getting the shape errors
my train datagenerator looks like this:
def train_datagenerator(train_batchsize):
train_datagen = ImageDataGenerator(
rescale=1 / 255.0,
rotation_range=20,
zoom_range=0.05,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
horizontal_flip=True,
fill_mode="nearest")
train_generator = train_datagen.flow_from_directory(train_dir,
target_size=(image_size, image_size),
batch_size=train_batchsize,
class_mode='categorical')
return train_generator
this is my code for my model:
def LSTM_model():
model = Sequential()
model.add(LSTM(512, input_shape=(224, 224)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(50))
model.add(Activation('sigmoid'))
model.add(Dense(3))
model.add(Activation('softmax'))
model.build()
model.summary()
return model
model.fit
def train(model):
train_generator = train_datagenerator(train_batchsize)
model.compile(loss='categorical_crossentropy',
#optimizer='sgd',
optimizer='adam',
metrics=['acc'])
train_start = time.clock()
print('Started training...')
history = model.fit_generator(train_generator,
steps_per_epoch=train_generator.samples / train_generator.batch_size,
epochs=epochs,
verbose=1)
train_finish = time.clock()
train_time = train_finish - train_start
print('Training completed in {0:.3f} minutes!'.format(train_time / 60))
print('Saving the trained model...')
model.save('/content/drive/My Drive/Project/trained_models/rnn_model.h5')
print("Saved trained model in 'trained_models/ folder'!")
return model, history
I get this error: Input 0 of layer lstm_5 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 150528]
Please help
I m not sure but can you try to this
model.add(LSTM(512, return_sequences=True, input_shape=(224, 224)))
I am currently using kerastuner ( 1.0.0) to fine-tune hyperparamteters in a CNN and it keeps displaying the error 'Too many failed attempts to build model.'. I have tried all other availabel solutions including those on this stackoverflow but none of them works. Does my model have too many parameters to tune?
from tensorflow.keras.datasets import fashion_mnist
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv2D, Dropout, MaxPooling2D
from tensorflow.keras.initializers import LecunNormal
!pip install keras-tuner==1.0.0
from kerastuner.tuners import RandomSearch
from kerastuner import HyperModel
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
class MCDropout(tf.keras.layers.Dropout):
def call(self, inputs):
return super().call(inputs, training=True)
NUM_CLASSES = 10
KERNEL_SIZE = (3, 3)
class CNNHyperModel(HyperModel):
def __init__(self, num_classes, kernel_size):
self.num_classes = num_classes
self.kernel_size = kernel_size
def build_model(self, hp):
model = keras.Sequential()
model.add(Conv2D(ilters=hp.Choice('input_units', values=[32, 16], default=16),
kernel_size=(self.kernel_size),
activation='selu',
kernel_intializer = LecunNormal,
input_shape=x_train.shape[1:]))
model.add(MCDropout(rate=hp.Float('dropout_0',min_value=0.0,max_value=0.2,default=0.1,step=0.1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
for i in range(hp.Choice('n_filters', 1, 4)):
model.add(Conv2D(hp.Choice(f'num_filters_{i}', values=[32, 16, 64], default=32),
kernel_size=(self.kernel_size),
activation='selu',
kernel_intializer = LecunNormal))
model.add(MCDropout(hp.Float(f'dropout_{i}',min_value=0.0,max_value=0.2,default=0.1,step=0.1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(units=hp.Choice('units',values = [128, 256, 512, 1024], defaults = 512),activation='selu'))
model.add(MCDropout(rate=hp.Float('dropout_5',min_value=0.3,max_value=0.7,default=0.4,step=0.1)))
model.add(Dense(self.num_classes, activation='softmax'))
model.compile(optimizer=keras.optimizers.Nadam(hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
hypermodel = CNNHyperModel(num_classes=NUM_CLASSES, kernel_size = KERNEL_SIZE)
SEED = 1
tuner = RandomSearch(
hypermodel,
objective='val_accuracy',
max_trials = 1,
executions_per_trial = 1
)
tuner.search(x=x_train,
y=y_train,
epochs=1,
batch_size=64,
validation_data =(x_test, y_test))```
I RUN THIS CODE AND IT KEEP DISPLAYING TOO MANY FAILED ATTEMPTS TO BUILD MODELS. (BTW I AM WRITING THIS ON GG COLAB.)
IS THERE ANYWAY TO FIX IT?
I try to use Keras Tuner for hyperparameter optimisation:
import keras
from kerastuner import HyperModel
from kerastuner.tuners import Hyperband
input_shape = (1, 28, 28)
num_classes = 10
# Define hypermodel class
class CNNHyperModel(HyperModel):
def __init__(self, input_shape, num_classes):
self.input_shape = input_shape
self.num_classes = num_classes
def build(self, hp):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation="softmax"))
model.compile(
loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=["accuracy"],
)
return model
# Instantiate
hypermodel = CNNHyperModel(input_shape=input_shape, num_classes=num_classes)
# Create tuner
HYPERBAND_MAX_EPOCHS = 40
MAX_TRIALS = 20
EXECUTION_PER_TRIAL = 2
SEED = 1
tuner = RandomSearch(
hypermodel,
max_epochs=HYPERBAND_MAX_EPOCHS,
objective='val_accuracy',
seed=SEED,
max_trials=MAX_TRIALS,
executions_per_trial=EXECUTION_PER_TRIAL,
directory='hyperband',
project_name='mnist'
)
I get
AttributeError: module 'tensorflow._api.v1.keras.metrics' has no attribute 'Metric'
with both Tensorflow 1.13 and 2.0 installed using conda.
Including from tensorflow.python.keras.metrics import Metric as suggested in this answer does not change anything.
Latest version of Tensorflow 2.6.0 has tf.keras.metrics.Metric API.
You can import as
from tensorflow.keras.metrics import Metric
Following is the code of one simple example for what I want to implement:
Error: raise TypeError("inputs must be a sequence"), TypeError: inputs must be a sequence
How to solve this to make the program can work? Any help will be appreciated.
from keras.models import Sequential
from keras.layers import LSTM, Dense, Flatten
import numpy as np
from keras.engine.topology import Layer
import tensorflow as tf
class MyLayer(Layer):
def __init__(self, **kwargs):
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
super(MyLayer, self).build(input_shape)
def call(self, x):
"Some other tf function will be put at here"
outputs, state = tf.contrib.rnn.static_rnn(tf.contrib.rnn.LSTMBlockCell(32), x, dtype=tf.float32)
return outputs
def compute_output_shape(self, input_shape):
return input_shape
def get_model(timesteps, data_dim):
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True))
model.add(MyLayer()) # this is my layer
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
return model
def run_demo():
data_dim = 16
timesteps = 8
num_classes = 10
model = get_model(timesteps, data_dim)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
"""Generate the traning and validation data"""
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_val, y_val))
if __name__ == "__main__":
run_demo()
Sorry, I am not too familiar with Recurrent Model.
But I think the problem is input size.
The size into custom layer (?, 8, 32)
but tf.nn.static_rnn require list like
so you need to change the input size then problem will fix.
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Flatten, Dense
class MyLayer(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
self.cell = tf.nn.rnn_cell.BasicRNNCell(32)
super(MyLayer, self).build(input_shape)
def call(self, x):
"Some other tf function will be put at here"
rnn_inputs = tf.unstack(x, axis=1)
outputs, state = tf.nn.static_rnn(self.cell, rnn_inputs, dtype=tf.float32)
for i in range(len(outputs)):
outputs[i] = tf.expand_dims(outputs[i], axis=1)
outputs = tf.concat(outputs, axis=1)
return outputs
def compute_output_shape(self, input_shape):
return input_shape
def get_model(timesteps, data_dim):
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True))
model.add(MyLayer()) # this is my layer
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
return model
def run_demo():
data_dim = 16
timesteps = 8
num_classes = 10
model = get_model(timesteps, data_dim)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
"""Generate the traning and validation data"""
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_val, y_val))
if __name__ == "__main__":
run_demo()