I need to know how this code works. It's taking Embedding then it sends it into this model. model1 is CNN and moel2 is Time distributed layer. Why wrapping is done in this code, i didn't find article on this.
model1 = Sequential()
model1.add(Embedding(nb_words + 1,
embedding_dim,
weights = [word_embedding_matrix],
input_length = max_sentence_len,
trainable = False))
model1.add(Convolution1D(filters = nb_filter,
kernel_size = filter_length,
padding = 'same'))
model1.add(BatchNormalization())
model1.add(Activation('relu'))
model1.add(Dropout(dropout))
model1.add(Convolution1D(filters = nb_filter,
kernel_size = filter_length,
padding = 'same'))
model1.add(BatchNormalization())
model1.add(Activation('relu'))
model1.add(Dropout(dropout))
model1.add(Flatten())
model2 = Sequential()
model2.add(Embedding(nb_words + 1,
embedding_dim,
weights = [word_embedding_matrix],
input_length = max_sentence_len,
trainable = False))
model2.add(Convolution1D(filters = nb_filter,
kernel_size = filter_length,
padding = 'same'))
model2.add(BatchNormalization())
model2.add(Activation('relu'))
model2.add(Dropout(dropout))
model2.add(Convolution1D(filters = nb_filter,
kernel_size = filter_length,
padding = 'same'))
model2.add(BatchNormalization())
model2.add(Activation('relu'))
model2.add(Dropout(dropout))
model2.add(Flatten())
then it merges and getting the output. I don't understand the computation behind this.
Related
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 defined a image, img_shape , its shape is (28,28,1) before this model,
def make_discriminator(img_shape):
return keras.Sequential([
keras.layers.Dropout(0.3),
keras.layers.Conv2D(32, 5, strides = 2,
padding='same',
input_shape = img_shape,
use_bias = False),
keras.layers.BatchNormalization(),
keras.layers.LeakyReLU(),
keras.layers.Conv2D(64, 5, strides = 2,
padding = 'same',
use_bias = False),
keras.layers.BatchNormalization(),
keras.layers.LeakyReLU(),
keras.layers.Flatten(),
keras.layers.Dense(1)
], "Discriminator")
Then I tried to directly use it as input and print the structure of this model,
D = make_discriminator(img_shape = img_shape)
print(D.summary())
However, it shows
This model has not yet been built. Build the model first by calling
build() or by calling the model on a batch of data.
But when I tried to add build() before summary,
D = make_discriminator(img_shape = img_shape)
it shows
build() got an unexpected keyword argument 'img_shape'
I dont know how to solve this problem...and the process of creating image is below,
import keras
import tensorflow as tf
import tensorflow_datasets as tfds
fmist = tfds.load('fashion_mnist')
def process(data):
img = tf.cast(data['image'], tf.float32)
lab = data['label']
img = (img / 255.0 - 0.5) * 2.0
return img
BATCH_SIZE = 256
train = fmist['train'].shuffle(10000).batch(BATCH_SIZE).\
map(process).prefetch(tf.data.experimental.AUTOTUNE)
img_shape = tf.data.experimental.get_structure(train).shape[1:]
print("image shape:", img_shape)
Try discriminator.build(input_shape=(1, 28, 28, 1)):
def make_discriminator(img_shape):
return tf.keras.Sequential([
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(32, 5, strides = 2,
padding='same',
input_shape = img_shape,
use_bias = False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2D(64, 5, strides = 2,
padding = 'same',
use_bias = False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1)
], "Discriminator")
discriminator = make_discriminator((28, 28, 1))
discriminator.build(input_shape=(1, 28, 28, 1))
print(discriminator.summary())
Or set the input_shape in the first layer of your model. Then, the remaining output shapes will be inferred and you do not have to call model.build():
def make_discriminator(img_shape):
return tf.keras.Sequential([
tf.keras.layers.Dropout(0.3, input_shape = img_shape),
tf.keras.layers.Conv2D(32, 5, strides = 2,
padding='same',
use_bias = False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2D(64, 5, strides = 2,
padding = 'same',
use_bias = False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1)
], "Discriminator")
discriminator = make_discriminator((28, 28, 1))
print(discriminator.summary())
I have created a model where I have multiple inputs - from table data and from .mat files.
The shape of the data extracted from the mat files is (4701, 1, 52, 53) the first number being the count of the records.
The shape of the table data is (4701, 1404)
This is my NN architecture:
inputs1 = Input(shape = (1, 52, 53))
model1 = Conv2D(filters = 32, kernel_size=5, padding = 'same', activation='relu')(inputs1)
model1 = BatchNormalization()(model1)
model1 = MaxPool2D(pool_size = (2,2), data_format='channels_first')(model1)
model1 = Conv2D(filters = 32, kernel_size=3, padding = 'same', activation='relu')(model1)
model1 = BatchNormalization()(model1)
model1 = MaxPool2D(pool_size = (2,2), data_format='channels_first')(model1)
model1 = Conv2D(filters = 64, kernel_size=3, padding = 'same', activation='relu')(model1)
model1 = BatchNormalization()(model1)
model1 = MaxPool2D(pool_size = (2,2), data_format='channels_first')(model1)
model1 = Conv2D(filters = 128, kernel_size=3, padding = 'same', activation='relu')(model1)
model1 = BatchNormalization()(model1)
model1 = MaxPool2D(pool_size = (2,2), data_format='channels_first')(model1)
flatten = Flatten()(model1)
inputs2 = Input(shape = (4701,))
model2 = Dense(4701, activation='relu')(inputs2)
model2 = Dropout(0.25)(model2)
model2 = Dense(538, activation = 'relu')(model2)
model2 = Dropout(0.25)(model2)
model2 = Dense(538, activation = 'relu')(model2)
model2 = Dropout(0.25)(model2)
model2 = Dense(264, activation = 'relu')(model2)
concat = Concatenate()([flatten, model2])
outputs = Dense(num_regr_outputs)(concat)
When I try to fit the data
combined_model.fit(
x = [trainImagesX, trainDataX],
y = trainDataY,
validation_split = ([valImagesX, valDataX], valDataY),
epochs = EPOCHS,
batch_size = BATCH_SIZE)
I get this error: TypeError: '<' not supported between instances of 'float' and 'tuple'.
I would appreciate if someone can tell me where is my mistake.
I have trained a model (which is built with tf.keras) with pure tensorflow. I have saved the model using model.save(model_150.h5) (as it is a keras model).
Here is my model:
conv_1_1 = Conv2D(filters = 64, kernel_size = 3, activation='relu', padding='same')(input_img)
conv_1_1_bn = BatchNormalization()(conv_1_1)
conv_1_1_do = Dropout(droprate)(conv_1_1_bn)
pool_1 = MaxPooling2D(pool_size= 2, strides = 2)(conv_1_1_do)
conv_4_1 = SeparableConv2D(filters = 512, kernel_size = 3, activation='relu', padding='same')(pool_1)
conv_4_1_bn = BatchNormalization()(conv_4_1)
conv_4_1_do = Dropout(droprate)(conv_4_1_bn)
pool_4 = MaxPooling2D(pool_size= 2, strides = 2)(conv_4_1_do)
conv_5_1 = SeparableConv2D(filters = 1024, kernel_size = 3, activation='relu', padding='same')(pool_4)
conv_5_1_bn = BatchNormalization()(conv_5_1)
conv_5_1_do = Dropout(droprate)(conv_5_1_bn)
upconv_1 = upconv_concat(conv_5_1_do, conv_4_1_do, n_filter=512, pool_size=2, stride=2)
conv_6_1 = SeparableConv2D(filters = 512, kernel_size = 3, activation='relu', padding='same')(upconv_1)
conv_6_1_bn = BatchNormalization()(conv_6_1)
conv_6_1_do = Dropout(droprate)(conv_6_1_bn)
upconv_2 = upconv_concat(conv_6_1_do, conv_1_1_do, n_filter=64, pool_size=2, stride=2)
conv_9_1 = SeparableConv2D(filters = 64, kernel_size = 3, activation='relu', padding='same')(upconv_2)
conv_9_1_bn = BatchNormalization()(conv_9_1)
conv_9_1_do = Dropout(droprate)(conv_9_1_bn)
ae_output = Conv2D(num_classes, kernel_size=1, strides = (1,1), activation="softmax")(conv_9_1_do)
I initially defined the model like this:
e_model = Model(input_img, ae_output)
Now I needed some custom training. So I trained the model with pure tensorflow like this:
Here is my loss function
def cut_loss(original_image):
ypred = e_model(original_image)
...
...
#do some computations and calculate some custom loss
...
return loss
Here is my optimizer
#optimizer
opt = tf.train.AdamOptimizer(learning_rate=e_lr).minimize(cut_loss(original_image))
Here is my training loop
with tf.Session() as sess:
sess.run(tf.global_variable_initializer())
for epoch in range(num_epochs):
print("epoch:", epoch)
count = 0
batch_start_index = 0
while (count != num_batches):
X_train_batch = X_train[batch_start_index : batch_start_index+batch_size] #send a batch of input images of size (batchsize, 224, 224, 1)
_, train_loss = sess.run([opt,loss], feed_dict={original_image: X_train_batch})
batch_start_index+=batch_size
count+=1
print("Train loss after ", str(epoch), "is", str(train_loss))
After the training, I saved the model and I restarted my jupyter kernel. when I tried loading the model from the h5 file like this
from tensorflow.keras.models import load_model
e_model = load_model('model_150.h5')
I'm getting the following error:
UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually
How can I get rid of this error?
Is it just bad to train the model using tensorflow and save the model using model.save() function from tf.keras?
Please let me know if any additional details are required. Thanks!
I want to use Subclassing/inheritance of the keras Model class. When I want to compile my model it isn't.
I started with keras recently but used a lot of pytorch before.
I currently run tensorflow and keras on version 1.10 and 2.16 respectively and really dont know why I cant compile the model. I tried updating tf to version 1.13 but nothing changed.
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from keras.layers import Input,Conv2D,MaxPooling2D,UpSampling2D,BatchNormalization
from keras import Model, layers
print(tf.VERSION)
print(tf.keras.__version__)
batch_size = 128
epochs = 50
inChannel = 1
img_width, img_height = 64, 64
input_shape = (img_width, img_height, 1)
class AE_64x64(Model):
def __init__(self):
super(AE_64x64, self).__init__()
'''
data_format: channels last
'''
self.conv1 = Conv2D(filters=30, kernel_size=(7,7), activation='relu', padding='same', strides=2)(Input(shape=input_shape))
self.conv2 = Conv2D(filters=40, kernel_size=(5,5), activation='relu', padding='same', strides=2)
self.batchnorm = BatchNormalization(axis=2)
self.max_pool = MaxPooling2D((3,3),padding='same')
self.conv3 = Conv2D(filters=50, kernel_size=(3,3), activation='relu', padding='same', strides=2)
self.conv4 = Conv2D(filters=60, kernel_size=(3,3), activation='relu')
self.b1 = Conv2D(filters=80, kernel_size=(3,3), activation='relu')
self.b2 = Conv2D(filters=99, kernel_size=(3,3), activation='relu')
self.conv6 = Conv2D(filters=50, kernel_size=(3,3), activation='relu')
self.conv7 = Conv2D(filters=40, kernel_size=(3,3), activation='relu')
self.conv8 = Conv2D(filters=30, kernel_size=(3,3), activation='relu')
self.conv9 = Conv2D(filters=1, kernel_size=(3,3), activation='relu')
def call(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.batchnorm(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.max_pool(x)
x = self.batchnorm(x)
x = self.b1(x)
x = self.b2(x)
x = self.batchnorm(x)
x = self.conv5(x)
x = self.conv6(x)
x = self.batchnorm(x)
x = self.conv7(x)
x = self.conv8(x)
x = self.batchnorm(x)
x = self.conv9(x)
return x
AE_Model = AE_64x64()
AE_Model.compile(loss='mean_squared_error',optimizer=tf.train.AdamOptimizer(),metrics= ['mean_squared_error'])
AE_Model.summary()
I expected a summary output but instead I received this error message:
RuntimeError: You must compile your model before using it.
Is there a logical mistake in the code or a Hardware/Version problem?
you at least have to build your model. Or you fit your model with data.
anyway, when I run your code without data I get this result
AE_Model.compile(loss='mean_squared_error',optimizer=tf.train.AdamOptimizer(),metrics= ['mean_squared_error'])
AE_Model.build(input_shape)
AE_Model.summary()
1.13.1
2.2.4-tf
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================