docstr: This example demonstrates the use of Convolution1D for text classification.
  This example is from Keras
docstr_tok: ["This","example","demonstrates","the","use","of","Convolution1D","for","text","classification",".","This","example","is","from","Keras"]
code: def test_text_classification(self):
      """This example demonstrates the use of Convolution1D for text classification.
      This example is from Keras
      """
  
      # TODO: support backend support
  
      import numpy as np
  
      np.random.seed(1337)  # for reproducibility
  
      from keras.preprocessing import sequence
      from keras.models import Sequential
      from keras.layers import Dense, Dropout, Activation
      from keras.layers import Embedding
      from keras.layers import Convolution1D
      from keras.datasets import imdb
  
      # set parameters:
      max_features = 5000
      maxlen = 400
      batch_size = 32
      embedding_dims = 50
      nb_filter = 250
      filter_length = 3
      hidden_dims = 250
      nb_epoch = 2
  
      print("Loading data...")
      (X_train, y_train), (X_test, y_test) = imdb.load_data(
          nb_words=max_features
      )
      print(len(X_train), "train sequences")
      print(len(X_test), "test sequences")
  
      print("Pad sequences (samples x time)")
      X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
      X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
      print("X_train shape:", X_train.shape)
      print("X_test shape:", X_test.shape)
  
      print("Build model...")
      model = Sequential()
  
      model.add(
          Embedding(max_features, embedding_dims, input_length=maxlen)
      )  # Exception if specify Dropout dropout=0.2
  
      # we add a Convolution1D, which will learn nb_filter
      # word group filters of size filter_length:
      model.add(
          Convolution1D(
              nb_filter=nb_filter,
              filter_length=filter_length,
              border_mode="valid",
              activation="relu",
              subsample_length=1,
          )
      )
      # we use max pooling:
      model.add(GlobalMaxPooling1D())
      # model.add(GlobalAveragePooling1D())
  
      # We add a vanilla hidden layer:
      model.add(Dense(hidden_dims))
      model.add(Dropout(0.2))
      model.add(Activation("relu"))
  
      # We project onto a single unit output layer, and squash it with a sigmoid:
      model.add(Dense(1))
      model.add(Activation("sigmoid"))
  
      model.compile(
          loss="binary_crossentropy",
          optimizer="adam",
          metrics=["accuracy"],
      )
      model = with_bigdl_backend(model)
  
      model.fit(
          X_train,
          y_train,
          batch_size=batch_size,
          nb_epoch=nb_epoch,
          validation_data=(X_test, y_test),
      )
      # 2017-09-22 15:53:45 INFO  DistriOptimizer$:657
      # - Top1Accuracy is Accuracy(correct: 21557, count: 25000, accuracy: 0.86228)
      # this result is from GlobalAveragePooling not GlobalMaxPooling.
      model.predict(X_test)
      model.evaluate(X_test, y_test)
      print(model)
  
file_path: BigDL/python/dllib/test/bigdl/nn/test_backend.py
imports: from __future__ import print_function
  from keras.layers import *
  import pytest
  from keras.applications import *
  from bigdl.dllib.keras.converter import *
  from bigdl.dllib.nn.keras.backend import *
  from test.bigdl.test_utils import BigDLTestCase, TestModels
label_desc: Use image node_id 6 for calling the TestBackend obj's underlying member method code with example usage: obj.test_text_classification() without return types