tensorflow / tensorflow/probability

Probabilistic Neural Network- MICHAEL POLLIND https://www.kaggle.com/code/mpollind/titanic-data-probabilistic-neural-network/notebook

Open
#1,661 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hello, I am new to Tensorflow and machine learning in general and am actually mechanically trained. I am attempting to implement a PNN for thermodynamic predictions.

I have managed to modify this code by Michael Pollind from Kaggle for the Titanic competition and take no responsibility for this work.

I have managed to get good prediction results (90% in our field is impressive with experimental error), however I cannot understand how I would get the trained script to run with a new array of data.

Could anyone please advise?

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.model_selection import train_test_split
from ipywidgets import interact, interactive, fixed, interact_manual,FloatSlider

Read the CSV input file and show first 5 rows

df_train = pd.read_csv('../input/train.csv')
df_train.head(5)

We can't do anything with the Name, Ticket number, and Cabin, so we drop them.

df_train = df_train.drop(['PassengerId','Name','Ticket', 'Cabin'], axis=1)

To make 'Sex' numeric, we replace 'female' by 0 and 'male' by 1

df_train['Sex'] = df_train['Sex'].map({'female':0, 'male':1}).astype(int)

We replace 'Embarked' by three dummy variables 'Embarked_S', 'Embarked_C', and 'Embarked Q',

which are 1 if the person embarked there, and 0 otherwise.

df_train = pd.concat([df_train, pd.get_dummies(df_train['Embarked'], prefix='Embarked')], axis=1)
df_train = df_train.drop('Embarked', axis=1)

We normalize the age and the fare by subtracting their mean and dividing by the standard deviation

age_mean = df_train['Age'].mean()
age_std = df_train['Age'].std()
df_train['Age'] = (df_train['Age'] - age_mean) / age_std

fare_mean = df_train['Fare'].mean()
fare_std = df_train['Fare'].std()
df_train['Fare'] = (df_train['Fare'] - fare_mean) / fare_std

In many cases, the 'Age' is missing - which can cause problems. Let's look how bad it is:

print("Number of missing 'Age' values: {:d}".format(df_train['Age'].isnull().sum()))

A simple method to handle these missing values is to replace them by the mean age.

df_train['Age'] = df_train['Age'].fillna(df_train['Age'].mean())
Number of missing 'Age' values: 177

With that, we're almost ready for training

df_train.head()

Finally, we convert the Pandas dataframe to a NumPy array, and split it into a training and test set

x_train = df_train.drop('Survived', axis=1).values
y_train = [[(value == i) * 1 for i in range(0,2)] for value in df_train['Survived'].values]

x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2)
x_train = np.array(x_train)
x_test = np.array(x_test)
y_train = np.array(y_train)
y_test = np.array(y_test)

uniform_tf = lambda x: (tf.math.abs(x) <= 1) and 1/2 or 0

triangle_tf = lambda x: (np.abs(x) <= 1) and (1 - np.abs(x)) or 0

gaussian_tf = lambda x: (1.0/tf.sqrt(2np.pi)) tf.exp(-.5*x**2)
def _pattern(input,name,feature_count,h):
with tf.variable_scope(name) as scope:
bias = tf.get_variable('bias',[feature_count, 1],initializer=tf.constant_initializer(0))
bandwidth = tf.constant(1.0/(h * feature_count))
return tf.multiply(tf.reduce_sum(tf.map_fn(lambda x: (gaussian_tf(x)/h),input + tf.transpose(bias)),axis=1),bandwidth)
tf.reset_default_graph()

N number of traning example with a 28*28 size image

inputs = tf.placeholder(tf.float32, shape=(None,x_train.shape[1]), name='inputs')

0-2 survived or perished

labels = tf.placeholder(tf.float32, shape=(None, 2), name='labels')

survive = _pattern(inputs,'survived',x_train.shape[1],.2)
perished = _pattern(inputs,'perished',x_train.shape[1],.2)
result = tf.stack([survive,perished],axis=1)

Loss function and optimizer

lr = tf.placeholder(tf.float32, shape=(), name='learning_rate')
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=result, labels=labels))
optimizer = tf.train.AdamOptimizer(lr).minimize(loss)

Prediction

pred_label = tf.argmax(result,1)
correct_prediction = tf.equal(pred_label, tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

Configure GPU not to use all memory

config = tf.ConfigProto()
config.gpu_options.allow_growth = True

Start a new tensorflow session and initialize variables

sess = tf.InteractiveSession(config=config)
sess.run(tf.global_variables_initializer())

This is the main training loop: we train for 50 epochs with a learning rate of 0.05 and another

50 epochs with a smaller learning rate of 0.01

performance = []
for learning_rate in [0.05, 0.01]:
for epoch in range(200):
avg_cost = 0.0

    # For each epoch, we go through all the samples we have.
    for i in range(0,x_train.shape[0]):
        # Finally, this is where the magic happens: run our optimizer, feed the current example into X and the current target into Y
        _, c = sess.run([optimizer, loss], feed_dict={lr:learning_rate, 
                                                      inputs: [x_train[i]],
                                                      labels: [y_train[i]]})
        avg_cost += c
    avg_cost /= x_train.shape[0]    
    performance += [accuracy.eval(feed_dict={inputs: x_test, labels: y_test})]
    
    # Print the cost in this epcho to the console.
    if epoch % 10 == 0:
        print("Epoch: {:3d}    Train Cost: {:.4f}".format(epoch, avg_cost))

Epoch: 0 Train Cost: 0.5289
Epoch: 10 Train Cost: 0.5086
Epoch: 20 Train Cost: 0.5089
Epoch: 30 Train Cost: 0.5089
Epoch: 40 Train Cost: 0.5089
Epoch: 50 Train Cost: 0.5089
Epoch: 60 Train Cost: 0.5089
Epoch: 70 Train Cost: 0.5089
Epoch: 80 Train Cost: 0.5089
Epoch: 90 Train Cost: 0.5089
Epoch: 100 Train Cost: 0.5089
Epoch: 110 Train Cost: 0.5089
Epoch: 120 Train Cost: 0.5089
Epoch: 130 Train Cost: 0.5089
Epoch: 140 Train Cost: 0.5089
Epoch: 150 Train Cost: 0.5089
Epoch: 160 Train Cost: 0.5089
Epoch: 170 Train Cost: 0.5089
Epoch: 180 Train Cost: 0.5089
Epoch: 190 Train Cost: 0.5089
Epoch: 0 Train Cost: 0.4881
Epoch: 10 Train Cost: 0.4801
Epoch: 20 Train Cost: 0.4774
Epoch: 30 Train Cost: 0.4728
Epoch: 40 Train Cost: 0.4722
Epoch: 50 Train Cost: 0.4719
Epoch: 60 Train Cost: 0.4715
Epoch: 70 Train Cost: 0.4713
Epoch: 80 Train Cost: 0.4710
Epoch: 90 Train Cost: 0.4702
Epoch: 100 Train Cost: 0.4698
Epoch: 110 Train Cost: 0.4697
Epoch: 120 Train Cost: 0.4696
Epoch: 130 Train Cost: 0.4696
Epoch: 140 Train Cost: 0.4696
Epoch: 150 Train Cost: 0.4696
Epoch: 160 Train Cost: 0.4695
Epoch: 170 Train Cost: 0.4695
Epoch: 180 Train Cost: 0.4695
Epoch: 190 Train Cost: 0.4695
acc_train = accuracy.eval(feed_dict={inputs: x_train, labels: y_train})
print("Train accuracy: {:3.2f}%".format(acc_train*100.0))

acc_test = accuracy.eval(feed_dict={inputs: x_test, labels: y_test})
print("Test accuracy: {:3.2f}%".format(acc_test*100.0))

Once again, I take no responsibility for this work. It is a very good solution to a similar problem than my own.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the notebook's preprocessing block and the inputs placeholder, then inspect how sess.run is used during training. Determine how a new array would receive the same preprocessing and be passed to the trained session; done means producing predictions for new data without retraining.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, numpy, pandas, python
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.