title: joblib.load __main__ AttributeError
body: <p>I'm starting to dive into deploying a predictive model to a web app using Flask, and unfortunately getting stuck at the starting gate. </p>
  
  <p><strong>What I did:</strong></p>
  
  <p>I pickled my model in my <strong>model.py</strong> program:</p>
  
  <pre><code>import numpy as np
  from sklearn.externals import joblib
  
  class NeuralNetwork():
      """
      Two (hidden) layer neural network model. 
      First and second layer contain the same number of hidden units
      """
      def __init__(self, input_dim, units, std=0.0001):
          self.params = {}
          self.input_dim = input_dim
  
          self.params['W1'] = np.random.rand(self.input_dim, units)
          self.params['W1'] *= std
          self.params['b1'] = np.zeros((units))
  
          self.params['W2'] = np.random.rand(units, units)
          self.params['W2'] *= std * 10  # Compensate for vanishing gradients
          self.params['b2'] = np.zeros((units))
  
          self.params['W3'] = np.random.rand(units, 1)
          self.params['b3'] = np.zeros((1,))
  
  model = NeuralNetwork(input_dim=12, units=64)
  
  #####THIS RIGHT HERE ##############
  joblib.dump(model, 'demo_model.pkl')
  </code></pre>
  
  <p>then I created an <strong>api.py</strong> file in the same directory as my <em>demo_model.pkl</em>, per this tutorial (<a href="https://blog.hyperiondev.com/index.php/2018/02/01/deploy-machine-learning-models-flask-api/" rel="noreferrer">https://blog.hyperiondev.com/index.php/2018/02/01/deploy-machine-learning-models-flask-api/</a>):</p>
  
  <pre><code>import flask
  from flask import Flask, render_template, request
  from sklearn.externals import joblib
  
  app = Flask(__name__)
  
  
  @app.route("/")
  @app.route("/index")
  def index():
      return flask.render_template('index.html')
  
  
  # create endpoint for the predictions (HTTP POST requests)
  @app.route('/predict', methods=['POST'])
  def make_prediction():
      if request.method == 'POST':
          return render_template('index.html', label='3')
  
  
  if __name__ == '__main__':
      # LOAD MODEL WHEN APP RUNS ####
      model = joblib.load('demo_model.pkl')
      app.run(host='0.0.0.0', port=8000, debug=True)
  </code></pre>
  
  <p>I also made a templates/index.html file in the same directory with this info:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;NN Model as Flask API&lt;/title&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Boston Housing Price Predictor&lt;/h1&gt;
          &lt;form action="/predict" method="post" enctype="multipart/form-data"&gt;
              &lt;input type="file" name="image" value="Upload"&gt;
              &lt;input type="submit" value="Predict"&gt; {% if label %} {{ label }} {% endif %}
          &lt;/form&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>running:</p>
  
  <pre><code>&gt;&gt; python api.py
  </code></pre>
  
  <p>gives me an error with the pickler:</p>
  
  <pre><code>Traceback (most recent call last):
    File "api.py", line 22, in &lt;module&gt;
      model = joblib.load('model.pkl')
    File "C:\Users\joshu\Anaconda3\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 578, in load
      obj = _unpickle(fobj, filename, mmap_mode)
    File "C:\Users\joshu\Anaconda3\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 508, in _unpickle
      obj = unpickler.load()
    File "C:\Users\joshu\Anaconda3\lib\pickle.py", line 1043, in load
      dispatch[key[0]](self)
    File "C:\Users\joshu\Anaconda3\lib\pickle.py", line 1342, in load_global
      klass = self.find_class(module, name)
    File "C:\Users\joshu\Anaconda3\lib\pickle.py", line 1396, in find_class
      return getattr(sys.modules[module], name)
  AttributeError: module '__main__' has no attribute 'NeuralNetwork'
  </code></pre>
  
  <p>Why is the main module of the program getting involved with my NeuralNetwork model? I'm very confused at the moment... any advice would be appreciated.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Adding a class definition <code>class NeuralNetwork(object): pass</code> to my <strong>api.py</strong> program fixed the bug. </p>
  
  <pre><code>import flask
  from flask import Flask, render_template, request
  from sklearn.externals import joblib
  
  
  class NeuralNetwork(object):
      pass
  
  
  app = Flask(__name__)
  </code></pre>
  
  <p><strong><em>If anyone would be willing to offer me an explanation of what was going on that would be hugely appreciated!</em></strong> </p>