Tensor Flow
Tensor Flow
import tensorflow as tf
print(tf.__version__)
OUTPUT:
2.9.2
First create a scalar using tf.constant.
scalar = tf.constant(7)
print(scalar)
print(scalar.ndim)
OUTPUT:
tf.Tensor(7, shape=(), dtype=int32)
0
Create a vector
vector = tf.constant([10,10])
print(vector)
print(vector.ndim)
OUTPUT:
tf.Tensor([10 10], shape=(2,), dtype=int32)
1
Creating a Matrix
matrix = tf.constant([
[10,11],
[12,13]
])
print(matrix)
print(matrix.ndim)
OUTPUT:
tf.Tensor(
[[10 11]
[12 13]], shape=(2, 2), dtype=int32)
2
basic_tensor = tf.constant([[10,11],[12,13]])
print(basic_tensor)
OUTPUT:
tf.Tensor(
[[10 11]
[12 13]], shape=(2, 2), dtype=int32)
OUTPUT:
tf.Tensor(
[[20 21]
[22 23]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[0 1]
[2 3]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[100 110]
[120 130]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[1. 1.1]
[1.2 1.3]], shape=(2, 2), dtype=float64)
Matrix Multiplication
tensor_011 = tf.constant([[2,2],[4,4]])
tensor_012 = tf.constant([[2,3],[4,5]])
print(tf.matmul(tensor_011,tensor_012))
OUTPUT:
tf.Tensor(
[[12 16]
[24 32]], shape=(2, 2), dtype=int32)
OUTPUT:
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(9.0, shape=(), dtype=float32)
tf.Tensor(45.0, shape=(), dtype=float32)
Find the square, square root, and log of each value in a tensor.
print(tf.sqrt(tensor_013))
print(tf.square(tensor_013))
print(tf.math.log(tensor_013))
OUTPUT:
tf.Tensor(
[[1. 1.4142135 1.7320508]
[2. 2.236068 2.4494898]
[2.6457512 2.828427 3. ]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[ 1. 4. 9.]
[16. 25. 36.]
[49. 64. 81.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[0. 0.6931472 1.0986123]
[1.3862944 1.609438 1.7917595]
[1.9459102 2.0794415 2.1972246]], shape=(3, 3), dtype=float32)
Conclusion
Tensorflow is a powerful library to build deep-learning models. It
has all the tools we need to construct neural networks to solve
problems like image classification, sentiment analysis, stock
market predictions, etc.