Tensorflow: Cannot interpret feed_dict key as Tensor

前端 未结 7 1045
清歌不尽
清歌不尽 2020-12-08 14:27

I am trying to build a neural network model with one hidden layer (1024 nodes). The hidden layer is nothing but a relu unit. I am also processing the input data in batches o

相关标签:
7条回答
  • 2020-12-08 14:54

    In my case, I was using loop while calling in CNN multiple times, I fixed my problem by doing the following:

    # Declare this as global:
    
    global graph
    
    graph = tf.get_default_graph()
    
    # Then just before you call in your model, use this
    
    with graph.as_default():
    
    # call you models here
    

    Note: In my case too, the app ran fine for the first time and then gave the error above. Using the above fix solved the problem.

    Hope that helps.

    0 讨论(0)
  • 2020-12-08 14:56

    The error message TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("...", dtype=dtype) is not an element of this graph can also arise in case you run a session outside of the scope of its with statement. Consider:

    with tf.Session() as sess:
        sess.run(logits, feed_dict=feed_dict) 
    
    sess.run(logits, feed_dict=feed_dict)
    

    If logits and feed_dict are defined properly, the first sess.run command will execute normally, but the second will raise the mentioned error.

    0 讨论(0)
  • 2020-12-08 14:57

    This worked for me

    from keras import backend as K
    

    and after predicting my data i inserted this part of code then i had again loaded the model.

    K.clear_session()
    

    i faced this problem in production server, but in my pc it was running fine

    ...........

    from keras import backend as K
    
    #Before prediction
    K.clear_session()
    
    #After prediction
    K.clear_session()
    
    0 讨论(0)
  • 2020-12-08 15:01

    Variable x is not in the same graph as model, try to define all of these in the same graph scope. For example,

    # define a graph
    graph1 = tf.Graph()
    with graph1.as_default():
        # placeholder
        x = tf.placeholder(...)
        y = tf.placeholder(...)
        # create model
        model = create(x, w, b)
    
    with tf.Session(graph=graph1) as sess:
    # initialize all the variables
    sess.run(init)
    # then feed_dict
    # ......
    
    0 讨论(0)
  • 2020-12-08 15:03

    You can also experience this while working on notebooks hosted on online learning platforms like Coursera. So, implementing following code could help get over with the issue.

    Implement this at the topmost block of Notebook file:

    1. from keras import backend as K
    2. K.clear_session()
    0 讨论(0)
  • 2020-12-08 15:14

    If you use django server, just runserver with --nothreading for example:

    python manage.py runserver --nothreading  
    
    0 讨论(0)
提交回复
热议问题