AI Artificial Intelligence

Viele Teile ergeben ein Ganzes! Die Kunst unwichtige Details zu vermeiden!

Grundlegende Tensors








Die Form von Tensors:

  • Shape: The length (number of elements) of each of the axes of a tensor.
  • Rank: Number of tensor axes. A scalar has rank 0, a vector has rank 1, a matrix is rank 2.
  • Axis or Dimension: A particular dimension of a tensor.
  • Size: The total number of items in the tensor, the product shape vector.




Indizes:

  • indexes start at 0
  • negative indices count backwards from the end
  • colons, :, are used for slices: start:stop:step





Die Form des Tensors ändern:




Der gesamte Code:

import tensorflow as tf
print(tf.reduce_sum(tf.random.normal([1000, 1000])))

# Tensor vom Rang 0 - ein Skalar mit nur einem Wert und ohne Achsen
tensor_rang_0 = tf.constant(9)
print(tensor_rang_0)

# Tensor vom Rang 1 ist eine Liste von Werten - mit einer Achse
tensor_rang_1 = tf.constant([3.2, 4.8, 8.4])
print(tensor_rang_1)

# Tensor vom Rang 2 - 2 Achsen
tensor_rang_2 = tf.constant([[1, 2],[3, 4],[5, 6]], dtype=tf.float16)
print(tensor_rang_2)
     
# 3 dimensionaler Tensor mit 3 Achsen
tensor_rang_3 = tf.constant([
  [[0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9]],
  [[10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19]],
  [[20, 21, 22, 23, 24],
   [25, 26, 27, 28, 29]],])

print(tensor_rang_3)

# Tensors mit Rechenoperationen
a = tf.constant([[1, 2],
                 [3, 4]])
b = tf.constant([[1, 1],
                 [1, 1]]) # Could have also said `tf.ones([2,2])`

print(tf.add(a, b), "\n")
print(tf.multiply(a, b), "\n")
print(tf.matmul(a, b), "\n")

# Tensors können in allen Arten von Operationen verwendet werden
c = tf.constant([[4.0, 5.0], [10.0, 1.0]])

# Find the largest value
print(tf.reduce_max(c))
# Find the index of the largest value
print(tf.argmax(c))
# Compute the softmax
print(tf.nn.softmax(c))

# Tensor - Rang 4
rank_4_tensor = tf.zeros([3, 2, 4, 5])
print("Type of every element:", rank_4_tensor.dtype)
print("Number of axes:", rank_4_tensor.ndim)
print("Shape of tensor:", rank_4_tensor.shape)
print("Elements along axis 0 of tensor:", rank_4_tensor.shape[0])
print("Elements along the last axis of tensor:", rank_4_tensor.shape[-1])
print("Total number of elements (3*2*4*5): ", tf.size(rank_4_tensor).numpy())
print(rank_4_tensor)

# Indizes
rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print(rank_1_tensor.numpy())

print("First:", rank_1_tensor[0].numpy())
print("Second:", rank_1_tensor[1].numpy())
print("Last:", rank_1_tensor[-1].numpy())

# Indexing with a : slice keeps the axis:
print("Everything:", rank_1_tensor[:].numpy())
print("Before 4:", rank_1_tensor[:4].numpy())
print("From 4 to the end:", rank_1_tensor[4:].numpy())
print("From 2, before 7:", rank_1_tensor[2:7].numpy())
print("Every other item:", rank_1_tensor[::2].numpy())
print("Reversed:", rank_1_tensor[::-1].numpy())

# Tensors von höherem Rang haben Mehrfach - Indizes
rank_2_tensor = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)
print(rank_2_tensor.numpy())

# Pull out a single value from a 2-rank tensor
print(rank_2_tensor[1, 1].numpy())

# Get row and column tensors
print("Second row:", rank_2_tensor[1, :].numpy())
print("Second column:", rank_2_tensor[:, 1].numpy())
print("Last row:", rank_2_tensor[-1, :].numpy())
print("First item in last column:", rank_2_tensor[0, -1].numpy())
print("Skip the first row:")
print(rank_2_tensor[1:, :].numpy(), "\n")

# Beispiel mit einem 3 - Achsen Tensor
rank_3_tensor = tf.constant([
  [[0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9]],
  [[10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19]],
  [[20, 21, 22, 23, 24],
   [25, 26, 27, 28, 29]],])

print(rank_3_tensor)

print(rank_3_tensor[:, :, 4])

# Shape returns a `TensorShape` object that shows the size along each axis
x = tf.constant([[1], [2], [3]])
print(x.shape)

# You can convert this object into a Python list, too
print(x.shape.as_list())

# Neue Form
reshaped = tf.reshape(x, [1, 3])
print(x.shape)
print(reshaped.shape)

# Beispiel mit einem 3 - Achsen Tensor
rank_3_tensor = tf.constant([
  [[0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9]],
  [[10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19]],
  [[20, 21, 22, 23, 24],
   [25, 26, 27, 28, 29]],])

print(rank_3_tensor)

# A `-1` passed in the `shape` argument says "Whatever fits".
print(tf.reshape(rank_3_tensor, [-1]))

print(tf.reshape(rank_3_tensor, [3*2, 5]), "\n")
print(tf.reshape(rank_3_tensor, [3, -1]))

KI

Copyright © 2022. All Rights Reserved. Ehrenfried Stuhlpfarrer