Keras backendsKeras is a modellevel library, offers highlevel building blocks that are useful to develop deep learning models. Instead of supporting lowlevel operations such as tensor products, convolutions, etc. itself, it depends upon the backend engine that is well specialized and optimized tensor manipulation library. It doesn't pick just one library of a tensor to implement Keras tied to that particular library. It handles the situation in a modular way by seamlessly plugging many distinct backend engines to Keras. Following are the three available backend implementations, which are as follows;
Switching from one backend to anotherYou will probably find the Keras configuration file at: $HOME/.keras/keras.json In case you face a problem finding it there, then you may create one! Note: Especially for the Windows user, you have to replace $HOME with %USERPROFILE%.Following is the default configuration; Here, you just have to change the backend field to "theano", "tensorflow" or "cntk", and then Keras will make use of the modified configuration when you will run any Keras code. Once you define the KERAS_BACKEND environment variable, it will override whatsoever defined inside your config file: Possibly you can load many more backends in Keras then "tensorflow", "theano" or "cntk" as it can easily make use of external backends. This can be done by changing keras.json and "backend" setting. Let's suppose you have a Python module named as my_module to be used as an external backend; then, in that case, the keras.json file may undergo some changes, which is as follows; In order to use an external backend, it must be validated and encompass functions like placeholder, variable, and function. If the external backend is not valid then, it may generate an error which may contain all the missing entries. keras.json detailsFollowing are the settings contained in the keras.json file: The settings can be simply modified by editing $HOME/.keras/keras.json.
Usage of abstract Keras backend for writing new codeWith the help of abstract Keras backend API, you can make your written Keras module more compatible with both Theano (th) and TensorFlow (tf). Following is an intro to it; The backend module can be import thru: An input placeholder will be instantiated by the code given below, which is equal to tf.placeholder() or th.tensor.matrix(), th.tensor.tensor3(), etc. A variable will be instantiated by incorporating the following code, which in return is equal to tf.Variable() or th.shared(). Most of the tensor operations that you may require will be performed in a similar way as you will do in TensorFlow or Theano are as follows: Backend functionsbackend The backend function is used to revert back the current backend name. Returns It returns a string that relates to the current name of the backing being used. Example symbolic It can be defined as a decorator, which is utilized in TensorFlow 2.0 for entering the Keras graph. Arguments
Returns It returns a decorated function. eager It can be defined as a decorator, which is utilized in TensorFlow 2.0 for exiting the Keras graph. Arguments
Returns It returns a decorated function. get_uid It provides a unique UID that gives a string prefix. Arguments
Returns This backend function returns an integer. Example This function is used for setting up the manual variable initialization flags. The flag is indicated as a Boolean that governs for a variable to be initialized or the user has to handle the initialization because they are selfinstantiated by default. Arguments
epsilon It is used to return the fuzz factor value, which is being utilized in the numeric expressions. Returns It returns a float. Example reset_uids It is used to reset the graph identifiers. epsilon It outputs a fuzz factor value, which is utilized in the numeric expressions. Returns It returns a float value. Example set_epsilon It is used to set the fuzz factor value, which is being used in the numeric expressions. Arguments e: It can be defined as a float value that represents the epsilon's new value. Example floatx It is used to output a string of float type, such as 'float16', 'float32', 'float64'. Returns It returns a string of the current default float type. Example set_floatx It is used to set the default float type value. Arguments
Example Raises
cast_to_floatx It is used for casting a Numpy array to the default Keras float type. Arguments
Returns It returns the same Numpy array that is being casted to its new type. Example image_data_format It is used to returns the default image data format convention. Returns It returns a string either of 'channels_first' or 'channels_last' Example set_image_data_format This function is used for setting up the data format convention's value. Arguments
Example Raises
learning_phase It outputs the flag of a learning phase, which refers to a bool tensor (0 = test, 1 = train) to be passed as an input to any of the Keras function that utilizes a distinct behavior both at training and testing time. Returns It returns a scalar integer tensor or Python integer of the learning phase. set_learning_phase It is used to set a fixed value to the learning phase. Arguments
Raises
clear_session It is used for resetting each and every state that is produced by Keras. The global state that is utilized for the implementation of the Functional modelbuilding API as well as to uniquify autogenerated layer names is handled by Keras. When multiple models are built in a loop, then an increasing amount of memory over a certain time period will be consumed by the global state, which you will wish to clear it. It is used for destroying the current graph of Keras and creating a new one. It is very useful as it avoids clutter from old models/layers. Example1: calling clear_session() while creating models in a loop. Example2: resetting the layer name generation counter. is_sparse It is used to return whether a tensor is a sparse tensor. Arguments
Returns It returns a Boolean. Example to_dense It is used in conversion of a sparse tensor to a dense tensor and returns it. Arguments
Returns It returns a dense tensor. Example variable It helps in instantiating a variable and returning it. Arguments
Returns It returns an instance of a variable that comprising of a Keras metadata. Example is_variable constant It lead to the creation of a unique tensor. Arguments
Returns It also return a unique Tensor. is_keras_tensor It outputs whether x is a Keras tensor or not. A "Keras tensor" is a tensor that was returned by a Keras layer, (Layer class) or by Input. Arguments
Returns It returns a Boolean that represents whether the argument is a Keras tensor or not. Raises It raises a ValueError if x is not a symbolic tensor. Example is_tensor placeholder It helps in instantiating a placeholder tensor and returning it. Arguments
Returns It returns an instance of a Tensor by including a Keras metadata. Example is_placeholder It returns if x is a placeholder or not. Arguments
Returns It returns a Boolean. shape It outputs the symbolic shape of a tensor or variable. Arguments
Returns It returns a tensor of symbolic shape. Examples int_shape It can be defined as a tuple of int or None entries that outputs the tensor or a variable's shape. Arguments
Returns It either returns a tuple of integers or None entries. Example Numpy implementation ndim It refers to an integer that are returned as number of axes within a tensor. Arguments
Returns It outputs the number of axes as an integer value. Example Numpy implementation size It outputs the tensor size. Arguments
Returns It returns the tensor's size. Example dtype It can be defined as a string, which is returned as a dtype of a Keras tensor or variable. Arguments
Returns For x it returns its dtype. Example Numpy implementation eval It helps in evaluating tensor value. Arguments
Returns It outputs a Numpy array. Example Numpy implementation zeros It helps in instantiation of those variables that are allzeros followed by returning it. Arguments
Returns It returns a variable that includes the Keras metadata, which is filled with 0.0. It should be noted that if it is symbolic n shape, then a variable cannot be returned rather a dynamicshaped tensor will be returned. Example Numpy implementation ones It helps in Instantiation of an allones variable followed by returning it. Arguments
Returns It returns a Keras variable, which is filled with 0.0. It should be noted that if it is symbolic n shape, then a variable cannot be returned rather a dynamicshaped tensor will be returned. Example Numpy implementation eye It helps in the instantiation of an identity matrix followed by returning it. Arguments
Returns It outputs a Keras variable that represents an identity matrix. Example Numpy implementation zeros_like It helps in instantiating the similar shape variable that are allzeros as another tensor. Arguments
Returns It returns a variable of Keras filled with all zeros that constitutes a shape of x. Example Numpy implementation ones_like It helps in instantiating the similar shape variable that are allones as another tensor. Arguments
Returns It returns a variable of Keras filled with all zeros that constitutes a shape of x. Example Numpy implementation identity It outputs a tensor having a similar content as that of the input tensor. Arguments
Returns It returns a tensor that has same shape, type as well as content. random_uniform_variable It put an emphasis on the instantiation a variable that have its values drawn from a uniform distribution. Arguments
Returns It outputs a Keras variable that has been filled with drawn samples. Example Numpy implementation random_normal_variable It helps in the instantiation of a variable whose values are drawn from a normal distribution. Arguments
Returns It outputs a Keras variable that has been filled with drawn samples. Example Numpy implementation count_params It outputs the constant number of components residing within a Keras variable or tensor. Arguments
Returns It results in an integer, which depicts the total number of elements present in x, i.e., the product of the static dimensions of an array. Example Numpy implementation cast It helps in casting a tensor to a distinct dtype followed by returning it. In case, if you cast a Keras variable then also it will result in a Keras tensor. Arguments
Returns It output a Keras tensor with dtype dtype. Example update It helps in updating the value of x to new_x. Arguments
Returns It results in the updated x variable update_add It adds an increment, which helps to update the value of x. Arguments
Returns It returns the updated x variable. update_sub It subtracts the decrement so as to update the value of x. Arguments
Returns It returns the updated x variable. moving_average_update For a variable it computes its moving average. Arguments
Returns It outputs an operation, which is utilized for updating the variable. dot It returns a tensor by either multiplying 2 tensors or variable. While multiplying an nD tensor to another nD tensor, a Theano behavior is reproduced. (e.g. (2, 3) * (4, 3, 5) > (2, 4, 5)) Arguments
Returns It returns a tensor, which is produced after undergoing a dot product between x and y. Examples Numpy implementation batch_dot batch_dot is useful in computing batchwise dot product between x and y, where x and y are data inside batches (i.e. in a shape of (batch_size, :)). It either outputs a tensor or variable that encompass less dimensions than the input. If we reduce the number of dimensions to 1, then we can use expand_dims, which ensure the ndim to be atleast 2. Arguments
Returns It returns a tensor that has a shape identical to the concatenation of x's shape and y's shape (). Here the shape of x relates to the less the dimension that was summed over and y signifies less the batch dimension and the dimension that was summed over. However, it is reshaped to (batch_size, 1) if the final rank is 1. Examples Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17], [53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the offdiagonal elements. Pseudocode: Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape:
transpose It is used to transpose a tensor followed by returning it. Arguments
Returns It returns a tensor. Examples Numpy implementation gather It helps in the retrieval of indices indices elements within the reference of tensor. Arguments
Returns It outputs a tensor of same type as that of the reference. Numpy implementation max It calculates tensor's maximum value. Arguments
Returns It returns a tensor that represents the maximum values of x. Numpy implementation min It computes the minimum value inside a tensor. Arguments
Returns It returns a tensor that represents the minimum values of x. Numpy implementation sum It outputs the summation of values within a tensor, along with the specified axis. Arguments
Returns It returns a tensor encompassing sum of x. Numpy implementation prod In conjunction with the specific axis, it computes the multiplication of values inside a tensor. Arguments
Returns It returns a tensor encompassing product of elements within the x. Numpy implementation cumsum In conjunction with the specific axis, it computes the cumulative sum of values inside a tensor. Arguments
Returns It returns a tensor encompassing the cumulative sum of values of x along an axis. Numpy implementation cumprod In conjunction with the specific axis, it computes the cumulative product of values inside a tensor. Arguments
Returns It returns a tensor encompassing cumulative product of values of x along an axis. Numpy implementation var In conjunction with the specific axis, it computes the tensor's variance. Arguments
Returns It returns tensor's variance of elements residing in x. Numpy implementation rnn It is useful for reiterating above the tensor dimension. Arguments
Returns It outputs a tensor of shape (samples, output_dim)
Returns It returns a tuple of shape (last_output, outputs, new_states), where last_output relates to rnn's most recent output consisting a shape of (samples, …), outputs refer to a tensor of shape (samples, time, …), such that each entry outputs[s, t] corresponds to the step function's output for sample s and time t and new_states can be defined as a tensor list that represents the newest states, which are reverted by the step function of shape encompassing a shape of (samples, …). Raises
Next TopicKeras Models
