title: EEG data classification using DNN in Tensorflow
body: <p>Following the iris classification demo:
  <a href="https://www.tensorflow.org/get_started/tflearn#construct_a_deep_neural_network_classifier" rel="nofollow noreferrer">https://www.tensorflow.org/get_started/tflearn#construct_a_deep_neural_network_classifier</a>
  <a href="https://www.tensorflow.org/get_started/monitors" rel="nofollow noreferrer">https://www.tensorflow.org/get_started/monitors</a></p>
  
  <p>I was expecting to get the same good accuracy using eeg data as input data for classification of actions. However,the accuracy obtained is below 70% using the code below:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  import tensorflow as tf
  import shutil
  
  IRIS_TRAINING = "eeg_training2.csv"
  IRIS_TEST ="eeg_test.csv"
  
  # Define the training inputs
  def get_train_inputs():
      x = tf.constant(training_set.data)
      y = tf.constant(training_set.target)
  
      return x, y
  # Define the test inputs
  def get_test_inputs():
    x = tf.constant(test_set.data)
    y = tf.constant(test_set.target)
  
    return x, y
  
  
    # Needed to make sure the logging output is visible.
  tf.logging.set_verbosity(tf.logging.INFO)
  
  shutil.rmtree('./tmp',ignore_errors=True)
  
  # Load datasets
  training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32)
  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TEST, target_dtype=np.int, features_dtype=np.float32)
  print('Training set lenght:' + str(len(training_set)))
  print('Testing set lenght:' + str(len(test_set)))
  print('database loaded')
  
  
  validation_metrics = {
      "accuracy":
          tf.contrib.learn.MetricSpec(
              metric_fn=tf.contrib.metrics.streaming_accuracy,
              prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
      "precision":
          tf.contrib.learn.MetricSpec(
              metric_fn=tf.contrib.metrics.streaming_precision,
              prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
      "recall":
          tf.contrib.learn.MetricSpec(
              metric_fn=tf.contrib.metrics.streaming_recall,
              prediction_key=tf.contrib.learn.PredictionKey.CLASSES)
  }
  
  validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(
      test_set.data,
      test_set.target,
      every_n_steps=50,
      metrics=validation_metrics,
      early_stopping_metric="loss",
      early_stopping_metric_minimize=True,
      early_stopping_rounds=200)
  
  # Define the features used in the DNN model: 14 features with real value
  feature_columns = [tf.contrib.layers.real_valued_column("", dimension=14)]
  
  # Build 3 layer DNN with 25, 50, 25 units respectively.
  classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                              hidden_units=[25, 50, 25],
                                              n_classes=2,
                                              model_dir="./tmp/eeg-model",
                                              #dropout=0.1,
                                              config=tf.contrib.learn.RunConfig(save_checkpoints_secs=10))
  
  # Fit model.
  #classifier.fit(input_fn=get_train_inputs, steps=2000)
  classifier.fit(x=training_set.data,
                 y=training_set.target,
                 steps=5000,
                 monitors=[validation_monitor])
  
    # Evaluate accuracy.
  accuracy_score = classifier.evaluate(input_fn=get_test_inputs,
                                         steps=1)["accuracy"]
  
  print("\nTest Accuracy: {0:f}".format(accuracy_score*100) + '%\n')
  
    # Classify two new flower samples. first: 0 second: 1
  def new_samples():
      return np.array(
        [[4289.23,4006.15,4264.62,4121.03,4327.18,4614.87,4067.69,4614.87,4196.92,4225.64,4192.31,4269.23,4595.9,4350.77],
  [4291.79,4009.23,4265.64,4125.13,4330.26,4621.03,4071.79,4619.49,4193.33,4229.74,4195.38,4273.85,4601.03,4349.23],
  [4293.85,4008.72,4264.62,4126.67,4333.33,4623.08,4075.9,4617.95,4193.85,4233.85,4202.05,4275.38,4607.18,4355.9],
  [4295.38,4010.77,4266.67,4126.67,4333.33,4618.97,4075.9,4610.26,4193.85,4234.36,4205.13,4274.87,4605.64,4361.03],
  [4294.36,4016.92,4273.33,4132.82,4335.9,4618.46,4077.95,4610.26,4196.92,4232.31,4205.64,4279.49,4609.23,4358.46],
  [4289.74,4012.82,4273.33,4130.77,4333.85,4617.95,4081.03,4613.85,4195.9,4223.59,4202.05,4282.05,4609.23,4352.31],
  [4286.15,4000,4265.13,4121.03,4325.64,4615.38,4078.97,4612.31,4193.85,4220.51,4194.87,4275.9,4598.97,4347.18],
  [4286.15,3995.38,4263.59,4121.03,4328.21,4617.44,4074.87,4611.79,4200.51,4222.56,4191.28,4271.28,4594.36,4348.21],
  [4287.18,3996.41,4271.79,4123.59,4333.33,4618.97,4074.36,4614.87,4205.13,4226.15,4194.36,4271.79,4596.41,4350.77],
  [4286.67,3994.87,4273.85,4121.54,4330.77,4616.41,4075.9,4620.51,4203.08,4230.77,4197.44,4271.79,4598.46,4350.77],
  [4308.21,3993.33,4260.51,4096.92,4328.72,4607.69,4060.51,4612.31,4202.56,4228.21,4191.79,4271.28,4606.67,4365.64],
  [4296.92,3986.15,4249.23,4092.82,4327.69,4609.74,4064.62,4613.33,4206.67,4229.23,4192.31,4265.13,4596.41,4357.95],
  [4294.36,3985.64,4245.13,4092.82,4327.69,4616.92,4066.15,4620.51,4210.77,4229.74,4196.41,4264.62,4596.41,4355.38],
  [4295.38,3990.26,4247.69,4095.9,4328.21,4614.36,4059.49,4617.44,4204.1,4229.23,4197.44,4265.64,4604.1,4360],
  [4300,3992.82,4254.87,4103.59,4329.74,4611.28,4056.92,4612.31,4205.13,4230.26,4198.46,4265.64,4603.08,4365.13],
  [4306.67,3994.36,4263.08,4107.18,4331.28,4614.36,4061.03,4614.87,4214.87,4232.82,4203.08,4268.72,4602.05,4366.67],
  [4311.79,4000,4266.67,4108.72,4334.36,4613.85,4063.08,4615.38,4213.85,4234.36,4208.21,4276.41,4610.26,4373.85],
  [4313.33,4006.67,4265.64,4116.41,4335.9,4609.74,4060.51,4612.82,4205.64,4235.9,4212.31,4282.56,4617.95,4380.51],
  [4314.36,4008.21,4265.64,4118.97,4336.41,4611.79,4061.03,4616.92,4212.82,4242.05,4217.95,4284.62,4619.49,4378.97],
  [4317.95,4008.21,4266.15,4114.87,4337.44,4620,4066.67,4622.05,4220.51,4247.69,4218.97,4286.67,4620,4378.97]], dtype=np.float32)
  
  predictions = list(classifier.predict(input_fn=new_samples))
  
  print("New Samples, Class Predictions:    {}\n".format(predictions))#Ground truth: {1,2}
  </code></pre>
  
  <p>Does someone tried Tensorflow's DNN for eeg classification? I saw a post here at stackoverflow but they used cnn which could not be the best choice for classification of eeg data. My data came from UCI's repository: <a href="https://archive.ics.uci.edu/ml/datasets/EEG+Eye+State#" rel="nofollow noreferrer">https://archive.ics.uci.edu/ml/datasets/EEG+Eye+State#</a> . I used the first 6000 instances for training and the last 1000 for testing but results are really poor. I changed the structure of the hidden layers and increased the number of runs but results still are bad. Any suggestion about the code in order to get higher accuracy will be appreciate. Any suggestions about better approaches in Tensorflow for eeg data classification will be appreciate too.</p>