text_plain_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import random

import tensorflow as tf

import tensorflow as tf

features = ['Pclass_1', 'Pclass_2', 'Pclass_3', 'Sex_female', 'Age', 'SibSp', 'Parch', 'Fare']
df = pd.read_csv('../input/train.csv')
t = pd.DataFrame({'Validation': list(map(lambda x: random.random() < 0.3, range(891)))})
x_preprocess = pd.DataFrame({'Pclass_1': (df['Pclass'] == 1) * 1, 'Pclass_2': (df['Pclass'] == 2) * 1, 'Pclass_3': (df['Pclass'] == 3) * 1, 'Sex_female': (df['Sex'] == 'female') * 1, 'Age': df['Age'] / pd.Series.std(df['Age']), 'SibSp': df['SibSp'], 'Parch': df['Parch'], 'Fare': df['Fare'] / pd.Series.std(df['Fare'])})
x_preprocess.fillna(0, inplace=True)
y = pd.DataFrame({'Survived': df['Survived']})
y_train = y[t['Validation'] == False]
x_train = x_preprocess[t['Validation'] == False]
y_validation = y[t['Validation']]
x_validation = x_preprocess[t['Validation']]

x = tf.placeholder(dtype=tf.float32, shape=[None, len(features)])
y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
h1_feature = 32
h2_feature = 32
epic_max = 1000
mini_batch = 64
W1 = tf.Variable(tf.truncated_normal(shape=[len(features), h1_feature], stddev=0.1))
W2 = tf.Variable(tf.truncated_normal(shape=[h1_feature, h2_feature], stddev=0.1))
W3 = tf.Variable(tf.truncated_normal(shape=[h2_feature, 1], stddev=0.1))
b1 = tf.Variable(tf.ones(shape=[1, h1_feature]))
b2 = tf.Variable(tf.ones(shape=[1, h2_feature]))
b3 = tf.Variable(tf.ones(shape=[1, 1]))
init = tf.global_variables_initializer()
x_dropout = tf.nn.dropout(x, 0.5)
h1_relu = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
h2_relu = tf.nn.sigmoid(tf.matmul(h1_relu, W2) + b2)
h3_sigmoid = tf.nn.sigmoid(tf.matmul(h2_relu, W3) + b3)
cross_entropy = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=h3_sigmoid, logits=y))
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cross_entropy)
accuracy = tf.reduce_mean(tf.cast(tf.equal(h3_sigmoid, y), tf.float32))
with tf.Session() as sess:
    sess.run(init)
    feed_train = {x: x_train[features].values, y: y_train[['Survived']].values}
    feed_validation = {x: x_validation[features].values, y: y_validation[['Survived']].values}
    for j in range(100):
        for i in range(100):
            sess.run(train_step, feed_dict=feed_train)

features = ['Pclass_1', 'Pclass_2', 'Pclass_3', 'Sex_female', 'Age', 'SibSp', 'Parch', 'Fare']
df = pd.read_csv('../input/train.csv')
t = pd.DataFrame({'Validation': list(map(lambda x: random.random() < 0.3, range(891)))})
x = pd.DataFrame({'Pclass_1': (df['Pclass'] == 1) * 1, 'Pclass_2': (df['Pclass'] == 2) * 1, 'Pclass_3': (df['Pclass'] == 3) * 1, 'Sex_female': (df['Sex'] == 'female') * 1, 'Age': df['Age'] / pd.Series.std(df['Age']), 'SibSp': df['SibSp'], 'Parch': df['Parch'], 'Fare': df['Fare'] / pd.Series.std(df['Fare'])})
x.fillna(0, inplace=True)
y = pd.DataFrame({'Survived': df['Survived']})
y_train = y[t['Validation'] == False]
x_train = x[t['Validation'] == False]
y_validation = y[t['Validation']]
x_validation = x[t['Validation']]

x = tf.placeholder(dtype=tf.float32, shape=[None, len(features)])
y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
h1_feature = 32
h2_feature = 32
epic_max = 1000
mini_batch = 64
W1 = tf.Variable(tf.truncated_normal(shape=[len(features), h1_feature], stddev=0.1))
W2 = tf.Variable(tf.truncated_normal(shape=[h1_feature, h2_feature], stddev=0.1))
W3 = tf.Variable(tf.truncated_normal(shape=[h2_feature, 1], stddev=0.01))
b1 = tf.Variable(tf.zeros(shape=[1, h1_feature]))
b2 = tf.Variable(tf.zeros(shape=[1, h2_feature]))
b3 = tf.Variable(tf.zeros(shape=[1, 1]))
x_dropout = tf.nn.dropout(x, 0.5)
h1_tanh = tf.nn.tanh(tf.matmul(x, W1) + b1)
h2_tanh = tf.nn.tanh(tf.matmul(h1_tanh, W2) + b2)
h3_sigmoid = tf.nn.sigmoid(tf.matmul(h2_tanh, W3) + b3)
cross_entropy = -tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=h3_sigmoid, logits=y))
train_step = tf.train.AdamOptimizer(learning_rate=0.5).minimize(cross_entropy)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(h3_sigmoid), y), tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    feed_train = {x: x_train[features].values, y: y_train[['Survived']].values}
    feed_validation = {x: x_validation[features].values, y: y_validation[['Survived']].values}
    print(sess.run([cross_entropy, accuracy], feed_dict=feed_train))
    print(sess.run([cross_entropy, accuracy], feed_dict=feed_validation))
    for j in range(10):
        for i in range(1000):
            sess.run(train_step, feed_dict=feed_train)
        print(sess.run([cross_entropy, accuracy], feed_dict=feed_train))
        print(sess.run([cross_entropy, accuracy], feed_dict=feed_validation))