All Questions
Tagged with tf.keras keras-layer 
            
            171
            questions
        
        
            59
            votes
        
        
            20
            answers
        
        
            131k
            views
        
    How to fix "AttributeError: module 'tensorflow' has no attribute 'get_default_graph'"?
                I am trying to run some code to create an LSTM model but i get an error:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
My code is as follows:
from keras.models import ...
            
        
       
    
            46
            votes
        
        
            4
            answers
        
        
            96k
            views
        
    Running the Tensorflow 2.0 code gives 'ValueError: tf.function-decorated function tried to create variables on non-first call'. What am I doing wrong?
                error_giving_notebook
non_problematic_notebook
As it can be seen that I have used tf.function decorator in the 'error_giving_notebook' and it throws a ValueError while the same notebook without any ...
            
        
       
    
            11
            votes
        
        
            1
            answer
        
        
            1k
            views
        
    How do I save and load BatchNormalization Layer in this Tensorflow model?
                I am trying to save a model and then load it later to make some predictions; what happens is that the accuracy of the model after training is 95%+, but when I save it and then load it, the accuracy ...
            
        
       
    
            9
            votes
        
        
            2
            answers
        
        
            4k
            views
        
    Is it still necessary to implement `compute_output_shape()` when defining a custom tf.keras Layer?
                I have implemented a custom Layer in tf.keras, using TensorFlow 2.1.0.
In the past, when using the stand-alone Keras, it was important to define the compute_output_shape(input_shape) method in any ...
            
        
       
    
            5
            votes
        
        
            2
            answers
        
        
            17k
            views
        
    AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'
                I have been working on keras yolov3 model for object detection. This error keeps showing up. 
Here is the error:
AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'
...
            
        
       
    
            5
            votes
        
        
            2
            answers
        
        
            11k
            views
        
    keras : add layers to another model
                I need to add layers to an existing model. However, I need to add the layers at "the main model level", that is I can't use the classic functional approach. For example, if I use something ...
            
        
       
    
            5
            votes
        
        
            1
            answer
        
        
            3k
            views
        
    How does Embedding layer in Keras work on float input values?
                x is a (64, 1) dimensional vector created randomly using tf.random.uniform((BATCH_SIZE, 1)), where the BATCH_SIZE = 64.
A random initialization looks like this:
tf.Tensor(
[[0.76922464]
 [0.7928164 ]
 ...
            
        
       
    
            5
            votes
        
        
            0
            answers
        
        
            2k
            views
        
    keras.backend.function return a AttributeError: Layer dense is not connected, no input to return
                I want to know the median result of a neutral network for tuning purpose. I design my model and use keras.backend.function but failed(AttributeError: Layer dense is not connected, no input to return). ...
            
        
       
    
            4
            votes
        
        
            1
            answer
        
        
            5k
            views
        
    Adding custom metric Keras Subclassing API
                I'm following the section "Losses and Metrics Based on Model Internals" on chapter 12 of "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition - Aurélien Geron", in which he ...
            
        
       
    
            4
            votes
        
        
            1
            answer
        
        
            2k
            views
        
    Memory leak when running universal-sentence-encoder-large itterating on dataframe
                I have 140K sentences I want to get embeddings for. I am using TF_HUB Universal Sentence Encoder and am iterating over the sentences(I know it's not the best way but when I try to feed over 500 ...
            
        
       
    
            4
            votes
        
        
            1
            answer
        
        
            4k
            views
        
    Unable to understand the behavior of method `build` in tensorflow keras layers (tf.keras.layers.Layer)
                Layers in tensorflow keras have a method build that is used to defer the weights creation to a time when you have seen what the input is going to be. a layer's build method
I have a few questions i ...
            
        
       
    
            4
            votes
        
        
            0
            answers
        
        
            2k
            views
        
    TypeError: Cannot interpret '<KerasTensor: shape=(None, 3) dtype=float32 (created by layer 'input_1')>' as a data type
                Very simple question. I am using tensorflow probability package to use a bijector to form a trainable_distribution from a simple distribution (let's say gaussian)
Everything works properly in jupyter ...
            
        
       
    
            3
            votes
        
        
            3
            answers
        
        
            2k
            views
        
    How to display the layers of a pretrained model instead of a single entry in model.summary() output?
                As the title clearly describes the question, I want to display the layers of a pretained model instead of a single entry (please see the vgg19 (Functional) entry below) in model.summary() function ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            1k
            views
        
    Keras Layer Build Error: build() takes 1 Positional Argument but two were given
                I have the following error in this simple layer:
class MyLayer(Layer):
def __init__(self):
    super(MyLayer, self).__init__()
def build(self):
    # Create a trainable weight variable for this ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            62
            views
        
    a problem and how to deal with batch while creating a Model
                from keras_multi_head import MultiHeadAttention
import keras
from keras.layers import Dense,Input,Multiply
from keras import backend as K
from keras.layers.core import Dropout, Layer
from keras.models ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            486
            views
        
    Converting Keras to Tensorflow -- Pruning + Probability
                I have a code that mixes Tensorflow Probability (requires TF 2.00) with Keras Pruning, pruning weights of a first Dense Layer and providing inputs for TF probability, having both codes (Keras + TF) in ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            689
            views
        
    Tensorflow v2.16 tf.keras.layers Embedding not accepting argument for input_length
                While working with Embedding for an LSTM model I came across an issue with input_length:
ValueError: Unrecognized keyword arguments passed to Embedding: {'input_length': 536}
Is input length being ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            125
            views
        
    How to obtain all layers within a custom layer subclassed from keras.layers
                I'm trying to get all layers from within a custom layer subclassed from tf.keras.layers but I am having difficulties with this. The end goal is to create a DAG (Directed Acyclic Graph) that has layers ...
            
        
       
    
            3
            votes
        
        
            1
            answer
        
        
            790
            views
        
    How to freeze selected weights in keras or tf.keras?
                I am trying out something where I required to freeze some selected weights. Take this example 
from keras.models import Sequential
from keras.layers import Dense,Input
model = Sequential()
model.add(...
            
        
       
    
            3
            votes
        
        
            0
            answers
        
        
            59
            views
        
    Custom layer at top of Tensorflow model
                I want to add a Custom Layer to a Tensorflow Model which transforms every row of a the input set to the model in a set pattern.
For example, below is the custom layer than I have:
class CustomLayer:
  ...
            
        
       
    
            2
            votes
        
        
            2
            answers
        
        
            8k
            views
        
    How to remove the last layer from a pre-trained model. I have tried model.layers.pop() but it is not working
                I am trying to remove the last layer so that I can use transfer Leaning.
vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential()
for layer in vgg16_model.layers:
    model.add(layer)
...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            5k
            views
        
    Keras --- Training freezes during fit_generator()
                I am trying to train my 6000 train dataset and 1000 validation dataset but I have a problem: the program just freezes and hangs during training without any error message .
1970/6000 [========>........
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            4k
            views
        
    keras Conv2d value error: Negative dimension size
                New to keras, i am  dealing with a regression problem with Tensorflow as backend. 
X1 = TrainingSet[:,0:603]
Y1 = TrainingSet[:,603:607]
###################################
#reshape Xtrain for CNN
X1 ...
            
        
       
    
            2
            votes
        
        
            3
            answers
        
        
            2k
            views
        
    Can't load Keras model using RectifiedAdam optimizer
                Trying to load a Keras model using tf.keras.models.load_model I get the following error:
import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
model = tf.keras.models....
            
        
       
    
            2
            votes
        
        
            2
            answers
        
        
            4k
            views
        
    "A `Concatenate` layer requires inputs with matching shapes except for the concat axis." how to solve this issuse?
                Below is my unet model ,i loaded images with kerasImageDataGenerator with 360X480
 dim,but when compile the model.
but model is complied if dim are 128X128 , 256X256, to load this dim what are ...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            112
            views
        
    ValueError in model subclassing with tensorflow 2
                I'm trying to implement a WideResnet using Model subclassing in keras. I cannot understand what's wrong in my code:
class ResidualBlock(layers.Layer):
  def __init__(self, filters, kernel_size, ...
            
        
       
    
            2
            votes
        
        
            3
            answers
        
        
            9k
            views
        
    'KerasLayer' object has no attribute 'layers'
                I'm trying to customize the model taken from tf hub but
can't access the layers with following error  'KerasLayer' object has no attribute 'layers' 
Here is my code as an example: 
import ...
            
        
       
    
            2
            votes
        
        
            3
            answers
        
        
            9k
            views
        
    TypeError: __init__() got multiple values for argument 'kernel_size'
                I am getting the below error while executing my program ...
def conv2d(x, output_dim, k_size=5, stride=2, stddev=0.02, name="conv2d"):
    #conv = tf.keras.layers.Conv2D(x, output_dim, kernel_size=...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            3k
            views
        
    ValueError: Dimensions must be equal, but are 68 and 10 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT]
                I am using a LSTM and a VNN to convert active text to passive text. I am feeding in tokenized data to the lstm and getting a feature vector of shape (68,1) and I am then using that as input data for a ...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            1k
            views
        
    How to use batch size to create a tensor within a custom TensorFlow Layer
                I'm creating a custom TF layer and inside it I need to create a tensor of ones with something like this
class MyLayer(Layer):
  def __init__(self, config, **kwargs):
    super(MyLayer, self).__init__(*...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            864
            views
        
    How to perform Virtual Batch Normalization (VBN) in keras
                VBN is talked in This paper. And implemented Here, Here and Here. I donot want to go to core/full code. I just want to know, how to use VBN as keras layer, as i am not very expert tensorflow/keras ...
            
        
       
    
            2
            votes
        
        
            0
            answers
        
        
            472
            views
        
    Custom Image Shear layer in tf.keras
                Im migrating from Keras's ImageDataGenerators to tf.data and I have issues with reproducing the same augmentation I used to have in generator. While there are regularization layers for Random Zoom, ...
            
        
       
    
            2
            votes
        
        
            1
            answer
        
        
            371
            views
        
    Multiply output of Keras layer with a scalar
                Pardon me if this is a poorly framed question. This happens to be my first question here.
Say I have an output layer in Keras, and I want to multiply the last value (result of sigmoid activation) with ...
            
        
       
    
            2
            votes
        
        
            0
            answers
        
        
            341
            views
        
    Error with Tensorflow 2 in a custom-written layer which worked okay in Tensorflow 1 (assert t.graph == cond_graph; AssertionError)
                I have written a custom-written layer (implementing this article in Keras). This layer works fine in TF versions (1.13 and 1.15) as I have tested myself. Also, in TF 2.0, when I run tf.compat.v1....
            
        
       
    
            1
            vote
        
        
            2
            answers
        
        
            9k
            views
        
    ValueError: A merge layer should be called on a list of inputs. Tensorflow Keras
                I am currently trying to use the first 50 layers of the MobileNetV2. Therefore, I want to extract those layers and create a new model.
I thought I could just call every layer, but the "block_2_add" ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            2k
            views
        
    Loading a model Raise ValueError Unknown loss function
                this is the code after i try to save and load my model: 
model.save('path_to_my_model.h5')
del model
model = tf.keras.models.load_model('path_to_my_model.h5', custom_objects={'Wraparound2D': ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            5k
            views
        
    how to see tensor value of a layer output in keras
                I have a Seq2Seq model. I am interested to print out the matrix value of the output of the encoder per iteration.
So for example as the dimension of the matrix in the encoder is (?,20) and the epoch =...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            2k
            views
        
    How to load a saved model with keras? (Error : : TypeError: __init__() got an unexpected keyword argument 'trainable')
                I created a CAPTCHA model based on what is provided in the Keras code example.
But when I load the model, an error pops up.
I show you the code I wrote in Jupyter notebook.
STEP1) Model build
class ...
            
        
       
    
            1
            vote
        
        
            2
            answers
        
        
            711
            views
        
    Concatenation of ResNet (2D images) with fully-connected network (1D input)
                I'm using a pre-built ResNet in Keras (TensorFlow 2) in the following way:
from tensorflow.keras.applications.resnet50 import ResNet50
base_model = ResNet50(weights=None, include_top=False, ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            124
            views
        
    a question about boolean list in custom layer in tf.keras
                I'm trying to construct a custom output layer for my model so that the range of angles can be constrained in [-90,90]. The code is as follows:
class OutputLayer(Layer):
    def __init__(self):
       ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            548
            views
        
    Does freezing keras layers reduce memory needed for training?
                I have pretrained weights of a large model that can't fit in my vram. If I freeze layers, the memory needed for training will be reduced? If so, the freezed layer weights aren't saved in vram?
            
        
       
    
            1
            vote
        
        
            2
            answers
        
        
            2k
            views
        
    Can I write a Keras Lambda layer with non-trainable/fixed weights?
                https://keras.io/layers/writing-your-own-keras-layers/
According to here, stateless customer operations can be wrapped to Lambda layers. But if there are trainable variables, we need to implement our ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            2k
            views
        
    Keras (Tensorflow) Reshape Layer input error
                I have a reshape input error and i don't know why.
The requested shape is 1058400, what is (1, 21168) multiplied with the batch size of 50.
What I do not understand is the apparent input size of ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            429
            views
        
    TensorFlow 'Variable' has no trainable parameters
                I'm using a Keras Lambda Layer to make some operations with a tensor of trainable weights (or at least it should); to do that I choosed a tf.Variable as parameter but, despite trainable=True, the ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            429
            views
        
    Is it possible to add different behavior for training and testing in keras Functional API
                I want to use different behavior in functional API for training and testing. Is it possible?
E.g.,
a = Input
b = CONV1(a)
if testing:
    return b
c = CONV2(b)
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            750
            views
        
    keras: insert layers to the beginning (near the input) of an existing model
                I need to add layers to the beginning of an existing model. However, I need to add the layers on "the main model level", that is I can't use the classic functional approach. For example, if ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            191
            views
        
    How to retrieve intermediate predictions results in a loop
                Need to calculate time of the prediction results of each layer and pass the output of the model as an input to another layer. Treating each layer as a model and getting the intermediate results and ...
            
        
       
    
            1
            vote
        
        
            1
            answer
        
        
            789
            views
        
    Fix “AttributeError: module 'tensorflow' has no attribute 'get_default_graph'”
                I have created an LSTM model, when  run it I get the following error:
 (...) File "/Users/myfolder/Desktop/Project-Deep-Learning-master/Flask_App/app.py", line 40, in <module>
    graph = tf....
            
        
       
    
            1
            vote
        
        
            0
            answers
        
        
            16
            views
        
    Extracting intermediate layer of saved model in keras. That intermediate layer is another sub-model defined in main model
                Here is my architecture.
def model1():
    inputs = layers.Input(shape=(x1.shape[-2],x1.shape[-1]), name='Input_layer')
    x = layers.Reshape((x1.shape[-2], x1.shape[-1], 1), name='Input_reshape')(...
            
        
       
    
            1
            vote
        
        
            0
            answers
        
        
            39
            views
        
    Keras tensorflow Model not returning Map with UserDefined Keynames
                I am new to Keras API , I am able to get Map as response but i am not able to get userdefined key names in response For Ex: My Expected response is
{
    "predictions": [
        {
          ...
            
        
       
     
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
        