TensorFlow Basic

Ryan
2 min readJul 29, 2021
TensorFlow by Google

What is TensorFlow?

TensorFlow is open-source software library developed by Google Brain team for deep neural networks. November 2015 created with Apache 2.0 license, currently countless commits from over 2900 contributors.

Tensor?

All data used in Tensorflow is structured via tensor structor. A tensor is a vector or matrix of n-dimensions that represents all types of data. All values in a tensor hold identical data type with a known (or partially known) shape. The shape of the data is the dimensionality of the matrix or array.

Each tensor has rank (dimension), shape, data type, rank can be dynamic.

╔══════╦══════════════════╦════════════════════════════════════════╗
║ Rank ║ Numerical entity ║ Examples ║
╠══════╬══════════════════╬════════════════════════════════════════╣
║ 0 ║ Scalar ║ s = 123 ║
║ 1 ║ Vector ║ v = [1.1, 2.2, 3.3] ║
║ 2 ║ Matrix ║ m = [[1, 2, 3], [4, 5, 6]] ║
║ 3 ║ 3-tensor ║ t = [[[1], [2], [3]], [[4], [5], [6]]] ║
║ n ║ n-tensor ║ ... ║
╚══════╩══════════════════╩════════════════════════════════════════╝

Solving simple linear regression using Tensorflow

We can try basic linear regression problem using Tensorflow

Linear model

When you have y = ax + b and given values of x, y as below, it’s not hard to assume a and b. a = 1, b = 1

╔═══╦═══╦═══╦═══╦═══╗
║ x ║ 1 ║ 2 ║ 3 ║ 4 ║
╠═══╬═══╬═══╬═══╬═══╣
║ y ║ 2 ║ 3 ║ 4 ║ 5 ║
╚═══╩═══╩═══╩═══╩═══╝

Solving linear regression with machine learning means finding a and b from y = ax + b. In machine learning we often use h(x) = wx + b, not so much different, just finding w (weight) and b (bias). Machine using given data and with hypothesis h(x) finding optimal w and b is called learning, if w and b values are found, with any random value x can be used to inference value y.

You can see our model is finding right weight and bias with given hypothesis each step has passed.


step | w| b| cost
-----|----|----|-----
1| 0.78| 0.72| 1.62
step | w| b| cost
-----|----|----|-----
3| 0.90| 0.76| 0.55
step | w| b| cost
-----|----|----|-----
100| 1.04| 0.86| 0.00
step | w| b| cost
-----|----|----|-----
1100| 1.00| 1.00| 0.00

--

--

Ryan

iOS engineer & data science enthusiast. Into algorithmic trading. https://github.com/Rsych