Tensorboard displaying only 8 nodes for Embedding layer - python

I tried to visualize the graph of a network with an input Embedding layer with 100 as output
dims using gloVe.100d. However, tensorboard states that only 8 nodes is present in the Embedding layer.
def lstm_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Embedding(input_dim=10000, output_dim=100,
embeddings_initializer=tf.keras.initializers.Constant(value=embeddings_matrix)),
tf.keras.layers.LSTM(units=16, return_sequences=True),
tf.keras.layers.LSTM(units=8, return_sequences=True),
tf.keras.layers.LSTM(units=1),
tf.keras.layers.Dense(units=1, activation='sigmoid')])
return model

Related

convolute pre-trained network over a bigger image with keras/tensorflow

I want to define a tensorflow convolutional model by including a pretrained model inside it.
I have a pretreained sequential model defined as:
kernel_size = 11
def my_base_model():
model = keras.Sequential([
layers.Flatten(input_shape=[kernel_size, kernel_size])
layers.Dense(32, activation='relu', name="layer1"),
layers.Dense(1, name="layer2", activation="sigmoid")
])
# ...
return model
model = my_base_model()
model.fit(...)
Now, I want to use this network as a filter for slide arbitrary-size images:
I order to make it easier/streamline, I think in building another CNN network. For example, supposing the input image shape is (640, 640). In order to achieve a (640 - 11 + 1, 640 - 11 + 1)` output image:
def my_cnn_model():
model = keras.Sequential([
layers.Conv2D(1, (kernel_size, kernel_size), activation='linear', input_shape=(640, 640, 1)),
# <== including base model here
])
return model
Obviously, I cannot just append my_base_model there because I want to apply the model for each 11x11 square.
Is it possible? If not, is there another way to implement it instead of directly sliding the 640x640 with 2 nested for loops?

How to create customize LSTM Layer?

I have a model like this:
model = Sequential()
model.add(Embedding(100, 5, input_length=X.shape[1]))
model.add(LSTM(100))
model.add(Dense(2, activation='softmax'))
print(model.summary())
I was surfing on the internet for enhance the layer like this on below:
Input Layer - 7 feature
Recurrent Layer - 8 hidden unit
Fully Connected Layer - 4 neuron
Output Layer - 2 neuron
Here's my code I've been tried
model = tf.keras.Sequential([
tf.keras.layers.LSTM(feature, input_shape = input_shape),
tf.keras.layers.LSTM(8),
tf.keras.layers.Dense(4, activation=tf.nn.relu)
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])
But I didn't find anything - how to enhance layer like this?
Thanks in advance

What is the correct method to construct a many-to-many LSTM model using Keras in Python?

I am trying to make a 3 sequence many-to-many LSTM model, but I am confused about it's implementation in Keras. I searched on internet for examples of many-to-many models, but each website gives different method. That has confused me even more. What is the correct method of those? I want a model like this:
Some of the various methods I found were
Using encoder, decoder
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
model = Sequential()
# encoder layer
model.add(LSTM(100, activation='relu', input_shape=(3, 1)))
# repeat vector
model.add(RepeatVector(3))
# decoder layer
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
Another with encoder, decoder
from keras.models import Model
from keras.layers import Input, LSTM, Dense
encoder_inputs = Input(shape=(None, 1))
encoder = LSTM(100, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(None, 1))
decoder_lstm = LSTM(100, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model = Sequential()
model.add(LSTM(100,input_shape=(3,1),return_sequences=True))
model.add(TimeDistributed(Dense(2)))
model.compile(optimizer='adam', loss='mse')
model = Sequential()
model.add(LSTM(100,input_shape=(3,1),return_sequences=True))
model.compile(optimizer='adam', loss='mse')
Which one of these is the correct method? which one will give the model like the one I want?
You have to mention your problem statement first.
1 and 2 are best for neural machine translation problems. While 2 is superior because it is considering return states in LSTM layer. 3 is also a good architecture where logic from input to output is simple. 4 is a very basic architecture becuase nth output in the output array has knowledge about [0 to n-1th input, not later ones] also no fully connected (Dense) layer so even moderate logic cannot be learned here.

Doubts about TensorFlow Keras

I was reading this tutorial when a doubt appeared. In the tutorial de dataset has 10 attributes (after the one-hot conversion), but when the model was created, the input layer has more neurons (64) than inputs (10).
Here's the code:
def build_model():
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
return model
I thought that the number of neurons should be equals to the number of entries. Can anyone explain this?
Thanks for your attention
The 64 in your Dense layers specifies the number of neurons of the first and second hidden layers. The input_shape parameter specifies how keras will automatically handle the number of neurons in the input layer, so 10 in your case.

Pre-training Keras Xception and InceptionV3 models

I'm trying to do a simple binary classification problem using Keras and its pre-built ImageNet CNN architecture.
For VGG16, I took the following approach,
vgg16_model = keras.application.vgg16.VGG16()
'''Rebuild the vgg16 using an empty sequential model'''
model = Sequential()
for layer in vgg16_model.layers:
model.add(layer)
'''Since the problem is binary, I got rid of the output layer and added a more appropriate output layer.'''
model.pop()
'''Freeze other pre-trained weights'''
for layer in model.layers:
layer.trainable = False
'''Add the modified final layer'''
model.add(Dense(2, activation = 'softmax'))
And this worked marvelously with higher accuracy than my custom built CNN. But it took a while to train and I wanted to take a similar approach using Xception and InceptionV3 since they were lighter models with higher accuracy.
xception_model = keras.applicaitons.xception.Xception()
model = Sequential()
for layer in xception_model.layers:
model_xception.add(layer)
When I run the above code, I get the following error:
ValueError: Input 0 is incompatible with layer conv2d_193: expected axis -1 of input shape to have value 64 but got shape (None, None, None, 128)
Basically, I would like to do the same thing as I did with VGG16 model; keep the other pretrained weights as they are and simply modify the output layer to a binary classification output instead of an output layer with 1000 outcomes. I can see that unlike VGG16, which has relatively straightforward convolution layer structure, Xception and InceptionV3 have some funky nodes that I'm not 100% familiar with and I'm assuming those are causing issues.
Your code fails because InceptionV3 and Xception are not Sequential models (i.e., they contain "branches"). So you can't just add the layers into a Sequential container.
Now since the top layers of both InceptionV3 and Xception consist of a GlobalAveragePooling2D layer and the final Dense(1000) layer,
if include_top:
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dense(classes, activation='softmax', name='predictions')(x)
if you want to remove the final dense layer, you can just set include_top=False plus pooling='avg' when creating these models.
base_model = InceptionV3(include_top=False, pooling='avg')
for layer in base_model.layers:
layer.trainable = False
output = Dense(2, activation='softmax')(base_model.output)
model = Model(base_model.input, output)

Categories

Resources