I don't know how to solve it.
def create_model(time_steps, num_layer, num_filters, kernel_size, strides, dropout_rate, activation):
model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=(time_steps, 1)))
for i in range(num_layer):
filters = int(num_filters / (i+1))
model.add(
tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, padding="same", strides=strides, activation=activation
)
)
if i < (num_layer - 1):
model.add(tf.keras.layers.Dropout(rate=dropout_rate))
for i in reversed(range(num_layer)):
filters = int(num_filters / (i+1))
model.add(
tf.keras.layers.Conv1DTranspose(
filters=filters, kernel_size=kernel_size, padding="same", strides=strides, activation=activation
)
)
if i != 0:
model.add(tf.keras.layers.Dropout(rate=dropout_rate))
model.add(
tf.keras.layers.Conv1DTranspose(
filters=1, kernel_size=kernel_size, padding="same"
)
)
return model
def objective(trial):
num_layer = trial.suggest_int("num_layer", 1, 3)
num_filters = int(trial.suggest_categorical("num_filters", [16, 32, 64]))
kernel_size = trial.suggest_int("kernel_size", 1, 5, 2)
strides = trial.suggest_int("strides", 2, 4, 2)
dropout_rate = trial.suggest_uniform('dropout_rate', 0.0, 0.5)
activation = trial.suggest_categorical("activation", ["relu", "sigmoid", "tanh"])
optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam"])
model = create_model(TIME_STEPS, num_layer, num_filters, kernel_size, strides, dropout_rate, activation)
model.compile(
optimizer=optimizer,
loss="mse"
)
model.summary()
history = model.fit(
x_train,
x_train,
epochs=50,
batch_size=128,
validation_split=0.1,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
],
)
return history.history["val_loss"][-1]
study = optuna.create_study()
study.optimize(objective, n_trials=50)
Error Code
study.optimize(objective, n_trials=50)
Error Statement
ValueError: Dimensions must be equal, but are 32 and 20 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](mean_squared_error/remove_squeezable_dimensions/Squeeze, IteratorGetNext:1)' with input shapes: [?,32], [?,20].
Related
I want to predict a time series using cnn-lstm model.This is my model:
def generate_model():
model = keras.models.Sequential([
Conv1D(64, 3, padding='causal', activation='relu', input_shape=(24, 20)),
BatchNormalization(),
Conv1D(64, 3, padding='causal', activation='relu'),
BatchNormalization(),
Conv1D(32, 3, padding='causal', activation='relu'),
MaxPool1D(3),
LSTM(100, dropout=0.2, return_sequences=True),
LSTM(50, dropout=0.3),
Dense(1, activation='relu')
])
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='mean_squared_error',
metrics=[tf.keras.metrics.MeanAbsoluteError(), tf.keras.metrics.RootMeanSquaredError(), RSquare()])
return model
Then I use this line of code to train my model:
history1 = model1.fit(X1_train, y1_train, epochs=200, batch_size=32, validation_data=(X1_test, y1_test), verbose=2, callbacks=callbacks)
But values of loss and metrics stays the same and does not change. This is how they look.
These are my callbacks, just in case:
from keras.callbacks import LearningRateScheduler
def decay_schedule(epoch, lr):
lr = lr - 0.0001
return lr
lr_scheduler = LearningRateScheduler(decay_schedule)
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='max', min_delta=1e-3, patience=50)
callbacks=[lr_scheduler, callback]
Thank you in advance.
# model part
``` This is part of the unet model ```
def unet(input_size):
inputs = keras.Input(input_size) # here is the inputs
conv1 = Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(inputs)
conv1 = Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
# train part
batch_size = 8
epoch = 100
``` This is the DataGenerator ```
class DataGenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
return batch_x, batch_y
train_gen = DataGenerator(x_train, y_train, batch_size) # This is the train_gen
test_gen = DataGenerator(x_test, y_test, batch_size) # This is the test_gen
input_size = keras.Input([512,512,4]) # The input of Unet,error derived here
model = unet(input_size)
weight = "./UNet.h5"
model_checkpoint = ModelCheckpoint(weight,
monitor='val_loss',
verbose=1,
save_best_only=True,
mode='min',
save_weights_only = False)
history=model.fit(train_gen,
epochs=epoch,
batch_size=batch_size,
shuffle=True,
validation_data=test_gen,
callbacks= [model_checkpoint])
error part
I got an error of 'TypeError: Cannot iterate over a Tensor with unknown first dimension'. The error may derive from the diffenrence of the DataGenerator and the input shape of keras. If you have some ideas, could you please help me solve this error. Thanks!
i want to establish the VAE-CNN but i dont know why show this error.
train_datagen = ImageDataGenerator(rescale=1. / 255)
validation_datagen = ImageDataGenerator(rescale=1. / 255)
train_gen = train_datagen.flow_from_directory(
'./train for dataset/',
target_size=(80, 24),
color_mode='grayscale',
batch_size=32,
class_mode='input',
shuffle=True,
seed = 42
)
validation_gen = validation_datagen.flow_from_directory(
'./test/',
target_size=(80, 24),
color_mode='grayscale',
batch_size=32,
class_mode='input',
shuffle=False,
seed = 42
)
#VAE-CNN
filter1_V=64
filter2_V=88
latent_dim_V=20
original_inputs = keras.Input(shape=(80,24,1))
init = tf.keras.initializers.VarianceScaling(scale=0.3, mode='fan_in',distribution='uniform')
layer1_v = layers.Conv2D(filter1_V, kernel_size=3, activation = 'relu', kernel_initializer=init, padding='same', strides = 2)(original_inputs)
layer1_v = layers.MaxPool2D(pool_size=(2,2))(layer1_v)
# strides is 2 in default, which equals to pool_size
layer2_v = layers.Conv2D(filter2_V, kernel_size=3, activation='relu', kernel_initializer=init, padding='same', strides = 2)(layer1_v)
layer2_v = layers.MaxPool2D(pool_size=(2,2))(layer2_v)
layer3_v = layers.Flatten()(layer2_v)
# start to code the core part of mean and variance
#get mean
layer_mean = layers.Dense(latent_dim_V)(layer3_v)
# get log variance, it can get the value from negative to positive, if only use variance, the value is only positive
log_var = layers.Dense(latent_dim_V)(layer3_v)
# dur to the sample, in order to get back propogation, add one parameter which its distribution is normal(0,1)
def sampling(args):
layer_mean,log_var=args
eps = K.random_normal(shape=(K.shape(log_var)[0],latent_dim_V),mean=0.,stddev=1.0)
# reparameterize
# the standard varinace is what we want
std = K.exp(log_var)**0.5
return layer_mean + std * eps
z = layers.Lambda(sampling, output_shape=(latent_dim_V,))([layer_mean, log_var])
#decoder part
dec1_v = layers.Dense(layer3_v.shape[1], activation='relu')(z)
dec2_v = layers.Reshape((layer2_v.shape[1],layer2_v.shape[2],layer2_v.shape[3]))(dec1_v)
dec3_v = layers.Conv2DTranspose(filter2_V, kernel_size=3, output_padding=(1,2), activation = 'relu',kernel_initializer=init, padding = 'same', strides=(2,3))(dec2_v)
dec4_v = layers.Conv2DTranspose(filter1_V, kernel_size=3, activation = 'relu', kernel_initializer=init, padding = 'same', strides=2)(dec3_v)
dec5_v = layers.Conv2DTranspose(filter1_V, kernel_size=3, activation = "relu", kernel_initializer=init, padding = 'same', strides=2)(dec4_v)
dec_v_outputs = layers.Conv2DTranspose(1, kernel_size=3, activation = "relu", kernel_initializer=init, padding = 'same', strides=2)(dec5_v)
encoder_v = keras.Model(inputs=original_inputs, outputs=[z,layer_mean,log_var], name='encoder')
decoder_v = keras.Model(inputs=z, outputs=dec_v_outputs, name='decoder')
outputs = decoder_v(encoder_v(original_inputs)[0])
vae_model = keras.Model(inputs=original_inputs, outputs=outputs, name='vae_model')
vae_model.summary()
kl_loss = -0.5 * K.sum(log_var + 1 - layer_mean**2 - K.exp(log_var), axis=-1)
kl_loss = K.mean(kl_loss)/1920.
lr=1e-3
optimizer = keras.optimizers.Adam(learning_rate=lr)
vae_model.add_loss(kl_loss)
vae_model.compile(optimizer, loss="binary_crossentropy")
history=vae_model.fit(train_gen,train_gen, epochs=4, batch_size=32, validation_data=(validation_gen,validation_gen))
i want to get a VAE-CNN
and there is a error:ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 80, 24, 1), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []
why is it and how to solve?
I have a class called RL_Brain:
class RL_Brain():
def __init__(self, n_features, n_action, memory_size=10, batch_size=32, gamma=0.9, fi_size=10):
self.n_features = n_features
self.n_actions = n_action
self.encoder = keras.Sequential([
Input((self.n_features,)),
Dense(16, activation='relu', kernel_initializer='glorot_normal', name='encoder_1'),
Dense(16, activation='relu', kernel_initializer='glorot_normal', name='encoder_2'),
Dense(16, activation='relu', kernel_initializer='glorot_normal', name='encoder_3'),
Dense(self.fi_size, activation='softmax', name='fi'),
])
self.decoder = keras.Sequential([
Input((self.fi_size,)),
Dense(16, activation='relu', name='decoder_1', trainable=True),
Dense(16, activation='relu', name='decoder_2', trainable=True),
Dense(16, activation='relu', name='decoder_3', trainable=True),
Dense(self.n_features, activation=None, name='decoder_output', trainable=True)
])
def learn(self, state, r, a, state_):
encoded = tf.one_hot(tf.argmax(self.encoder(state), axis=1), depth=self.fi_size)
encoded_ = tf.one_hot(tf.argmax(self.encoder(state_), axis=1), depth=self.fi_size)
decoded_state = self.decoder(encoded).numpy()
with tf.GradientTape() as tape:
loss1 = mean_squared_error(state, decoded_state)
grads = tape.gradient(loss1, self.decoder.trainable_variables)
self.opt.apply_gradients(zip(grads, self.decoder.trainable_variables))
When I run the learn function, I get the following error:
File "/Users/wangheng/app/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/optimizer_v2/utils.py", line 78, in filter_empty_gradients raise ValueError("No gradients provided for any variable: %s." % ...
ValueError: No gradients provided for any variable: ['decoder_1/kernel:0', 'decoder_1/bias:0', 'decoder_2/kernel:0', 'decoder_2/bias:0', 'decoder_3/kernel:0', 'decoder_3/bias:0', 'decoder_output/kernel:0', 'decoder_output/bias:0'].
the following line is causing that error
decoded_state = self.decoder(encoded).numpy()
Once you do that, there is no path from your loss function to your trainable variables so no gradient can be calculated.
I am trying to pass to my triplet network 3 images using my data generator. I am loading the different pairs and stacking them into batches. I don't know how can I return it back as 3 different arrays. I tried appending into a list, but that also didn't work. How can I use a data generator to return them back?
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, batch_size=16, dim=(244,244,3), n_channels=3, shuffle=True):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.list_IDs = list_IDs
self.n_channels = n_channels
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
X,Z, y = self.__data_generation(list_IDs_temp)
return X, Z, y
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
# V = np.stack((X, Z), axis=-1)
# F = np.stack((V, y), axis=-1)
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
X = np.empty((self.batch_size, *self.dim))
Z = np.empty((self.batch_size, *self.dim))
y = np.empty((self.batch_size, *self.dim))
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
image = plt.imread(os.path.join(IMAGE_DIR, ID[0])).astype(np.float32)
image = imresize(image, (IM_SIZE, IM_SIZE))
image1 = plt.imread(os.path.join(IMAGE_DIR, ID[1])).astype(np.float32)
image1 = imresize(image1, (IM_SIZE, IM_SIZE))
image2 = plt.imread(os.path.join(IMAGE_DIR, ID[2])).astype(np.float32)
image2 = imresize(image2, (IM_SIZE, IM_SIZE))
X[i,] = image
Z[i,] = image1
y[i,] = image2
return X, Z, y
input_a = Input(shape=(224,224,3))
input_b = Input(shape=(224,224,3))
input_c = Input(shape=(224,224,3))
conv = Sequential([
Conv2D(24, (7, 7), strides=(1,1), input_shape=(224,224,3)),
BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9),
MaxPooling2D((3,3), strides=(2, 2)),
Activation('relu'),
Dropout(0.2),
ZeroPadding2D((2, 2)),
Conv2D(64, (5, 5), padding='same', strides=(1,1), kernel_initializer='glorot_uniform'),
BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9),
MaxPooling2D((3,3), strides=(2, 2)),
Activation('relu'),
Dropout(0.2),
ZeroPadding2D((1, 1)),
Conv2D(96, (3,3), padding='same', strides=(1,1),kernel_initializer='glorot_uniform'),
BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9),
MaxPool2D(pool_size=(2,2), strides=(2,2)),
Activation('relu'),
Dropout(0.2),
ZeroPadding2D((1, 1)),
Conv2D(96, (3,3), padding='same', strides=(1,1),kernel_initializer='glorot_uniform'),
BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9),
Activation('relu'),
MaxPool2D(pool_size=(2,2), strides=(2,2)),
Dropout(0.2),
ZeroPadding2D((1, 1)),
Conv2D(64, (5, 5), padding='same', strides=(1,1), kernel_initializer='glorot_uniform'),
BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9),
Activation('relu', name="activation_1_5"),
MaxPooling2D((3,3), strides=(2, 2)),
Dropout(0.2),
Dense(256, activation='relu'),
Flatten()
])
net1 = conv(input_a)
net2 = conv(input_b)
net3 = conv(input_c)
d1 = subtract(net1, net2)
d2 = subtract(net1, net3)
n1 = norm(d1)
n2 = norm(d2)
out = Activation('sigmoid')(subtract(n2, n1))
model = Model(inputs=[input_a, input_b, input_c], outputs=out)
params = {'dim': (224,224,3),
'batch_size': BATCH_SIZE,
'n_channels': 3,
'shuffle': False}
paramsv = {'dim': (224,224,3),
'batch_size': BATCH_SIZE,
'n_channels': 3,
'shuffle': True}
training_generator = DataGenerator(partition_image['train'], **params)
validation_generator = DataGenerator(partition_image['validation'], **paramsv)
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=1e-6)
filepath = 'weights/weights.{epoch:02d}-{val_loss:.2f}.hdf5'
cpkt1 = ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=True, mode='auto', period=1)
cpkt2 = TensorBoard(log_dir='tensorboard/', histogram_freq=0, write_graph=True, write_images=True)
cpkt3 = EarlyStopping(monitor='val_loss', min_delta=0, patience=4, verbose=0, mode='auto')
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=['accuracy'])
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch=int(np.ceil(len(partition_image['train']) / BATCH_SIZE) ),
validation_steps=int(np.ceil(len(partition_image['validation']) / BATCH_SIZE) ),
epochs= EPOCHS,
shuffle = True,
verbose=1, callbacks=[cpkt1,cpkt2,cpkt3])
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays: [array([[[[180., 189., 194.],
[...
There might be other solutions, but what I do is to name my input layers and then use as inputs an dictionary with the same names.
So in your model you should name your inputs:
input_a = Input(shape=(224,224,3), name = "input_a")
input_b = Input(shape=(224,224,3), name = "input_b")
input_c = Input(shape=(224,224,3), name = "input_b")
Then, in the generator must return something like this:
inputs ={"input_a":X,
"input_b":Z,
"input_c":y}
outputs ={"output":o}
return inputs,outputs
You can find and example with a generator with multiple inputs in this keras example