338 lecturi
338 lecturi

Nu există TensorFlow fără tensiuni

Prea lung; A citi

Tensorii sunt arate multidimensionale în centrul TensorFlow, permițând reprezentarea și manipularea eficientă a datelor. Acest ghid acoperă crearea de tensori, operațiuni și concepte avansate, cum ar fi difuzarea și ragged tensori, oferind o înțelegere cuprinzătoare pentru practicanții învățării automate.
featured image - Nu există TensorFlow fără tensiuni
Tensor Flow - [Technical Documentation] HackerNoon profile picture
0-item

Conținut Overview

  • de bază
  • Despre forme
  • Indexări
  • Manipularea formelor
  • Mai multe despre DTypes
  • transmisiune
  • tf.convert_to în tensor
  • Tenzori răsucite
  • Tensiuni stricte
  • Scăderea tensiunilor

Tenzorii sunt matrice multidimensionale cu un tip uniform (numit adtypePuteți vedea toate sprijinitedtypeslatf.dtypes.

Dacă eşti familiarizat cuNumărTensiunile sunt canp.arrays.

Toți tensorii sunt neschimbători, la fel ca numerele și șirurile Python: nu puteți niciodată să actualizați conținutul unui tensor, doar să creați unul nou.

import tensorflow as tf
import numpy as np

2024-08-15 03:05:18.327501: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-08-15 03:05:18.348450: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-08-15 03:05:18.354825: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered

de bază

În primul rând, creați niște tensiuni de bază.

Aici este un tensor "scalar" sau "rank-0".Un scalar conține o singură valoare și nu conține "axe".

# This will be an int32 tensor by default; see "dtypes" below.
rank_0_tensor = tf.constant(4)
print(rank_0_tensor)

tf.Tensor(4, shape=(), dtype=int32)
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1723691120.932442  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.936343  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.940040  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.943264  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.954872  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.958376  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.961894  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.964843  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.967730  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.971300  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.974711  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691120.977717  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.208679  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.210786  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.212791  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.214776  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.216798  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.218734  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.220650  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.222554  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.224486  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.226429  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.228329  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.230251  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.269036  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.271069  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.273006  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.274956  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.276917  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.278854  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.280754  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.282664  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.284613  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.287058  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.289508  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723691122.291891  176945 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355


Un tensor "vector" sau "rank-1" este ca o listă de valori.

# Let's make this a float tensor.
rank_1_tensor = tf.constant([2.0, 3.0, 4.0])
print(rank_1_tensor)
tf.Tensor([2. 3. 4.], shape=(3,), dtype=float32)


A "matrix" or "rank-2" tensor has two axes:

# If you want to be specific, you can set the dtype (see below) at creation time
rank_2_tensor = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)
print(rank_2_tensor)
tf.Tensor(
[[1. 2.]
 [3. 4.]
 [5. 6.]], shape=(3, 2), dtype=float16)

A scalar, shape: []

A vector, shape: [3]

A matrix, shape: [3, 2]

A scalar, the number 4

The line with 3 sections, each one containing a number.

A 3x2 grid, with each cell containing a number.

A scalar, the number 4

The line with 3 sections, each one containing a number.

A 3x2 grid, with each cell containing a number.

Tensors may have more axes; here is a tensor with three axes:

# There can be an arbitrary number of
# axes (sometimes called "dimensions")
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)
tf.Tensor(
[[[ 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]]], shape=(3, 2, 5), dtype=int32)

Există multe modalități prin care puteți vizualiza un tensor cu mai mult de două axe.

A 3-axis tensor, shape: [3, 2, 5]



Puteți converti un tensor într-o arie NumPy fie folosindnp.arraysau latensor.numpyMetodă :

np.array(rank_2_tensor)
array([[1., 2.],
       [3., 4.],
       [5., 6.]], dtype=float16)
rank_2_tensor.numpy()
array([[1., 2.],
       [3., 4.],
       [5., 6.]], dtype=float16)

Tenzorii conțin adesea floate și inc, dar au multe alte tipuri, inclusiv:

  • Numere complexe
  • Stringă

Bazătf.Tensorclasa necesită ca tensorii să fie „rectangulari” – adică, de-a lungul fiecărei axe, fiecare element are aceeași dimensiune.

  • Tensiuni ridicate
  • Tensiuni economice

Puteți face matematică de bază pe tensori, inclusiv adunarea, multiplicarea în formă de element și multiplicarea matricei.

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

print(tf.add(a, b), "\n")
print(tf.multiply(a, b), "\n")
print(tf.matmul(a, b), "\n")
tf.Tensor(
[[2 3]
 [4 5]], shape=(2, 2), dtype=int32) 

tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32) 

tf.Tensor(
[[3 3]
 [7 7]], shape=(2, 2), dtype=int32)
print(a + b, "\n") # element-wise addition
print(a * b, "\n") # element-wise multiplication
print(a @ b, "\n") # matrix multiplication

tf.Tensor(
[[2 3]
 [4 5]], shape=(2, 2), dtype=int32) 

tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32) 

tf.Tensor(
[[3 3]
 [7 7]], shape=(2, 2), dtype=int32)

Tenzorii sunt utilizați în toate tipurile de operațiuni (sau "Ops").

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.math.argmax(c))
# Compute the softmax
print(tf.nn.softmax(c))
tf.Tensor(10.0, shape=(), dtype=float32)
tf.Tensor([1 0], shape=(2,), dtype=int64)
tf.Tensor(
[[2.6894143e-01 7.3105854e-01]
 [9.9987662e-01 1.2339458e-04]], shape=(2, 2), dtype=float32)

Notă: De obicei, oriunde o funcție TensorFlow așteaptă un Tensor ca intrare, funcția va accepta, de asemenea, orice poate fi transformat într-un Tensor folosind tf.convert_to_tensor.

Notă: De obicei, oriunde o funcție TensorFlow așteaptă un Tensor ca intrare, funcția va accepta, de asemenea, orice poate fi transformat într-un Tensor folosind tf.convert_to_tensor.

tf.convert_to_tensor([1,2,3])
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
tf.reduce_max([1,2,3])
<tf.Tensor: shape=(), dtype=int32, numpy=3>
tf.reduce_max(np.array([1,2,3]))
<tf.Tensor: shape=(), dtype=int64, numpy=3>


Despre forme

Tenzorii au forme.Unele vocabular:

  • Forma: Lungimea (numărul de elemente) a fiecărei axe a unui tensor.
  • Un scalar are rangul 0, un vector are rangul 1, o matrice are rangul 2.
  • Axa sau dimensiunea: o anumită dimensiune a unui tensor.
  • Dimensiune: Numărul total de elemente din tensor, produsul elementelor vectorului de formă.

Notă: Deși este posibil să vedeți o referință la un „tensor cu două dimensiuni”, un tenzor cu rang 2 nu descrie de obicei un spațiu 2D.

Note:Deși puteți vedea o referință la un „tensor cu două dimensiuni”, un tenzor de rang 2 nu descrie de obicei un spațiu 2D.

Tenzori șitf.TensorShapeobiectele au proprietăți convenabile pentru accesarea acestora:

rank_4_tensor = tf.zeros([3, 2, 4, 5])


A rank-4 tensor, shape: [3, 2, 4, 5]

A tensor shape is like a vector.

A 4-axis tensor

A tensor shape is like a vector.

A 4-axis tensor

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())
Type of every element: <dtype: 'float32'>
Number of axes: 4
Shape of tensor: (3, 2, 4, 5)
Elements along axis 0 of tensor: 3
Elements along the last axis of tensor: 5
Total number of elements (3*2*4*5):  120

Cu toate acestea, reţineţi căTensor.ndimşiTensor.shapeAtributele nu se întorcTensorobiecte. dacă aveţi nevoie de oTensorFoloseștetf.ranksautf.shapeAceastă diferență este subtilă, dar poate fi importantă atunci când construiți grafice (mai târziu).

tf.rank(rank_4_tensor)
<tf.Tensor: shape=(), dtype=int32, numpy=4>
tf.shape(rank_4_tensor)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 4, 5], dtype=int32)>

În timp ce axele sunt adesea menționate de indicii lor, ar trebui să urmăriți întotdeauna semnificația fiecăruia. Adesea, axele sunt ordonate de la global la local: axa lotului mai întâi, urmată de dimensiunile spațiale, și caracteristicile pentru fiecare locație ultima.

Typical axis order

Keep track of what each axis is. A 4-axis tensor might be: Batch, Width, Height, Features

Keep track of what each axis is. A 4-axis tensor might be: Batch, Width, Height, Features


Indexări

Indexarea cu un singur ax

TensorFlow urmează regulile standard de indexare Python, similar cuindexarea unei liste sau a unui șir în Python, și regulile de bază pentru indexarea NumPy.

  • Indicele începe cu 0
  • indicii negativi numără înapoi de la sfârșit
  • colone, :, sunt utilizate pentru felii: start:stop:step


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

Indexarea cu un scalar îndepărtează axa:

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

Indexarea cu a:Slice păstrează axa:

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())
Everything: [ 0  1  1  2  3  5  8 13 21 34]
Before 4: [0 1 1 2]
From 4 to the end: [ 3  5  8 13 21 34]
From 2, before 7: [1 2 3 5 8]
Every other item: [ 0  1  3  8 21]
Reversed: [34 21 13  8  5  3  2  1  1  0]

Indexarea multi-axială

Tensorii de rang superior sunt indexați prin trecerea mai multor indici.

Exact aceleași reguli ca și în cazul cu un singur ax se aplică fiecărei axe în mod independent.

print(rank_2_tensor.numpy())
[[1. 2.]
 [3. 4.]
 [5. 6.]]

Dacă treceți un număr întreg pentru fiecare index, rezultatul este un scalar.

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

You can index using any combination of integers and slices:

# 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")
Second row: [3. 4.]
Second column: [2. 4. 6.]
Last row: [5. 6.]
First item in last column: 2.0
Skip the first row:
[[3. 4.]
 [5. 6.]]

Here is an example with a 3-axis tensor:

print(rank_3_tensor[:, :, 4])
tf.Tensor(
[[ 4  9]
 [14 19]
 [24 29]], shape=(3, 2), dtype=int32)


Selecting the last feature across all locations in each example in the batch

A 3x2x5 tensor with all the values at the index-4 of the last axis selected.

The selected values packed into a 2-axis tensor.

A 3x2x5 tensor with all the values at the index-4 of the last axis selected.

The selected values packed into a 2-axis tensor.

Citește TheTendință de tăiere ghidpentru a afla cum puteți aplica indexarea pentru a manipula elementele individuale din tensori.

Manipularea formelor

Refacerea unui tensor este de mare utilitate.

# Shape returns a `TensorShape` object that shows the size along each axis
x = tf.constant([[1], [2], [3]])
print(x.shape)
(3, 1)
# You can convert this object into a Python list, too
print(x.shape.as_list())
[3, 1]

Puteți transforma un tensiune într-o formă nouă.tf.reshapeoperațiunea este rapidă și ieftină, deoarece datele subiacente nu au nevoie să fie duplicate.

# You can reshape a tensor to a new shape.
# Note that you're passing in a list
reshaped = tf.reshape(x, [1, 3])
print(x.shape)
print(reshaped.shape)
(3, 1)
(1, 3)

Datele își păstrează aspectul în memorie și se creează un nou tensor, cu forma solicitată, care indică aceleași date. TensorFlow utilizează ordinea de memorie "row-major" în stilul C, unde creșterea indicelui cel mai drept corespunde unui singur pas în memorie.

print(rank_3_tensor)
tf.Tensor(
[[[ 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]]], shape=(3, 2, 5), dtype=int32)

Dacă plasați un tensor, puteți vedea ce ordine este plasată în memorie.

# A `-1` passed in the `shape` argument says "Whatever fits".
print(tf.reshape(rank_3_tensor, [-1]))
tf.Tensor(
[ 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], shape=(30,), dtype=int32)

Singura utilizare rezonabilă atf.reshapeeste de a combina sau de a diviza axele adiacente (sau de a adăuga/elimina1cu s).

Pentru acest tensor 3x2x5, resetarea în (3x2)x5 sau 3x(2x5) sunt ambele lucruri rezonabile de făcut, deoarece felii nu se amestecă:

print(tf.reshape(rank_3_tensor, [3*2, 5]), "\n")
print(tf.reshape(rank_3_tensor, [3, -1]))
tf.Tensor(
[[ 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]], shape=(6, 5), dtype=int32) 

tf.Tensor(
[[ 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]], shape=(3, 10), dtype=int32)



Some good reshapes.

A 3x2x5 tensor

The same data reshaped to (3x2)x5

The same data reshaped to 3x(2x5)

A 3x2x5 tensor

The same data reshaped to (3x2)x5

The same data reshaped to 3x(2x5)

Remodelarea va "funcționa" pentru orice formă nouă cu același număr total de elemente, dar nu va face nimic util dacă nu respectați ordinea axelor.

Schimbarea axelor întf.reshapenu funcționează; aveți nevoietf.transposePentru aceasta.

# Bad examples: don't do this

# You can't reorder axes with reshape.
print(tf.reshape(rank_3_tensor, [2, 3, 5]), "\n") 

# This is a mess
print(tf.reshape(rank_3_tensor, [5, 6]), "\n")

# This doesn't work at all
try:
  tf.reshape(rank_3_tensor, [7, -1])
except Exception as e:
  print(f"{type(e).__name__}: {e}")


tf.Tensor(
[[[ 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]]], shape=(2, 3, 5), dtype=int32) 

tf.Tensor(
[[ 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]], shape=(5, 6), dtype=int32) 

InvalidArgumentError: { {function_node __wrapped__Reshape_device_/job:localhost/replica:0/task:0/device:GPU:0} } Input to reshape is a tensor with 30 values, but the requested shape requires a multiple of 7 [Op:Reshape]



Some bad reshapes.

You can't reorder axes, use tf.transpose for that

Anything that mixes the slices of data together is probably wrong.

The new shape must fit exactly.

You can't reorder axes, use tf.transpose for that

Anything that mixes the slices of data together is probably wrong.

The new shape must fit exactly.

Puteţi rula peste forme care nu sunt complet specificate. fie forma conţine oNone(o lungime de osie este necunoscută) sau întreaga formă esteNone(Rata tensiunii este necunoscută).

Cu excepția tf.RaggedTensor, astfel de forme vor apărea numai în contextul API-urilor simbolice de graficare ale TensorFlow:

  • tf. funcţie
  • Funcționalitatea puternică a focului.


Mai multTipuri

Tipuri

Pentru a verifica atf.TensorTipul de date utilizeazăTensor.dtypeProprietăţii .

Când se creează otf.Tensordintr-un obiect Python, puteți specifica opțional tipul de date.

Dacă nu, TensorFlow alege un tip de date care poate reprezenta datele.tf.int32și Python numere de puncte plutitoare latf.float32În caz contrar, TensorFlow utilizează aceleași reguli pe care NumPy le folosește atunci când convertește în matrice.

Puteți arunca de la tip la tip.

the_f64_tensor = tf.constant([2.2, 3.3, 4.4], dtype=tf.float64)
the_f16_tensor = tf.cast(the_f64_tensor, dtype=tf.float16)
# Now, cast to an uint8 and lose the decimal precision
the_u8_tensor = tf.cast(the_f16_tensor, dtype=tf.uint8)
print(the_u8_tensor)
tf.Tensor([2 3 4], shape=(3,), dtype=uint8)


transmisiune

Transmiterea este un concept împrumutat de laCaracteristici similare în NumPyPe scurt, în anumite condiții, tensiunile mai mici sunt "întinse" automat pentru a se potrivi tensiunilor mai mari atunci când se execută operațiuni combinate pe ele.

Cel mai simplu și cel mai frecvent caz este atunci când încercați să multiplicați sau să adăugați un tensor la un scalar.

x = tf.constant([1, 2, 3])

y = tf.constant(2)
z = tf.constant([2, 2, 2])
# All of these are the same computation
print(tf.multiply(x, 2))
print(x * y)
print(x * z)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)

În mod similar, axele cu lungimea 1 pot fi întinse pentru a se potrivi cu celelalte argumente.

În acest caz, o matrice 3x1 este înmulțită elementar cu o matrice 1x4 pentru a produce o matrice 3x4.[4].

# These are the same computations
x = tf.reshape(x,[3,1])
y = tf.range(1, 5)
print(x, "\n")
print(y, "\n")
print(tf.multiply(x, y))
tf.Tensor(
[[1]
 [2]
 [3]], shape=(3, 1), dtype=int32) 

tf.Tensor([1 2 3 4], shape=(4,), dtype=int32) 

tf.Tensor(
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]], shape=(3, 4), dtype=int32)

A broadcasted add: a [3, 1] times a [1, 4] gives a [3,4]

Adding a 3x1 matrix to a 4x1 matrix results in a 3x4 matrix

Adding a 3x1 matrix to a 4x1 matrix results in a 3x4 matrix

Iată aceeași operațiune fără difuzare:

x_stretch = tf.constant([[1, 1, 1, 1],
                         [2, 2, 2, 2],
                         [3, 3, 3, 3]])

y_stretch = tf.constant([[1, 2, 3, 4],
                         [1, 2, 3, 4],
                         [1, 2, 3, 4]])

print(x_stretch * y_stretch)  # Again, operator overloading
tf.Tensor(
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]], shape=(3, 4), dtype=int32)

De cele mai multe ori, difuzarea este eficientă atât în timp cât și în spațiu, deoarece operațiunea de difuzare nu materializează niciodată tensiunile extinse în memorie.

Vezi cum arată difuzareatf.broadcast_to.

print(tf.broadcast_to(tf.constant([1, 2, 3]), [3, 3]))
tf.Tensor(
[[1 2 3]
 [1 2 3]
 [1 2 3]], shape=(3, 3), dtype=int32)

Spre deosebire de matematică, de exemplu,broadcast_toNu face nimic special pentru a salva memoria.Aici materializezi tensiunea.

Poate deveni și mai complicat.Această secţiuneCartea lui Jake VanderPlasPython Manual de știință a datelorarată mai multe trucuri de difuzare (din nou în NumPy).


tf.convert_to în tensor

Cele mai multe opțiuni, cum ar fitf.matmulşitf.reshapeArgumente de clasătf.TensorCu toate acestea, veți observa în cazul de mai sus, obiectele Python în formă de tensori sunt acceptate.

Cele mai multe, dar nu toate, apeluri opsconvert_to_tensorExistă un registru de conversii, iar cele mai multe clase de obiecte, cum ar fi NumPyndarray,TensorShapeLista Python șitf.VariableTotul se va transforma automat.

tf.register_tensor_conversion_functionpentru mai multe detalii, și dacă aveți propriul tip doriți să convertiți automat într-un tensor.


Tenzori răsucite

Un tensor cu număr variabil de elemente de-a lungul uneia dintre axe se numește "ragged".tf.ragged.RaggedTensorPentru datele rationalizate.

De exemplu, Acest lucru nu poate fi reprezentat ca un tensor regulat:

tf.RaggedTensor, shape: [4, None]

A 2-axis ragged tensor, each row can have a different length.

A 2-axis ragged tensor, each row can have a different length.

ragged_list = [
    [0, 1, 2, 3],
    [4, 5],
    [6, 7, 8],
    [9]]
try:
  tensor = tf.constant(ragged_list)
except Exception as e:
  print(f"{type(e).__name__}: {e}")
ValueError: Can't convert non-rectangular Python sequence to Tensor.

În loc să creeze otf.RaggedTensorUtilizareatf.ragged.constant:

ragged_tensor = tf.ragged.constant(ragged_list)
print(ragged_tensor)
<tf.RaggedTensor [[0, 1, 2, 3], [4, 5], [6, 7, 8], [9]]>

Forma uneitf.RaggedTensorva conține câteva axe cu lungimi necunoscute:

print(ragged_tensor.shape)
(4, None)


Tensiuni String

tf.stringeste adtype, ceea ce înseamnă că puteți reprezenta datele ca șiruri (arate de byte cu lungime variabilă) în tensori.

Șirurile sunt atomice și nu pot fi indexate așa cum sunt șirurile Python. Lungimea șirului nu este una dintre axele tensorului.tf.stringsfuncţii pentru a le manipula.

Iată un tensor de șir scalar:

# Tensors can be strings, too here is a scalar string.
scalar_string_tensor = tf.constant("Gray wolf")
print(scalar_string_tensor)
tf.Tensor(b'Gray wolf', shape=(), dtype=string)

Un vector de șiruri:

A vector of strings, shape: [3,]

The string length is not one of the tensor's axes.

The string length is not one of the tensor's axes.


# If you have three string tensors of different lengths, this is OK.
tensor_of_strings = tf.constant(["Gray wolf",
                                 "Quick brown fox",
                                 "Lazy dog"])
# Note that the shape is (3,). The string length is not included.
print(tensor_of_strings)
tf.Tensor([b'Gray wolf' b'Quick brown fox' b'Lazy dog'], shape=(3,), dtype=string)

În articolul de mai sus, prinbPrefixul indică faptul cătf.stringdtype nu este un unicode, ci un byte-string.Tutorialul Unicodepentru mai multe despre lucrul cu text unicode în TensorFlow.

Dacă treceți caractere unicode, acestea sunt codificate cu utf-8.

tf.constant("🥳👍")
<tf.Tensor: shape=(), dtype=string, numpy=b'\xf0\x9f\xa5\xb3\xf0\x9f\x91\x8d'>

Unele funcții de bază cu șiruri pot fi găsite întf.stringsinclusivtf.strings.split.

# You can use split to split a string into a set of tensors
print(tf.strings.split(scalar_string_tensor, sep=" "))
tf.Tensor([b'Gray' b'wolf'], shape=(2,), dtype=string)
# ...but it turns into a `RaggedTensor` if you split up a tensor of strings,
# as each string might be split into a different number of parts.
print(tf.strings.split(tensor_of_strings))
<tf.RaggedTensor [[b'Gray', b'wolf'], [b'Quick', b'brown', b'fox'], [b'Lazy', b'dog']]>

Three strings split, shape: [3, None]

Splitting multiple strings returns a tf.RaggedTensor

Splitting multiple strings returns a tf.RaggedTensor

şitf.strings.to_number:

text = tf.constant("1 10 100")
print(tf.strings.to_number(tf.strings.split(text, " ")))
tf.Tensor([  1.  10. 100.], shape=(3,), dtype=float32)

Deşi nu poţi folositf.castPentru a transforma un tensor de șir în numere, îl puteți converti în byte și apoi în numere.

byte_strings = tf.strings.bytes_split(tf.constant("Duck"))
byte_ints = tf.io.decode_raw(tf.constant("Duck"), tf.uint8)
print("Byte strings:", byte_strings)
print("Bytes:", byte_ints)
Byte strings: tf.Tensor([b'D' b'u' b'c' b'k'], shape=(4,), dtype=string)
Bytes: tf.Tensor([ 68 117  99 107], shape=(4,), dtype=uint8)
# Or split it up as unicode and then decode it
unicode_bytes = tf.constant("アヒル 🦆")
unicode_char_bytes = tf.strings.unicode_split(unicode_bytes, "UTF-8")
unicode_values = tf.strings.unicode_decode(unicode_bytes, "UTF-8")

print("\nUnicode bytes:", unicode_bytes)
print("\nUnicode chars:", unicode_char_bytes)
print("\nUnicode values:", unicode_values)
Unicode bytes: tf.Tensor(b'\xe3\x82\xa2\xe3\x83\x92\xe3\x83\xab \xf0\x9f\xa6\x86', shape=(), dtype=string)

Unicode chars: tf.Tensor([b'\xe3\x82\xa2' b'\xe3\x83\x92' b'\xe3\x83\xab' b' ' b'\xf0\x9f\xa6\x86'], shape=(5,), dtype=string)

Unicode values: tf.Tensor([ 12450  12498  12523     32 129414], shape=(5,), dtype=int32)

petf.stringdtype este utilizat pentru toate datele de byte brute din TensorFlow.tf.ioModulul conține funcții pentru conversia datelor în și din byte, inclusiv decodarea imaginilor și parsing csv.

Tensiuni economice

Uneori, datele dvs. sunt rare, cum ar fi un spațiu de încorporare foarte larg.tf.sparse.SparseTensorși operațiuni conexe pentru a stoca datele scurte în mod eficient.

tf.SparseTensor, shape: [3, 4]

An 3x4 grid, with values in only two of the cells.

An 3x4 grid, with values in only two of the cells.

# Sparse tensors store values by index in a memory-efficient manner
sparse_tensor = tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]],
                                       values=[1, 2],
                                       dense_shape=[3, 4])
print(sparse_tensor, "\n")

# You can convert sparse tensors to dense
print(tf.sparse.to_dense(sparse_tensor))
SparseTensor(indices=tf.Tensor(
[[0 0]
 [1 2]], shape=(2, 2), dtype=int64), values=tf.Tensor([1 2], shape=(2,), dtype=int32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64)) 

tf.Tensor(
[[1 0 0 0]
 [0 0 2 0]
 [0 0 0 0]], shape=(3, 4), dtype=int32)

Publicat inițial pe site-ul TensorFlow, acest articol apare aici sub un titlu nou și este licențiat sub CC BY 4.0.

Originally published on the TensorFlow website,acest articol apare aici sub un titlu nou și este licențiat subCC BY 4.0.Modele de cod sunt împărtășite subApache 2.0 License.

Site-ul TensorFlow


Trending Topics

blockchaincryptocurrencyhackernoon-top-storyprogrammingsoftware-developmenttechnologystartuphackernoon-booksBitcoinbooks