title: How can I clean memory or use SageMaker instead to avoid MemoryError: Unable to allocate for an array with shape (25000, 2000) and data type float64
body: <p>I'm using keras to train a model on SageMaker, here's the code I'm using but I hit the error:</p>
  <pre><code>MemoryError: Unable to allocate 381. MiB for an array with shape (25000, 2000) 
      and data type float64
  </code></pre>
  <p>Here's the code:</p>
  <pre><code>import pandas as pd
  import numpy as np
  from keras.datasets import imdb
  from keras import models, layers, optimizers, losses, metrics
  import matplotlib.pyplot as plt
  
  # load imbd preprocessed dataset
  (train_data, train_labels), (test_data, test_labels) = imdb.load_data(
      num_words=2000)
  
  # one-hot encoding all the integer into a binary matrix
  def vectorize_sequences(sequences, dimension=2000):
      results = np.zeros((len(sequences), dimension))        
      for i, sequence in enumerate(sequences):
          results[i, sequence] = 1.                          
      return results
  
  x_train = vectorize_sequences(train_data)                  
  x_test = vectorize_sequences(test_data)
  </code></pre>
  <p>Then I get the error.</p>
  <p>The first time when I run this code it works but it failed when I tried to re-run it, how I can fix it by cleaning the memory or is there a way that I can use the memory on SageMaker?</p>