I have a simple Keras model:
model_2 = Sequential()
model_2.add(Dense(32, input_shape=(500,)))
model_2.add(Dense(4))
#answer = concatenate([response, question_encoded])
model_1 = Sequential()
model_1.add(LSTM(32, dropout_U = 0.2, dropout_W = 0.2, return_sequences=True, input_shape=(None, 2048)))
model_1.add(LSTM(16, dropout_U = 0.2, dropout_W = 0.2, return_sequences=False))
#model.add(LSTM(16, return_sequences=False))
merged = Merge([model_1, model_2])
model = Sequential()
model.add(merged)
model.add(Dense(8, activation='softmax'))
#model.build()
#print(model.summary(90))
print("Compiled")
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
The code fails with the error when calling fit():
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
Clearly, I have called compile. How could I resolve the error?
It looks like the problem is that you are creating 3 instances of the Sequential model but only compile the 3rd one (the merged).
It might be easier to use a different structure for a multi-modal network:
input_2 = Input(shape=(500,))
model_2 = Dense(32)(input_2 )
model_2 = Dense(4)(model_2)
input_1 = Input(shape=(None, 2048))
model_1 = LSTM(32, dropout_U = 0.2, dropout_W = 0.2, return_sequences=True)(input_1 )
model_1 = LSTM(16, dropout_U = 0.2, dropout_W = 0.2, return_sequences=False)(model_1)
merged = concatenate([model_2, model_1])
merged = Dense(8, activation='softmax')(merged)
model = Model(inputs=[input_2 , input_1], outputs=merged)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
Hope this helps!
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_)
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 have the following model, where keep_features=900 or so,y is one-hot encoding of classes. I am looking for the architecture below though(is that possible with keras, and what would the notation idea look like,specially the parallel part and the concatination)
model = Sequential()
model.add(Dense(keep_features, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='softmax'))
model.compile(loss=losses.categorical_crossentropy,optimizer='adam',metrics=['mae', 'acc'])
With the chapter "Multi-input and multi-output models" here you can make something like this for your desired model:
K = tf.keras
input1 = K.layers.Input(keep_features_shape)
denseA1 = K.layers.Dense(256, activation='relu')(input1)
denseB1 = K.layers.Dense(256, activation='relu')(input1)
denseC1 = K.layers.Dense(256, activation='relu')(input1)
batchA1 = K.layers.BatchNormalization()(denseA1)
batchB1 = K.layers.BatchNormalization()(denseB1)
batchC1 = K.layers.BatchNormalization()(denseC1)
denseA2 = K.layers.Dense(64, activation='relu')(batchA1)
denseB2 = K.layers.Dense(64, activation='relu')(batchB1)
denseC2 = K.layers.Dense(64, activation='relu')(batchC1)
batchA2 = K.layers.BatchNormalization()(denseA2)
batchB2 = K.layers.BatchNormalization()(denseB2)
batchC2 = K.layers.BatchNormalization()(denseC2)
denseA3 = K.layers.Dense(32, activation='softmax')(batchA2) # individual layer
denseB3 = K.layers.Dense(16, activation='softmax')(batchB2) # individual layer
denseC3 = K.layers.Dense(8, activation='softmax')(batchC2) # individual layer
concat1 = K.layers.Concatenate(axis=-1)([denseA3, denseB3, denseC3])
model = K.Model(inputs=[input1], outputs=[concat1])
model.compile(loss = K.losses.categorical_crossentropy, optimizer='adam', metrics=['mae', 'acc'])
This results in:
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)
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.