TensorFlow #2 - Getting started
Tags: datascience, python, machinelearning, tutorial
Published: 2020-07-11 08:22:22

It's second post from TensorFlow 101 series, so I assume you installed conda,
tensorflow and jupyter (preferably jupyterlab).

## First Jupyter Notebook
Let's create our first Jupyter notebook. To do it we need to start Jupyter
server. We can do it with two commands. You can start with JupyterLab enabled or
not.

If you want to use lab you just have to run:
```bash
jupyter lab
```
and without it:
```bash
jupyter notebook
```

If you're using PyCharm or VS Code you don't have to launch it manually from
terminal. You can run it inside the IDE/editor.

***Protip:*** if you're using PyCharm - Scientific view is very useful. It
displays IPython REPL, all your variables, documentation and your notebook at the
same time.

After you launched Jupyter we can create new notebook. It can be created in bare
Jupyter in the `New` menu on the right.

![Bare Jupyter](https://dev-to-uploads.s3.amazonaws.com/i/d9jue3rld1006nu6y9og.png)
For JupyterLab it should be visible inside `Launcher` in `Notebook` category.

![JupyterLab Launcher](https://dev-to-uploads.s3.amazonaws.com/i/btquyszxt2r2tdqsvhml.png)

Otherwise you can access it in the menu bar under `File>New>Notebook` option.

![JupyterLab Menu](https://dev-to-uploads.s3.amazonaws.com/i/kdofjt4cvm0w0u1b780f.png)

For PyCharm you can create it with `RMB` on the file list, with `file` menu or
`alt+insert` keybind.

## What can we do in here?

Basically any correct code in Python should run just fine. Jupyter Notebooks are
superior to regular scripts, because you can run your code just partially with
blocks. It also allows to create comment blocks with markdown.

As you can see in the picture - this code works perfectly fine if you launch
block `[3]` before `[5]`, but there's a catch. If you run all your notebook at
once - it'll throw an error because `a` is declared _AFTER_ `print(a)`.

![Exammple #1 - running block at the bottom first](https://dev-to-uploads.s3.amazonaws.com/i/atuz7rphm8asq64jme6p.png)
![Exammple #2 - running block at the bottom second](https://dev-to-uploads.s3.amazonaws.com/i/u1s5udfbj2eizgrkxdco.png)

## Tensorflow basics

Fist we have to import tensorflow. It's usually aliased as `tf`.
```python
import tensorflow as tf
```

Now we can check installed version and check if GPU is available. Of course if
you installed CPU-only version you won't be able to access GPU.
```python
print(tf.__version__)
print(tf.config.list_physical_devices("GPU"))
```
I've done that in separate blocks, so for me it looks like this.

![Chcecks](https://dev-to-uploads.s3.amazonaws.com/i/imtik9i7ft0muerciijf.png)

Now we can create our first tensors (but not flow!)

### Creating tensors

We can create tensor of ones with
```python
tf.ones([2,3,4])
```

Its argument is list of dimensions. In this example tensor has dimensions 2x3x4.
It's very similar to nested lists in python. Let's assume tensor is (for now) 
a list - so it contains 2 lists with 3 sub-lists and 4 elements.

By default TF uses float32 type. Yes, it's important to understand how does the
type work. Every tensor can have like float16 (half precision), float32 (single
precision), float64 (double precision), integers with proper size, strings,
complex numbers, and many others. There's one a bit special - bfloat16 -
this one is a bit more tricky, because it's not IEEE 754 compatible, It has
8-bit exponent and 7 bit fraction. Half precision floats contain 5b exponent
and 10b fraction, but use case is a bit different.

There are more functions like `tf.ones()`, but the most important one is just
`tf.constant()`. With this one we can create any tensor containing constant
values. There's also `tf.Variable()`, where our values are, of course, variable.

That's all for now folks. If you've got any questions or something is unclear
feel free to ask. I'll do my best to explain everything.

_Stay tuned!_

PS. You can also subscribe to my [newsletter](https://tinyletter.com/pgronkievitz)