Keras: Difference between Kernel and Activity regularizers

The activity regularizer works as a function of the output of the net, and is mostly used to regularize hidden units, while weight_regularizer, as the name says, works on the weights (e.g. making them decay). Basically you can express the regularization loss as a function of the output (activity_regularizer) or of the weights (weight_regularizer). The … Read more

How to get reproducible results in keras

You can find the answer at the Keras docs: https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development. In short, to be absolutely sure that you will get reproducible results with your python script on one computer’s/laptop’s CPU then you will have to do the following: Set the PYTHONHASHSEED environment variable at a fixed value Set the python built-in pseudo-random generator at a … Read more

Why plt.imshow() doesn’t display the image?

The solution was as simple as adding plt.show() at the end of the code snippet: import numpy as np np.random.seed(123) from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.utils import np_utils from keras.datasets import mnist (X_train,y_train),(X_test,y_test) = mnist.load_data() print X_train.shape from matplotlib import pyplot as plt … Read more

What is an Embedding in Keras?

As far as I know, the Embedding layer is a simple matrix multiplication that transforms words into their corresponding word embeddings. The weights of the Embedding layer are of the shape (vocabulary_size, embedding_dimension). For each training sample, its input are integers, which represent certain words. The integers are in the range of the vocabulary size. … Read more

Does model.compile() initialize all the weights and biases in Keras (tensorflow backend)?

When to use? If you’re using compile, surely it must be after load_model(). After all, you need a model to compile. (PS: load_model automatically compiles the model with the optimizer that was saved along with the model) What does compile do? Compile defines the loss function, the optimizer and the metrics. That’s all. It has … Read more

Keras, how do I predict after I trained a model?

model.predict() expects the first parameter to be a numpy array. You supply a list, which does not have the shape attribute a numpy array has. Otherwise your code looks fine, except that you are doing nothing with the prediction. Make sure you store it in a variable, for example like this: prediction = model.predict(np.array(tk.texts_to_sequences(text))) print(prediction)

What is the role of TimeDistributed layer in Keras?

In keras – while building a sequential model – usually the second dimension (one after sample dimension) – is related to a time dimension. This means that if for example, your data is 5-dim with (sample, time, width, length, channel) you could apply a convolutional layer using TimeDistributed (which is applicable to 4-dim with (sample, … Read more

keras: how to save the training history attribute of the history object

What I use is the following: with open(‘/trainHistoryDict’, ‘wb’) as file_pi: pickle.dump(history.history, file_pi) In this way I save the history as a dictionary in case I want to plot the loss or accuracy later on. Later, when you want to load the history again, you can use: with open(‘/trainHistoryDict’, “rb”) as file_pi: history = pickle.load(file_pi) … Read more

Which parameters should be used for early stopping?

Early stopping is basically stopping the training once your loss starts to increase (or in other words validation accuracy starts to decrease). According to documents it is used as follows; keras.callbacks.EarlyStopping(monitor=”val_loss”, min_delta=0, patience=0, verbose=0, mode=”auto”) Values depends on your implementation (problem, batch size etc…) but generally to prevent overfitting I would use; Monitor the validation … Read more

What does Keras Tokenizer method exactly do?

From the source code: fit_on_texts Updates internal vocabulary based on a list of texts. This method creates the vocabulary index based on word frequency. So if you give it something like, “The cat sat on the mat.” It will create a dictionary s.t. word_index[“the”] = 1; word_index[“cat”] = 2 it is word -> index dictionary … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)