tensorflow-practice

Common packages for data

1
2
3
4
5
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

Tensorflow Practice

  1. Data set loading and visualization
  2. Data Preprocessing/transforming
  3. Modeling using tf.nn (more basic) / tf.keras (advance and convenient API for modeling)
  4. Performance Visualization
  5. Model saving and reuse, reload
1
2
3
4
5
6
import tensorflow as tf
import tensorflow.keras as keras
# This one-hot convert words to index number rather than binary form
from tensorflow.keras.preprocessing.text import one_hot
#tf.one_hot import text/ labels into binary 0,1 form without compression
from numba import cuda
1
2
# Check if GPU is enabled
print(cuda.gpus)
<Managed Device 0>, <Managed Device 1>

Basic Practise

  1. tf.constant contain types of data. Operation like +, -, *, / , should be applied to data with same types only
1
2
3
4
5
6
7
8
9
10
ls = np.random.randint(0,100,50)

ls= tf.constant(ls)
x1= tf.constant(1)
x2= tf.constant(2)
x4= tf.constant(5)
x3 = x1+x2*x4
tf.print(x3,[x1,x2])
tf.print(tf.argsort(ls))
c = tf.argsort(ls)
11 [1, 2]
[32 24 27 ... 20 40 49]

Practice with Text+Numeric type data: titanic data

1
2
3
4
5
6
7
8
TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"
TEST_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/eval.csv"

# The first param in get_file: the name of dataset to save
# The second param: url to the dataset
trainset_path = keras.utils.get_file('titanic_train.csv', TRAIN_DATA_URL)
testset_path = keras.utils.get_file('titanic_test.csv', TEST_DATA_URL)
trainset_path, testset_path
Downloading data from https://storage.googleapis.com/tf-datasets/titanic/train.csv
32768/30874 [===============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tf-datasets/titanic/eval.csv
16384/13049 [=====================================] - 0s 0us/step





('/home/wenkanw/.keras/datasets/titanic_train.csv',
 '/home/wenkanw/.keras/datasets/titanic_test.csv')
1
2
3
# Load CSV file with pandas
trainset = pd.read_csv(trainset_path)
testset = pd.read_csv(testset_path)
1
trainset.head()

survived sex age n_siblings_spouses parch fare class deck embark_town alone
0 0 male 22.0 1 0 7.2500 Third unknown Southampton n
1 1 female 38.0 1 0 71.2833 First C Cherbourg n
2 1 female 26.0 0 0 7.9250 Third unknown Southampton y
3 1 female 35.0 1 0 53.1000 First C Southampton n
4 0 male 28.0 0 0 8.4583 Third unknown Queenstown y
1
trainset.groupby('sex').head()

survived sex age n_siblings_spouses parch fare class deck embark_town alone
0 0 male 22.0 1 0 7.2500 Third unknown Southampton n
1 1 female 38.0 1 0 71.2833 First C Cherbourg n
2 1 female 26.0 0 0 7.9250 Third unknown Southampton y
3 1 female 35.0 1 0 53.1000 First C Southampton n
4 0 male 28.0 0 0 8.4583 Third unknown Queenstown y
5 0 male 2.0 3 1 21.0750 Third unknown Southampton n
6 1 female 27.0 0 2 11.1333 Third unknown Southampton n
7 1 female 14.0 1 0 30.0708 Second unknown Cherbourg n
9 0 male 20.0 0 0 8.0500 Third unknown Southampton y
10 0 male 39.0 1 5 31.2750 Third unknown Southampton n

Comments