Tensorflow
Machine Learning
Artificial Intelligence: The effort to automate intellectual tasks normally performed by humans.
Machine Learning: Rather than giving the program the rules, an algorithm finds the rules for us.
Neural Network: A form of machine learning that uses a layered representation of data.
What is a Tensor?
- A generalization of vectors and matrices to potentially higher dimensions. Internally, Tensorflow represents tensors as n-dimensional arrays of base datatypes.
- Each tensor represents a partially defined computation that will eventually produce a value.
Creating Tensors
string = tf.Variable("this is a string", tf.string)
number = tf.Variable(324, tf.int16)
floating = tf.Variable(3.567, tf.float64)
What we created above is a tensor of rank 0, also known as a scalar.
Rank/Degree of Tensors
rank1_tensor = tf.Variable(["Test"], tf.string)
rank2_tensor = tf.Variable([["test", "ok"], ["test", "yes"]], tf.string)tf.rank(rank2_tensor)
>>> <tf.Tensor: shape(), dtype=int32, numpy=2>
Shape of Tensors
rank2_tensor.shape
>>> TensorShape([2, 2])
Changing Shape
# tf.ones() creates a shape [1,2,3] tensor full of ones
tensor1 = tf.ones([1,2,3])tensor2 = tf.reshape(tensor1, [2,3,1])# -1 tells the tensor to calculate the size of the dimension in that place -> will reshape the tensor to [3,2]
tensor3 = tf.reshape(tensor2, [3, -1])# The number of elements in the reshaped tensor MUST match the number in the originalprint(tensor1)
>>> tf.Tensor(
[[[1. 1. 1.]
[1. 1. 1.]]], shape=(1, 2, 3), dtype=float32)
Types of Tensors
- Variable -> mutable
- Constant -> immutable
- Placeholder
- SparseTensor
Tensorflow Core Learning Algorithm
Linear Regression
- Import
from __future__ import absolute_import, division, print_function, unicode_literalsimport numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllibimport tensorflow.compat.v2.feature_column as fc
- Data
# Load datasetdftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')
- Visualization
dftrain.head()

dftrain.age.hist(bins=20)

The majority of passengers are in their 20’s or 30's.
dftrain.sex.value_counts().plot(kind='barh')

The majority of passengers are male.
dftrain['class'].value_counts().plot(kind='barh')

The majority of passengers are in the “Third” class.
pd.concat([dftrain, y_train], axis = 1).groupby('sex').survived.mean().plot(kind='barh').set_xlabel('% survive')

Females have a much higher chance of survival.
Training vs. Testing Data
- Training: what we feed to the model so that it can develop and learn (much larger size than the testing data).
- Testing: what we use to evaluate the model and see how well it’s performing. We must use a separate set of data that the model has not been trained on to evaluate it.