There are similar questions but they are either outdated or doesn't work for my case.
This is my code:
left = Sequential()
left.add(LSTM(units=24,input_shape=(left_X.shape[1], left_X.shape[2])))
left.add(Dense(1))
right = Sequential()
right.add(LSTM(units=24,input_shape=(right_X.shape[1], right_X.shape[2])))
right.add(Dense(1))
model = Sequential()
model.add(Concatenate([left,right]))
model.add(Flatten())
model.add(Dense(1, activation='linear'))
model.compile(loss='mse',
optimizer='adam',
metrics=['mae'])
history = model.fit([left_X, right_X], train_y,
epochs=40,
validation_split=0.2,
verbose=1)
It raises an Assertion Error for fit
585 # since `Sequential` depends on `Model`.
586 if isinstance(inputs, list):
--> 587 assert len(inputs) == 1
588 inputs = inputs[0]
589 self.build(input_shape=(None,) + inputs.shape[1:])
I solved the problem using the following code, which uses Keras functional API:
inp1 = Input(shape=(train_X_1.shape[1], train_X_1.shape[2]))
inp2 = Input(shape=(train_X_2.shape[1], train_X_2.shape[2]))
inp3 = Input(shape=(train_X_3.shape[1], train_X_3.shape[2]))
x = SimpleRNN(10)(inp1)
x = Dense(1)(x)
y = LSTM(10)(inp2)
y = Dense(1)(y)
z = LSTM(10)(inp3)
z = Dense(1)(z)
w = concatenate([x, y, z])
# u = Dense(3)(w)
out = Dense(1, activation='linear')(w)
model = Model(inputs=[inp1, inp2, inp3], outputs=out)
model.compile(loss='logcosh',
optimizer='adam',
metrics=['mae'])
history = model.fit([train_X_1, train_X_2, train_X_3], train_y,
epochs=20,
validation_split=0.1,
verbose=1)
Related
I am trying to tune hyperparameter on the KerasRegressor
However, i only get the result of NaN's which is shown below, may i know what cause the issue?
everything works fine when i try to compile my model... but the scoring for the best parameters it always show NaNs, metrics that i used is RMSE
code snippet at below:
def create_model(optimizer,activation,lstm_unit_1,lstm_unit_2,lstm_unit_3, init='glorot_uniform'):
model = Sequential()
model.add(Conv1D(lstm_unit_1, kernel_size=1, activation=activation, input_shape = (trainX.shape[1], trainX.shape[2])))
model.add(GRU(lstm_unit_2, activation = activation, return_sequences=True, input_shape = (trainX.shape[1], trainX.shape[2])))
model.add(GRU(lstm_unit_3, activation = activation, return_sequences=True, input_shape = (trainX.shape[1], trainX.shape[2])))
model.add(Dense(units = 1))
model.add(Flatten())
model.compile(optimizer = optimizer, loss = 'mse', metrics = ['mean_squared_error'])
return model
model = tf.keras.wrappers.scikit_learn.KerasRegressor(build_fn = create_model,
epochs = 150,
verbose=False)
batch_size = [16,32,64,128]
lstm_unit_1 = [128,256,512]
lstm_unit_2 = lstm_unit_1.copy()
lstm_unit_3 = lstm_unit_1.copy()
optimizer = ['SGD','Adam','Adamax','RMSprop']
activation = ['relu','linear','sigmoid',]
param_grid = dict(lstm_unit_1=lstm_unit_1,
lstm_unit_2=lstm_unit_2,
lstm_unit_3=lstm_unit_3,
optimizer=optimizer,
activation=activation,
batch_size = batch_size)
warnings.filterwarnings("ignore")
random = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_jobs=-1, scoring='neg_mean_squared_error')
random_result = random.fit(trainX,trainY)
print(random_result.best_score_)
print(random_result.best_params_)
def rnn_model(self,activation="relu"):
in_out_neurons = 50
n_hidden = 512
model = Sequential()
model.add(LSTM(n_hidden, batch_input_shape=(None, self.seq_len, in_out_neurons), return_sequences=True))
model.add(Dense(in_out_neurons, activation=activation))
optimizer = Adam(learning_rate=0.001)
model.compile(loss="mean_squared_error", optimizer=optimizer)
model.summary()
return model
# then try to fit the model
final_x = np.zeros((319083, 2, 50))
final_y = np.zeros((319083, 1, 50))
# this works.
model = self.rnn_model()
model.fit(
final_x,final_y,
batch_size=400,
epochs=10,
validation_split=0.1
)
#However, when I trid to use hyperparameter sarch, this shows the error `ValueError: Invalid shape for y: (319083, 1, 50)`
activation = ["relu","sigmoid"]
model = KerasClassifier(build_fn=self.rnn_model,verbose=0)
param_grid = dict(activation=activation)
grid = GridSearchCV(estimator=model,param_grid=param_grid)
grid_result= grid.fit(final_x,final_y)
How dimension changes when using GridSearchCV
You should be using a KerasRegressor, since your model is not a classifier in that sense:
import tensorflow as tf
import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasRegressor
def rnn_model(activation="relu"):
in_out_neurons = 50
n_hidden = 512
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(n_hidden, batch_input_shape=(None, 2, in_out_neurons), return_sequences=True))
model.add(tf.keras.layers.Dense(in_out_neurons, activation=activation))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss="mean_squared_error", optimizer=optimizer)
model.summary()
return model
final_x = np.zeros((319083, 2, 50))
final_y = np.zeros((319083, 2, 50))
model = rnn_model()
activation = ["relu","sigmoid"]
model = KerasRegressor(build_fn=rnn_model,verbose=0)
param_grid = dict(activation=activation)
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result= grid.fit(final_x,final_y)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) # run with a way smaller dataset
Best: 0.000000 using {'activation': 'relu'}
I am having difficulty figuring out how to pass multiple arguments through keras tuner function. I looked all over all available documentation and questions related to this and I could not find anything for this particular problem.
I just want to be able to pass additional arguments through this function:
def build_model(hp, some_val_1, some_val_2)
Overall Code (Simplified):
import kerastuner as kt
def build_model(hp, some_val_1, some_val_2):
print(some_val_1)
print(some_val_2)
conv1d_val_1 = hp.Int("1-input_units", min_value=32, max_value=1028, step=64)
conv1d_filt_1 = hp.Int("1b-filter_units", min_value=2, max_value=10, step=1)
model.add(Conv1D(conv1d_val_1, conv1d_filt_1, activation='relu', input_shape=input_shape, padding='SAME'))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
return model
model = kt.Hyperband(build_model, objective="val_loss", max_epochs = 10, factor = 3, directory=os.path.normpath(path_save_dir))
model.search(x=x_train, y=y_train, epochs=10, batch_size=500, validation_data=(x_test, y_test), shuffle=True)
Attempt #1 (I tried many variations) - Does not work:
model = kt.Hyperband(build_model(kt.HyperParameters(), some_val_1, some_val_2), objective="val_loss", max_epochs = 10, factor = 3, directory=os.path.normpath(path_save_dir))
Attempt #2 (I tried many variations) - Does not work:
model = kt.Hyperband(build_model, some_val_1='1', some_val_2='2',objective="val_loss", max_epochs = 10, factor = 3, directory=os.path.normpath(path_save_dir))
Attempt #3 (I tried many variations) - Does not work:
model = kt.Hyperband(build_model, args=(some_val_1, some_val_2,),objective="val_loss", max_epochs = 10, factor = 3, directory=os.path.normpath(path_save_dir))
Please send help
You can create your own HyperModel subclass to do achieve this, check this link.
Example implementation, which will do what you are trying to do :-
import kerastuner as kt
class MyHyperModel(kt.HyperModel):
def __init__(self, some_val_1, some_val_2):
self.some_val_1 = some_val_1
self.some_val_2 = some_val_2
def build(self, hp):
## You can use self.some_val_1 and self.some_val_2 here
conv1d_val_1 = hp.Int("1-input_units", min_value=32, max_value=1028, step=64)
conv1d_filt_1 = hp.Int("1b-filter_units", min_value=2, max_value=10, step=1)
model.add(Conv1D(conv1d_val_1, conv1d_filt_1, activation='relu', input_shape=input_shape, padding='SAME'))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
return model
some_val_1 = 10
some_val_2 = 20
my_hyper_model = MyHyperModel(some_val_1 = some_val_1, some_val_2 = some_val_2)
model = kt.Hyperband(my_hyper_model, objective="val_loss", max_epochs = 10,
factor = 3, directory=os.path.normpath(path_save_dir))
Adding a complete example with the HyperModel tuned (I use input_shape and output_shape for some_val_1 and some_val_2).
## The hypermodel
class MyHyperModel(keras_tuner.HyperModel):
def __init__(self, input_shape, output_shape):
self.input_shape = input_shape
self.output_shape = output_shape
def build(self, hp):
model = keras.Sequential()
model.add(keras.Input(shape=(self.input_shape,)))
model.add(
layers.Dense(
units=hp.Int("units", min_value=32, max_value=64, step=32),
activation="relu"
)
) # tuning number of layers
model.add(layers.Dense(self.output_shape, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
return model
## The tuner
tuner = keras_tuner.RandomSearch(
hypermodel=CustomHyperModel(input_shape, output_shape),
objective='val_accuracy',
max_trials=3,
overwrite=True
)
tuner.search(X_train, y_train, epochs=3, validation_data=(X_val, y_val))
## The final model
model = tuner.get_best_models()[0]
model.summary()
I try to run this code (binary classification) but still stuck with this error : ValueError: Error when checking target: expected avg_pool to have 4 dimensions, but got array with shape (100, 2)
NUM_CLASSES = 2
CHANNELS = 3
IMAGE_RESIZE = 224
RESNET50_POOLING_AVERAGE = 'avg'
DENSE_LAYER_ACTIVATION = 'softmax'
OBJECTIVE_FUNCTION = 'binary_crossentropy'
NUM_EPOCHS = 10
EARLY_STOP_PATIENCE = 3
STEPS_PER_EPOCH_VALIDATION = 10
BATCH_SIZE_TRAINING = 100
BATCH_SIZE_VALIDATION = 100
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
train_data_dir = "C:\\Users\\Desktop\\RESNET"
model = ResNet50(include_top=True, weights='imagenet')
x = model.get_layer('avg_pool').output
predictions = Dense(1, activation='sigmoid')(x)
model = Model(input = model.input, output = predictions)
print(model.summary())
model.layers.pop()
model = Model(input=model.input,output=model.layers[-1].output)
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9), metrics= ['binary_accuracy'])
data_dir = "C:\\Users\\Desktop\\RESNET"
batch_size = 32
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
image_size = IMAGE_RESIZE
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
def append_ext(fn):
return fn+".jpg"
dir_path = os.path.dirname(os.path.realpath(__file__))
train_dir_path = dir_path + '\data'
onlyfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
NUM_CLASSES = 2
data_labels = [0, 1]
t = []
maxi = 25145
LieOffset = 15799
i = 0
while i < maxi: # t = tuple
if i <= LieOffset:
t.append(label['Lie'])
else:
t.append(label['Truth'])
i = i+1
train_datagenerator = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2)
train_generator = train_datagenerator.flow_from_directory(
train_data_dir,
target_size=(image_size, image_size),
batch_size=BATCH_SIZE_TRAINING,
class_mode='categorical', shuffle=False, subset='training') # set as training data
validation_generator = train_datagenerator.flow_from_directory(
train_data_dir, # same directory as training data
target_size=(image_size, image_size),
batch_size=BATCH_SIZE_TRAINING,
class_mode='categorical', shuffle=False, subset='validation')
(BATCH_SIZE_TRAINING, len(train_generator), BATCH_SIZE_VALIDATION, len(validation_generator))
from sklearn.grid_search import ParameterGrid
param_grid = {'epochs': [5, 10, 15], 'steps_per_epoch' : [10, 20, 50]}
grid = ParameterGrid(param_grid)
# Accumulate history of all permutations (may be for viewing trend) and keep watching for lowest val_loss as final model
for params in grid:
fit_history = model.fit_generator(
train_generator,
steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
epochs = NUM_EPOCHS,
validation_data=validation_generator,
validation_steps=STEPS_PER_EPOCH_VALIDATION,
callbacks=[cb_checkpointer, cb_early_stopper]
)
model.load_weights("../working/best.hdf5")
Remove these lines and it'll work,
model.layers.pop()
model = Model(input=model.input,output=model.layers[-1].output)
The former is removing the last(Dense) layer, and the letter means create a model without the last(Flatten, as Dense is already popped) layer.
It doesn't make sense as your target data is (100, 2). Why do you put them there in the first place?
Also I think this line
predictions = Dense(1, activation='sigmoid')(x)
Will error, as your target data is 2 channel, if it does error then change this to
predictions = Dense(2, activation='sigmoid')(x)
Update
The output of avg_pool is 4 dimention, (batch_size, height, width, channel). You need to do the Flatten first or use GlobalAveragePooling2D instead of AveragePooling2D.
Like
x = model.get_layer('avg_pool').output
x = keras.layers.Flatten()(x)
predictions = Dense(1, activation='sigmoid')(x)
Or
model = ResNet50(include_top=False, pooling='avg', weights='imagenet') # `pooling='avg'` makes the `ResNet50` include a `GlobalAveragePoiling` layer and `include_top=False` means that you don't include the imagenet's output layer
x = model.output # as I use `include_top=False`, you don't need to care the layer name, just use the model's output right away
predictions = Dense(1, activation='sigmoid')(x)
Also just as #bit01 said, change class_mode='categorical' to class_mode='binary'.
Change class_mode='categorical' to class_mode='binary' in both train_generator and validation_generator.
Additionally, delete the following lines as you have already created model.
model.layers.pop()
model = Model(input=model.input,output=model.layers[-1].output)
So, your model would be like:
model = ResNet50(include_top=True, weights='imagenet')
x = model.get_layer('avg_pool').output
x = Flatten()(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(input = model.input, output = predictions)
I am having trouble with a Keras assertion Error and would like to ask if anyone can please help:
I have ran Keras nn before with 2D convolution and never seen this error.
#-----------------BEGIN FUNCTION 1-----------------
def create_model(input_size1, num_labels, conv1_num_filters, conv1_filter_size1, conv2_num_filters, conv2_filter_size1, pool1_1, dropout1, pool2_1, dropout2, neurons1, reg_l2, neurons2, reg_l2_2):
model = Sequential()
model.add(Convolution1D(conv1_num_filters, conv1_filter_size1, init = 'glorot_uniform', border_mode='same',
input_shape=(1, input_size1),
activation = 'relu'))
model.add(MaxPooling1D(pool_length=(pool1_1),border_mode='same'))
model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
model.add(Convolution1D(conv2_num_filters, conv2_filter_size1, init = 'glorot_uniform', activation = 'relu', border_mode='same'))
model.add(MaxPooling1D(pool_length=(pool1_1),border_mode='same'))
model.add(Dropout(dropout1))
model.add(Flatten())
model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
model.add(Dense(neurons1, W_regularizer=l2(reg_l2), init = 'glorot_uniform', activation = 'relu'))
model.add(Dropout(dropout2))
model.add(BatchNormalization(epsilon=0.001, mode=0, axis=1, momentum=0.99, weights=None, beta_init='zero', gamma_init='one', gamma_regularizer=None, beta_regularizer=None))
model.add(Dense(neurons2, W_regularizer=l2(reg_l2_2), init = 'glorot_uniform', activation = 'relu'))
model.add(Dense(num_labels, init = 'glorot_uniform', activation = 'tanh'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) #0.01
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
print(model.summary())
#exit()
return model
#-----------------END FUNCTION 1-----------------
model2 = create_model(input_size1, num_labels, conv1_num_filters,
conv1_filter_size1, conv2_num_filters,
conv2_filter_size1, pool1_1, dropout1, pool2_1,
dropout2, neurons1, reg_l2, neurons2, reg_l2_2);
x_train_ex = np.expand_dims(x_train, 1)
x_test_ex = np.expand_dims(x_test, 1)
from keras.utils.np_utils import to_categorical
y_train_ex = to_categorical(y_train, len(np.unique(y_train)))
y_test_ex = to_categorical(y_test, len(np.unique(y_train)))
model2.fit(x_train_ex, y_train_ex, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(x_test_ex, y_test_ex)
)
I get an error saying:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-41-c4780c441db5> in <module>()
26
27 model2.fit(x_train_ex, y_train_ex, batch_size=batch_size, nb_epoch=nb_epoch,
---> 28 verbose=1, validation_data=(x_test_ex, y_test_ex))
29 #print(model2.score(x_train_ex, y_train))
30 #print(model2.score(x_test_ex, y_test))
.........(Lots more error messages)
AssertionError:
Thank you very much!
Problem seems to have gone away when I upgraded from Keras 1.1.1 to 1.2.0. May be a version problem.