How to add an attention mechanism in keras?

If you want to have an attention along the time dimension, then this part of your code seems correct to me: activations = LSTM(units, return_sequences=True)(embedded) # compute importance for each step attention = Dense(1, activation=’tanh’)(activations) attention = Flatten()(attention) attention = Activation(‘softmax’)(attention) attention = RepeatVector(units)(attention) attention = Permute([2, 1])(attention) sent_representation = merge([activations, attention], mode=”mul”) You’ve worked … Read more

Using pre-trained word2vec with LSTM for word generation

I’ve created a gist with a simple generator that builds on top of your initial idea: it’s an LSTM network wired to the pre-trained word2vec embeddings, trained to predict the next word in a sentence. The data is the list of abstracts from arXiv website. I’ll highlight the most important parts here. Gensim Word2Vec Your … Read more

How to display custom images in TensorBoard using Keras?

So, the following solution works well for me: import tensorflow as tf def make_image(tensor): “”” Convert an numpy representation image to Image protobuf. Copied from https://github.com/lanpa/tensorboard-pytorch/ “”” from PIL import Image height, width, channel = tensor.shape image = Image.fromarray(tensor) import io output = io.BytesIO() image.save(output, format=”PNG”) image_string = output.getvalue() output.close() return tf.Summary.Image(height=height, width=width, colorspace=channel, encoded_image_string=image_string) … Read more

How can I convert a trained Tensorflow model to Keras?

I think the callback in keras is also a solution. The ckpt file can be saved by TF with: saver = tf.train.Saver() saver.save(sess, checkpoint_name) and to load checkpoint in Keras, you need a callback class as follow: class RestoreCkptCallback(keras.callbacks.Callback): def __init__(self, pretrained_file): self.pretrained_file = pretrained_file self.sess = keras.backend.get_session() self.saver = tf.train.Saver() def on_train_begin(self, logs=None): if … Read more

ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer`

I have found a way to work around to solve the problem. For anyone who encounters the same issue, you can use the Lambda layer to wrap your tensorflow operations, this is what I did: from tensorflow.python.keras.layers import Lambda; def norm(fc2): fc2_norm = K.l2_normalize(fc2, axis = 3); illum_est = tf.reduce_sum(fc2_norm, axis = (1, 2)); illum_est … Read more

Early stopping with Keras and sklearn GridSearchCV cross-validation

[Answer after the question was edited & clarified:] Before rushing into implementation issues, it is always a good practice to take some time to think about the methodology and the task itself; arguably, intermingling early stopping with the cross validation procedure is not a good idea. Let’s make up an example to highlight the argument. … Read more

Error “Keras requires TensorFlow 2.2 or higher”

I had the same issue caused by last keras release,what i remember did(): 1-Upgrade tensorflow: pip install –user –upgrade tensorflow-gpu (there might be some missing packages, just pip install them) 2-Upgrade Tensorboard pip install –user –upgrade tensorboard (there might be some missing packages, just pip install them) 3-Downgrade Keras pip install keras==2.3.1 (latest version working … Read more