title: 'tf' is not defined on load_model() - using lambda
body: <p>I have a <code>Keras</code> model that I am trying to export and use in a different python code.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>from keras.models import Sequential
  from keras.layers import Dense, Embedding, LSTM, GRU, Flatten, Dropout, Lambda
  from keras.layers.embeddings import Embedding
  import tensorflow as tf
  
  
  EMBEDDING_DIM = 100
  
  model = Sequential()
  model.add(Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_length, trainable=False))
  model.add(Lambda(lambda x: tf.reduce_mean(x, axis=1)))
  model.add(Dense(8, input_dim=4, activation='relu'))
  model.add(Dense(3, activation='softmax'))
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  model.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_val_pad, y_val), verbose=2)
  model.save('my_model.h5') 
  </code></pre>
  
  <hr>
  
  <p>In <strong>another file</strong>, when I import <code>my_model.h5</code> :</p>
  
  <pre><code>from keras.models import load_model
  from keras.layers import Lambda
  import tensorflow as tf
  
  
  def learning(test_samples):
      model = load_model('my_model.h5')
      #ERROR HERE
      #rest of the code
  </code></pre>
  
  <p>The error is the following:</p>
  
  <pre><code>  in &lt;lambda&gt;
      model.add(Lambda(lambda x: tf.reduce_mean(x, axis=1)))
  NameError: name 'tf' is not defined
  </code></pre>
  
  <p>After research, I got that the fact that <strong>I used <code>lambda</code> in my model</strong> is the reason for this problem, but I added these references and it didn't help:</p>
  
  <pre><code>from keras.models import load_model
  from keras.layers import Lambda
  import tensorflow as tf
  </code></pre>
  
  <p>What could be the problem?</p>
  
  <p>Thank you</p>