🐬

【TF Tutorial】Chapter 2: Basics of TensorFlow

2024/07/15に公開

1. Tensors: Creation, Manipulation, and Operations

1.1 Tensor

Tensors are the central data structure in TensorFlow. They are multi-dimensional arrays with a uniform type (e.g., float32, int32).

1.2 Creation / Manipulation

・Tensor

import tensorflow as tf
t1 = tf.constant([1, 2, 3])
t2 = tf.constant([[1, 2], [3, 4]])
t3 = tf.zeros([3, 3])
t4 = tf.ones([3, 3])
t5 = tf.random.normal([3, 3], mean=0, stddev=1.0)
t6 = tf.reshape(t2, [4, 1])
t7 = t2[:, 1]  # Extracting the second column
t8 = tf.concat([t1, t1], axis=0)
t9 = t1 + t1
t10 = tf.multiply(t1, t1)
t11 = tf.matmul(t2, tf.transpose(t2))

# output
# t1:
# tf.Tensor([1 2 3], shape=(3,), dtype=int32)
# t2:
# tf.Tensor(
# [[1 2]
#  [3 4]], shape=(2, 2), dtype=int32)
# t3:
# tf.Tensor(
# [[0. 0. 0.]
#  [0. 0. 0.]
#  [0. 0. 0.]], shape=(3, 3), dtype=float32)
# t4:
# tf.Tensor(
# [[1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]], shape=(3, 3), dtype=float32)
# t5:
# tf.Tensor(
# [[ 1.0311382   0.02289583 -0.31852403]
#  [ 0.7167651  -0.27280018 -0.35337386]
#  [ 0.6389093  -0.1269994   0.03694919]], shape=(3, 3), dtype=float32)
# t6:
# tf.Tensor(
# [[1]
#  [2]
#  [3]
#  [4]], shape=(4, 1), dtype=int32)
# t7:
# tf.Tensor([2 4], shape=(2,), dtype=int32)
# t8:
# tf.Tensor([1 2 3 1 2 3], shape=(6,), dtype=int32)
# t9:
# tf.Tensor([2 4 6], shape=(3,), dtype=int32)
# t10:
# tf.Tensor([1 4 9], shape=(3,), dtype=int32)
# t11:
# tf.Tensor(
# [[ 5 11]
#  [11 25]], shape=(2, 2), dtype=int32)

・Variable
Variable is a part of tensor and mutable values.

# define
v1 = tf.Variable(3.0)
v2 = tf.Variable(tf.random.normal([3, 3]))
# assign
v1.assign(5.0) # overwrite
v2.assign_add(tf.ones([3, 3])) # add

# output
# v1:
#  <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.0> 
# v2:
#  <tf.Variable 'Variable:0' shape=(3, 3) dtype=float32, numpy=
# array([[ 0.5109919 , -0.92320675, -0.39161137],
#        [-0.95992166,  0.49418753,  0.12367737],
#        [-0.06705742,  0.26853216,  0.22966667]], dtype=float32)> 
# v1:
#  <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=5.0> 
# v2:
#  <tf.Variable 'Variable:0' shape=(3, 3) dtype=float32, numpy=
# array([[1.5109918 , 0.07679325, 0.60838866],
#        [0.04007834, 1.4941876 , 1.1236774 ],
#        [0.93294257, 1.2685322 , 1.2296667 ]], dtype=float32)> 

・Constants
Constants is a part of tensor and immutable values.

c1 = tf.constant(3.0)
c2 = tf.constant([1, 2, 3])

# output
# c1:
# tf.Tensor(3.0, shape=(), dtype=float32)
# c2:
# tf.Tensor([1 2 3], shape=(3,), dtype=int32)

1.3 Basic Operations

result = tf.add(t1, t1)
result = tf.subtract(t1, t1)
result = tf.multiply(t1, t1)
result = tf.divide(t1, t1)
result = tf.pow(t1, 2)
result = tf.sqrt(tf.cast(t1, tf.float32))

# output
# add:
# tf.Tensor([2 4 6], shape=(3,), dtype=int32)
# subtract:
# tf.Tensor([0 0 0], shape=(3,), dtype=int32)
# multiply:
# tf.Tensor([1 4 9], shape=(3,), dtype=int32)
# divide:
# tf.Tensor([1. 1. 1.], shape=(3,), dtype=float64)
# pow:
# tf.Tensor([1 4 9], shape=(3,), dtype=int32)
# sqrt:
# tf.Tensor([1.        1.4142135 1.7320508], shape=(3,), dtype=float32)

2. TensorFlow's Computational Graph

A computational graph in TensorFlow represents a series of operations as nodes in a graph, where the edges represent the data (tensors) flowing between them. This graph-based approach allows TensorFlow to optimize the computation and execute it efficiently across different devices like CPUs, GPUs, and TPUs.

Basic Concepts:
・Nodes: Represent operations (e.g., addition, multiplication) or variables/constants.
・Edges: Represent the data (tensors) that flow between operations.

Discussion