Kaggle (Titanic) x Neural Network の実装に関するメモ

##Reference
[Titanic: Machine Learning from Disaster](https://www.kaggle.com/c/titanic)

[Google Colab + Kaggle (Titanic) の実装に関するメモ](https://qiita.com/takeshikondo/items/32fabcfcfb48490abcfa)

##File Upload
```python
from google.colab import drive

drive.mount('/content/gdrive')

print (os.listdir('/content/gdrive/My Drive/...'))
```

##Data
```python
train = pd.read_csv('/content/train.csv')
test = pd.read_csv('/content/test.csv')

train['Age'] = train['Age'].fillna(train['Age'].median())
train['Embarked'] = train['Embarked'].fillna('S')

train['Sex'][train['Sex'] == 'male'] = 0
train['Sex'][train['Sex'] == 'female'] = 1
train['Embarked'][train['Embarked'] == 'S' ] = 0
train['Embarked'][train['Embarked'] == 'C' ] = 1
train['Embarked'][train['Embarked'] == 'Q'] = 2
 
#train.head(10)

test['Age'] = test['Age'].fillna(test['Age'].median())
test['Sex'][test['Sex'] == 'male'] = 0
test['Sex'][test['Sex'] == 'female'] = 1
test['Embarked'][test['Embarked'] == 'S'] = 0
test['Embarked'][test['Embarked'] == 'C'] = 1
test['Embarked'][test['Embarked'] == 'Q'] = 2
test.Fare[152] = test.Fare.median()
 
#test.head(10)

features = train[['Pclass', 'Sex', 'Age', 'Fare']].values
target = train['Survived'].values
features_test = test[['Pclass', 'Sex', 'Age', 'Fare']].values

train_indices = np.random.choice(len(features), round((len(features)*0.8)), \
                                replace = False)
test_indices = np.array(list(set(range(len(features))) - set(train_indices)))

x_train = features[train_indices]
x_test = features[test_indices]
y_train = np.identity(2)[target[train_indices]]
y_test = np.identity(2)[target[test_indices]]

def normalize_cols(m):
  col_max = m.max(axis = 0)
  col_min = m.min(axis = 0)
  return (m - col_min) / (col_max - col_min)

x_train = normalize_cols(x_train)
x_test = normalize_cols(x_test)
x_input = normalize_cols(features_test)
```

##Sample Code
```python
# MLP

class MLP():
  def __init__(self):
    pass

  def weight_variable(self, name, shape):
    initializer = tf.truncated_normal_initializer(mean = 0.0, stddev = 0.01, dtype = tf.float32)
    return tf.get_variable(name, shape, initializer = initializer)

  def bias_variable(self, name, shape):
    initializer = tf.constant_initializer(value = 0.0, dtype = tf.float32)
    return tf.get_variable(name, shape, initializer = initializer)

  def mlp(self, x, n_in, n_units, n_classes, keep_prob, reuse = False):
    with tf.variable_scope('mlp', reuse = reuse):
      w_1 = self.weight_variable('w_1', [n_in, n_units])
      b_1 = self.bias_variable('b_1', [n_units])

      f = tf.matmul(x, w_1) + b_1

      # batch norm
      #batch_mean, batch_var = tf.nn.moments(f, [0])
      #f = (f - batch_mean) / (tf.sqrt(batch_var) + 1e-10)

      # dropout
      f = tf.nn.dropout(f, keep_prob)

      # relu
      f = tf.nn.relu(f)

      # leaky relu
      #f = tf.maximum(0.2 * f, f)

      w_2 = self.weight_variable('w_2', [n_units, n_classes])
      b_2 = self.bias_variable('b_2', [n_classes])

      f = tf.matmul(f, w_2) + b_2
      
    return f

  def loss_cross_entropy(self, y, t):
    cross_entropy = - tf.reduce_mean(tf.reduce_sum(t * tf.log(tf.clip_by_value(y, 1e-10, 1.0)), axis = 1))
    return cross_entropy

  def accuracy(self, y, t):
    correct_preds = tf.equal(tf.argmax(y, axis = 1), tf.argmax(t, axis = 1))
    accuracy = tf.reduce_mean(tf.cast(correct_preds, tf.float32))
    return accuracy

  def training(self, loss, learning_rate, var_list):
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
    train_step = optimizer.minimize(loss, var_list = var_list)
    return train_step

  def fit(self, x_train, x_test, y_train, y_test, \
          n_in, n_units, n_classes, \
          learning_rate, n_iter, batch_size, show_step, is_saving, model_path):

    tf.reset_default_graph()

    x = tf.placeholder(shape = [None, n_in], dtype = tf.float32)
    y = tf.placeholder(shape = [None, n_classes], dtype = tf.float32)
    keep_prob = tf.placeholder(shape = [], dtype = tf.float32)

    logits = self.mlp(x, n_in, n_units, n_classes, keep_prob, reuse = False)
    probs = tf.nn.softmax(logits)
    loss = self.loss_cross_entropy(probs, y)

    var_list = tf.trainable_variables('mlp')

    train_step = self.training(loss, learning_rate, var_list)

    acc =  self.accuracy(probs, y)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with tf.Session() as sess:

      sess.run(init)

      history_loss_train = []
      history_loss_test = []
      history_acc_train = []
      history_acc_test = []

      for i in range(n_iter):
        # Training
        rand_index = np.random.choice(len(x_train), size = batch_size)
        x_batch = x_train[rand_index]
        y_batch = y_train[rand_index]

        feed_dict = {x: x_batch, y: y_batch, keep_prob: 0.7}
        sess.run(train_step, feed_dict = feed_dict)

        temp_loss = sess.run(loss, feed_dict = feed_dict)
        temp_acc = sess.run(acc, feed_dict = feed_dict)

        history_loss_train.append(temp_loss)
        history_acc_train.append(temp_acc)

        if (i + 1) % show_step == 0:
          print ('-' * 100)
          print ('Iteration: ' + str(i + 1) + \
                 '  Loss: ' + str(temp_loss) + '  Accuracy: ' + str(temp_acc))

        # Test
        rand_index = np.random.choice(len(x_test), size = batch_size)
        x_batch = x_test[rand_index]
        y_batch = y_test[rand_index]

        feed_dict = {x: x_batch, y: y_batch, keep_prob: 1.0}
        temp_loss = sess.run(loss, feed_dict = feed_dict)
        temp_acc = sess.run(acc, feed_dict = feed_dict)

        history_loss_test.append(temp_loss)
        history_acc_test.append(temp_acc)
        
      if is_saving:
        model_path = saver.save(sess, model_path)
        print ('done saving at ', model_path)

      print ('-' * 100)    
      fig = plt.figure(figsize = (10, 3))
      ax1 = fig.add_subplot(1, 2, 1)
      ax1.plot(range(n_iter), history_loss_train, 'b-', label = 'Training')
      ax1.plot(range(n_iter), history_loss_test, 'r-', label = 'Test')
      ax1.set_title('Loss')
      ax1.legend(loc = 'upper right')

      ax2 = fig.add_subplot(1, 2, 2)
      ax2.plot(range(n_iter), history_acc_train, 'b-', label = 'Training')
      ax2.plot(range(n_iter), history_acc_test, 'r-', label = 'Test')
      ax2.set_ylim(0.0, 1.0)
      ax2.set_title('Accuracy')
      ax2.legend(loc = 'lower right')

      plt.show()
      
  def predict(self, x_input, n_in, n_units, n_classes, model_path):

    x = tf.placeholder(shape = [None, n_in], dtype = tf.float32)
    keep_prob = tf.placeholder(shape = [], dtype = tf.float32)

    logits = self.mlp(x, n_in, n_units, n_classes, keep_prob, reuse = True)
    probs = tf.nn.softmax(logits)
    preds = tf.argmax(probs, axis = 1)
    
    saver = tf.train.Saver()

    with tf.Session() as sess:

      saver.restore(sess, model_path)

      feed_dict = {x: x_input, keep_prob: 1.0}

      prediction = sess.run(preds, feed_dict = feed_dict)
      
    return prediction
```

##Parameters
```python
n_in = 4
n_units = 64
n_classes = 2
learning_rate = 0.01
n_iter = 500
batch_size = 32
show_step = 100
model_path = 'datalab/model'
```

##Output
```python
is_saving = False

mlp.fit(x_train, x_test, y_train, y_test, \
         n_in, n_units, n_classes, \
         learning_rate, n_iter, batch_size, show_step, is_saving, model_path)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/511c2b0e-ccc4-e495-e486-74da424e99fd.png)


```python
prediction = mlp.predict(x_input, n_in, n_units, n_classes, model_path)

PassengerId = np.array(test['PassengerId']).astype(int)
solution = pd.DataFrame(prediction, PassengerId, columns = ['Survived'])

solution.to_csv('/content/solution.csv', index_label = ['PassengerId'])
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/a233dcae-e641-82f8-3437-fbd89e39021f.png)

##Sample Code 2
```python
train_org = pd.read_csv('/content/train.csv')
test_org = pd.read_csv('/content/test.csv')

train_copy['Cabin_Category'] = train_org['Cabin'].apply(lambda x: 0 if type(x) == float else 1).astype(int)

train_copy['Age'] = train_copy['Age'].fillna(value = 999)
train_copy['Age_Category'] = 8
train_copy.loc[train_copy['Age'] <= 10, 'Age_Category'] = 0
train_copy.loc[(train_copy['Age'] > 10) & (train_copy['Age'] <= 20), 'Age_Category'] = 1
train_copy.loc[(train_copy['Age'] > 20) & (train_copy['Age'] <= 30), 'Age_Category'] = 2
train_copy.loc[(train_copy['Age'] > 30) & (train_copy['Age'] <= 40), 'Age_Category'] = 3
train_copy.loc[(train_copy['Age'] > 40) & (train_copy['Age'] <= 50), 'Age_Category'] = 4
train_copy.loc[(train_copy['Age'] > 50) & (train_copy['Age'] <= 60), 'Age_Category'] = 5
train_copy.loc[(train_copy['Age'] > 60) & (train_copy['Age'] <= 70), 'Age_Category'] = 6
train_copy.loc[(train_copy['Age'] > 70) & (train_copy['Age'] <= 80), 'Age_Category'] = 7
train_copy['Age_Category'] = train_copy['Age_Category'].astype(int)

train_copy['SibSp_Category'] = 4
train_copy.loc[train_copy['SibSp'] == 0, 'SibSp_Category'] = 0
train_copy.loc[train_copy['SibSp'] == 1, 'SibSp_Category'] = 1
train_copy.loc[train_copy['SibSp'] == 2, 'SibSp_Category'] = 2
train_copy.loc[train_copy['SibSp'] == 3, 'SibSp_Category'] = 3
train_copy['SibSp_Category'] = train_copy['SibSp_Category'].astype(int)

train_copy['Parch_Category'] = 4
train_copy.loc[train_copy['Parch'] == 0, 'Parch_Category'] = 0
train_copy.loc[train_copy['Parch'] == 1, 'Parch_Category'] = 1
train_copy.loc[train_copy['Parch'] == 2, 'Parch_Category'] = 2
train_copy.loc[train_copy['Parch'] == 3, 'Parch_Category'] = 3
train_copy['Parch_Category'] = train_copy['Parch_Category'].astype(int)

train_copy['Pclass_Category'] = 2
train_copy.loc[train_copy['Pclass'] == 1, 'Pclass_Category'] = 0
train_copy.loc[train_copy['Pclass'] == 2, 'Pclass_Category'] = 1
train_copy['Pclass_Category'] = train_copy['Pclass_Category'].astype(int)

train_copy['Sex_Category'] = 1
train_copy.loc[train_copy['Sex'] == 'female', 'Sex_Category'] = 0
train_copy['Sex_Category'] = train_copy['Sex_Category'].astype(int)

train_copy['Embarked_Category'] = 0
train_copy.loc[train_copy['Embarked'] == 'S', 'Embarked_Category'] = 0
train_copy.loc[train_copy['Embarked'] == 'C', 'Embarked_Category'] = 1
train_copy.loc[train_copy['Embarked'] == 'Q', 'Embarked_Category'] = 2
train_copy['Embarked_Category'] = train_copy['Embarked_Category'].astype(int)

train_copy['Fare_Category'] = 1
train_copy.loc[train_copy['Fare'] <= 7.9104, 'Fare_Category'] = 0
train_copy.loc[(train_copy['Fare'] > 7.9104) & (train_copy['Fare'] <= 14.4542), 'Fare_Category'] = 1
train_copy.loc[(train_copy['Fare'] > 14.4542) & (train_copy['Fare'] <= 31.0), 'Fare_Category'] = 2
train_copy.loc[train_copy['Fare'] > 31.0, 'Fare_Category'] = 3
train_copy['Fare_Category'] = train_copy['Fare_Category'].astype(int)

drop_elements = ['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', \
                'Fare', 'Cabin', 'Embarked']
train_copy = train_copy.drop(drop_elements, axis = 1)

test_copy['Cabin_Category'] = test_org['Cabin'].apply(lambda x: 0 if type(x) == float else 1).astype(int)

test_copy['Age'] = test_copy['Age'].fillna(value = 999)
test_copy['Age_Category'] = 8
test_copy.loc[test_copy['Age'] <= 10, 'Age_Category'] = 0
test_copy.loc[(test_copy['Age'] > 10) & (test_copy['Age'] <= 20), 'Age_Category'] = 1
test_copy.loc[(test_copy['Age'] > 20) & (test_copy['Age'] <= 30), 'Age_Category'] = 2
test_copy.loc[(test_copy['Age'] > 30) & (test_copy['Age'] <= 40), 'Age_Category'] = 3
test_copy.loc[(test_copy['Age'] > 40) & (test_copy['Age'] <= 50), 'Age_Category'] = 4
test_copy.loc[(test_copy['Age'] > 50) & (test_copy['Age'] <= 60), 'Age_Category'] = 5
test_copy.loc[(test_copy['Age'] > 60) & (test_copy['Age'] <= 70), 'Age_Category'] = 6
test_copy.loc[(test_copy['Age'] > 70) & (test_copy['Age'] <= 80), 'Age_Category'] = 7
test_copy['Age_Category'] = test_copy['Age_Category'].astype(int)

test_copy['SibSp_Category'] = 4
test_copy.loc[test_copy['SibSp'] == 0, 'SibSp_Category'] = 0
test_copy.loc[test_copy['SibSp'] == 1, 'SibSp_Category'] = 1
test_copy.loc[test_copy['SibSp'] == 2, 'SibSp_Category'] = 2
test_copy.loc[test_copy['SibSp'] == 3, 'SibSp_Category'] = 3
test_copy['SibSp_Category'] = test_copy['SibSp_Category'].astype(int)

test_copy['Parch_Category'] = 4
test_copy.loc[test_copy['Parch'] == 0, 'Parch_Category'] = 0
test_copy.loc[test_copy['Parch'] == 1, 'Parch_Category'] = 1
test_copy.loc[test_copy['Parch'] == 2, 'Parch_Category'] = 2
test_copy.loc[test_copy['Parch'] == 3, 'Parch_Category'] = 3
test_copy['Parch_Category'] = test_copy['Parch_Category'].astype(int)

test_copy['Pclass_Category'] = 2
test_copy.loc[test_copy['Pclass'] == 1, 'Pclass_Category'] = 0
test_copy.loc[test_copy['Pclass'] == 2, 'Pclass_Category'] = 1
test_copy['Pclass_Category'] = test_copy['Pclass_Category'].astype(int)

test_copy['Sex_Category'] = 1
test_copy.loc[test_copy['Sex'] == 'female', 'Sex_Category'] = 0
test_copy['Sex_Category'] = test_copy['Sex_Category'].astype(int)

test_copy['Embarked_Category'] = 0
test_copy.loc[test_copy['Embarked'] == 'S', 'Embarked_Category'] = 0
test_copy.loc[test_copy['Embarked'] == 'C', 'Embarked_Category'] = 1
test_copy.loc[test_copy['Embarked'] == 'Q', 'Embarked_Category'] = 2
test_copy['Embarked_Category'] = test_copy['Embarked_Category'].astype(int)

test_copy['Fare_Category'] = 1
test_copy.loc[test_copy['Fare'] <= 7.9104, 'Fare_Category'] = 0
test_copy.loc[(test_copy['Fare'] > 7.9104) & (test_copy['Fare'] <= 14.4542), 'Fare_Category'] = 1
test_copy.loc[(test_copy['Fare'] > 14.4542) & (test_copy['Fare'] <= 31.0), 'Fare_Category'] = 2
test_copy.loc[test_copy['Fare'] > 31.0, 'Fare_Category'] = 3
test_copy['Fare_Category'] = test_copy['Fare_Category'].astype(int)

drop_elements = ['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', \
                'Fare', 'Cabin', 'Embarked']
test_copy = test_copy.drop(drop_elements, axis = 1)
```
 
```python
plt.figure(figsize = (5, 3))
plt.title('Pearson Correlation of Features')
sns.heatmap(train_copy.astype(float).corr(), linewidths = 0.1, linecolor = 'white')
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/1b80554a-e4aa-74b1-4a54-8d0275111122.png)

```python
y_data = np.identity(2)[train_copy['Survived']]
x_data1 = np.identity(2)[train_copy['Cabin_Category']]
x_data2 = np.identity(9)[train_copy['Age_Category']]
x_data3 = np.identity(5)[train_copy['SibSp_Category']]
x_data4 = np.identity(5)[train_copy['Parch_Category']]
x_data5 = np.identity(3)[train_copy['Pclass_Category']]
x_data6 = np.identity(2)[train_copy['Sex_Category']]
x_data7 = np.identity(3)[train_copy['Embarked_Category']]
x_data8 = np.identity(4)[train_copy['Fare_Category']]
x_data = np.concatenate([x_data1, x_data2, x_data3, x_data4, x_data5, x_data6,\
                        x_data7, x_data8], axis = 1)

pred_data1 = np.identity(2)[test_copy['Cabin_Category']]
pred_data2 = np.identity(9)[test_copy['Age_Category']]
pred_data3 = np.identity(5)[test_copy['SibSp_Category']]
pred_data4 = np.identity(5)[test_copy['Parch_Category']]
pred_data5 = np.identity(3)[test_copy['Pclass_Category']]
pred_data6 = np.identity(2)[test_copy['Sex_Category']]
pred_data7 = np.identity(3)[test_copy['Embarked_Category']]
pred_data8 = np.identity(4)[test_copy['Fare_Category']]
pred_data = np.concatenate([pred_data1, pred_data2, pred_data3, pred_data4, \
                            pred_data5, pred_data6, pred_data7, pred_data8], axis = 1)
```

```python
class MLP():
  def __init__(self):
    pass

  def weight_variable(self, name, shape):
    initializer = tf.truncated_normal_initializer(mean = 0.0, stddev = 0.01, dtype = tf.float32)
    return tf.get_variable(name, shape, initializer = initializer)

  def bias_variable(self, name, shape):
    initializer = tf.constant_initializer(value = 0.0, dtype = tf.float32)
    return tf.get_variable(name, shape, initializer = initializer)

  def mlp(self, x, n_in, n_units, n_classes, keep_prob, reuse = False):
    with tf.variable_scope('mlp', reuse = reuse):
      w_1 = self.weight_variable('w_1', [n_in, n_units])
      b_1 = self.bias_variable('b_1', [n_units])

      f = tf.matmul(x, w_1) + b_1

      # batch norm
      #batch_mean, batch_var = tf.nn.moments(f, [0])
      #f = (f - batch_mean) / (tf.sqrt(batch_var) + 1e-10)

      # dropout
      f = tf.nn.dropout(f, keep_prob)

      # relu
      f = tf.nn.relu(f)

      # leaky relu
      #f = tf.maximum(0.2 * f, f)

      w_2 = self.weight_variable('w_2', [n_units, n_classes])
      b_2 = self.bias_variable('b_2', [n_classes])

      f = tf.matmul(f, w_2) + b_2

    return f

  def loss_cross_entropy(self, y, t):
    cross_entropy = - tf.reduce_mean(tf.reduce_sum(t * tf.log(tf.clip_by_value(y, 1e-10, 1.0)), axis = 1))
    return cross_entropy

  def accuracy(self, y, t):
    correct_preds = tf.equal(tf.argmax(y, axis = 1), tf.argmax(t, axis = 1))
    accuracy = tf.reduce_mean(tf.cast(correct_preds, tf.float32))
    return accuracy

  def training(self, loss, learning_rate, var_list):
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
    train_step = optimizer.minimize(loss, var_list = var_list)
    return train_step

  def fit(self, x_data, y_data, n_in, n_units, n_classes, \
          learning_rate, n_epoch, n_iter, batch_size, show_step, \
          is_saving, model_path):

    tf.reset_default_graph()

    x = tf.placeholder(shape = [None, n_in], dtype = tf.float32)
    y = tf.placeholder(shape = [None, n_classes], dtype = tf.float32)
    keep_prob = tf.placeholder(shape = [], dtype = tf.float32)

    logits = self.mlp(x, n_in, n_units, n_classes, keep_prob, reuse = False)
    probs = tf.nn.softmax(logits)
    loss = self.loss_cross_entropy(probs, y)

    var_list = tf.trainable_variables('mlp')

    train_step = self.training(loss, learning_rate, var_list)

    acc =  self.accuracy(probs, y)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with tf.Session() as sess:

      sess.run(init)
      
      for e in range(n_epoch):
        train_indices = np.random.choice(len(x_data), round((len(x_data)*0.8)), \
                                replace = False)
        test_indices = np.array(list(set(range(len(x_data))) - set(train_indices)))
        x_train = x_data[train_indices]
        x_test = x_data[test_indices]
        y_train = y_data[train_indices]
        y_test = y_data[test_indices]
   
        history_loss_train = []
        history_loss_test = []
        history_acc_train = []
        history_acc_test = []
        
        for i in range(n_iter):
          # Training
          rand_index = np.random.choice(len(x_train), size = batch_size)
          x_batch = x_train[rand_index]
          y_batch = y_train[rand_index]

          feed_dict = {x: x_batch, y: y_batch, keep_prob: 0.7}
          sess.run(train_step, feed_dict = feed_dict)

          temp_loss = sess.run(loss, feed_dict = feed_dict)
          temp_acc = sess.run(acc, feed_dict = feed_dict)

          history_loss_train.append(temp_loss)
          history_acc_train.append(temp_acc)

          if (i + 1) % show_step == 0:
            print ('-' * 100)
            print ('Epoch: ' + str(e + 1) + ' Iteration: ' + str(i + 1) + \
                   '  Loss: ' + str(temp_loss) + '  Accuracy: ' + str(temp_acc))

          # Test
          rand_index = np.random.choice(len(x_test), size = batch_size)
          x_batch = x_test[rand_index]
          y_batch = y_test[rand_index]

          feed_dict = {x: x_batch, y: y_batch, keep_prob: 1.0}
          temp_loss = sess.run(loss, feed_dict = feed_dict)
          temp_acc = sess.run(acc, feed_dict = feed_dict)

          history_loss_test.append(temp_loss)
          history_acc_test.append(temp_acc)

        print ('-' * 100)    
        fig = plt.figure(figsize = (10, 3))
        ax1 = fig.add_subplot(1, 2, 1)
        ax1.plot(range(n_iter), history_loss_train, 'b-', label = 'Training')
        ax1.plot(range(n_iter), history_loss_test, 'r-', label = 'Test')
        ax1.set_title('Loss')
        ax1.legend(loc = 'upper right')

        ax2 = fig.add_subplot(1, 2, 2)
        ax2.plot(range(n_iter), history_acc_train, 'b-', label = 'Training')
        ax2.plot(range(n_iter), history_acc_test, 'r-', label = 'Test')
        ax2.set_ylim(0.0, 1.0)
        ax2.set_title('Accuracy')
        ax2.legend(loc = 'lower right')

        plt.show()

      if is_saving:
        model_path = saver.save(sess, model_path)
        print ('done saving at ', model_path)

  def predict(self, x_input, n_in, n_units, n_classes, model_path):

    x = tf.placeholder(shape = [None, n_in], dtype = tf.float32)
    keep_prob = tf.placeholder(shape = [], dtype = tf.float32)

    logits = self.mlp(x, n_in, n_units, n_classes, keep_prob, reuse = True)
    probs = tf.nn.softmax(logits)
    preds = tf.argmax(probs, axis = 1)

    saver = tf.train.Saver()

    with tf.Session() as sess:

      saver.restore(sess, model_path)

      feed_dict = {x: x_input, keep_prob: 1.0}

      prediction = sess.run(preds, feed_dict = feed_dict)

    return prediction
```

##Output 2
```python
mlp = MLP()

n_in = 33
n_units = 64
n_classes = 2
learning_rate = 0.01
n_epoch = 3
n_iter = 100
batch_size = 32
show_step = 100
model_path = 'datalab/model'

is_saving = False

mlp.fit(x_data, y_data, n_in, n_units, n_classes, learning_rate, \
        n_epoch, n_iter, batch_size, show_step, is_saving, model_path)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/dab8ca8f-7a27-f120-3986-6268801d1989.png)

```python
predictions = mlp.predict(pred_data, n_in, n_units, n_classes, model_path)

submission_mlp = pd.DataFrame({ 'PassengerId': PassengerId,
                            'Survived': predictions })
submission_mlp.to_csv("submission_mlp.csv", index=False)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/8fb0c6e9-d994-cd95-0db2-1f0f31efc350.png)