Imininingwane Overview
- Imininingwane
- mayelana Shapes
- Ukucaciswa
- Ukucubungula Shapes
- Ukubuyekezwa kwe-Dtypes
- Ukuhlobisa
- tf.convert_to ku_tensor
- Ukuhlobisa Tensors
- Ukuhlobisa Tensile
- Ukuhlobisa Tensors
Tensors zihlanganisa i-array ye-multidimensional nge-type ye-uniform (eyaziwa njenge-adtype
) Uyakwazi ukubona konke okukhuselwadtypes
Ukusukatf.dtypes
.
Uma unemibuzoUkubala, ama-tensors zihlanganisa likenp.arrays
.
Zonke ama-tensors zihlukile njenge-Python ama-number ne-strings: ungakwazi ukuhlaziywa okuqukethwe kwe-tensor, kuphela ukwakha omusha.
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
Imininingwane
Okokuqala, ukwakha amanye ama-tensors eziyinhloko.
Ngiyazi "scalar" noma "rank-0" tensor. I-scalar inesibini elilodwa, futhi akukho "i-axes".
# 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
A "vector" noma "rank-1" tensor kuyinto like a list of values. A vector has one axis:
# 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: |
A matrix, shape: |
---|---|---|
|
|
|
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)
Kwezindlela ezininzi ungayifundisa i-tensor enezinhloko ezingu-2.
A 3-axis tensor, shape: |
|
|
---|---|---|
|
|
|
Ungayifaka i-tensor ku-NumPy array noma usebenzisanp.array
noma itensor.numpy
Ukucaciswa:
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)
Tensors ngokuvamile zihlanganisa floats futhi ints, kodwa kukhona nezinye izinhlobo eziningi, kuhlanganise:
- Inombolo Complex
- Ukuhlobisa
Ibhizinisitf.Tensor
Class inikeza tensors ukuba 'rectangular' --- okuyinto, ngokulandelana nenkinga, zonke izakhiwo ubukhulu efanayo. Nokho, kukhona izinhlobo specialized of tensors that can handle different shapes:
- Ukuhlobisa Tensile
- Ukuhlobisa Tensile
Uyakwazi ukwenza imathemikhali esisodwa ku-tensors, kuhlanganise ukuguqulwa, ukuguqulwa kwe-element-like, kanye nokuguqulwa kwe-matrix.
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)
I-Tensors isetshenziselwa zonke izinhlobo zokusebenza (noma "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)
Qaphela: Ngokuvamile, lapho umsebenzi we-TensorFlow ibheka i-Tensor njenge-input, isicelo esithathwe konke okwenziwe ku-Tensor usebenzisa i-tf.convert_to_tensor. Khona isibonelo ngezansi.
Qaphela: Ngokuvamile, lapho umsebenzi we-TensorFlow ibheka i-Tensor njenge-input, isicelo esithathwe konke okwenziwe ku-Tensor usebenzisa i-tf.convert_to_tensor. Khona isibonelo ngezansi.
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>
mayelana Shapes
I-Tensors has shapes. Abaningi i-vocabulary:
- Umbala: Ubude (inombolo yama-elements) yezinhlayiyana ngamunye zezinhlayiyana ze-tensor.
- I-Rank: Inombolo ye-tensor axes. I-scalar inesibini ye-0, i-vector inesibini ye-1, futhi i-matrix inesibini ye-2
- Axis noma Dimension: Ubukhulu obuningi we-tensor.
- Usayizi: Inani lwezinto ephelele ku-tensor, umkhiqizo yezinto ze-vector ye-shape.
Qaphela: Nangona ungayifaka ku-"tensor ye-2-dimensional", i-tensor ye-rank-2 ikakhulukazi akubonisa indawo ye-2D.
Qaphela: Nangona ungayifaka ku-"tensor ye-2-dimensional", i-tensor ye-rank-2 ikakhulukazi akubonisa indawo ye-2D.
Tensors kanyetf.TensorShape
Izinto zihlanganisa izakhiwo ezinhle ukufinyelela ngezinto:
rank_4_tensor = tf.zeros([3, 2, 4, 5])
|
A rank-4 tensor, shape: |
---|---|
|
|
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
Ngiyazi ukuthiTensor.ndim
WazeTensor.shape
Izinzuzo AmasipalaTensor
izikhwama. Uma ufuna aTensor
Ukusebenzisatf.rank
nomatf.shape
Uhlobo le-function. Lezi zihlanganisa, kodwa kungenziwa ebalulekile ekukhiqizeni i-graphs (ngemuva kwalokho).
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)>
Nangona ama-axis zihlanganisa ngokuvamile nge-indices yayo, kufanele uxhumane ngokuvamile igama lwezinye. Ngokuvamile ama-axis zihlanganisa kusuka ku-global kuya ku-local: Isisindo se-batch kuqala, esilandelayo ngezinqubo zendawo, futhi izici ze-location ngamunye. Ngokuvamile, ama-vectors ze-feature zihlanganisa izindawo ezingxenyeni ze-memory.
Typical axis order |
---|
|
Ukucaciswa
I-indexing ye-single axis
I-TensorFlow ivela ngezinsizakalo ezivamile ze-Python ye-indexing, efana neUkubhalisa umbhalo noma umbhalo ku-Python, kanye nezinsizakalo eziyisisekelo ze-NumPy indexing.
- indices ezivela ku-0
- I-indices ye-negative ibekwe ngokushesha kusukela ekupheleni
- ama-colons, :, isetshenziselwa izikhwama: 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]
Ukubhalisa nge-scalar ukunciphisa isikhunta:
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
Ukubhalisa nge-a:
slice ukugcina i-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())
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]
I-indexing ye-multi-axis
Tensors okusezingeni eliphezulu zihlanganiswa ngokulandelana nezindices eziningi.
Izinsizakalo efanayo ngokufanayo ne-single-axis isicelo ngamunye isixazululo ngokufanayo.
print(rank_2_tensor.numpy())
[[1. 2.]
[3. 4.]
[5. 6.]]
Ukubuyekeza inani elilodwa ngamunye ye-index, imiphumela kuyinto i-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 |
---|---|
|
|
OkufakiweUmhlahlandlela we-Slicing Guideukuze ufunde indlela yokusebenza kwe-indexing ukuguqulwa izinhlayiya ezithile ku-tensors akho.
Ukucubungula Shapes
Ukuguqulwa kwe-tensor kuyinto ezinhle kakhulu.
# 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]
Ungayifaka i-tensor ku-shape entsha. I-tf.reshape
Ukusebenza ngokushesha futhi kulungile njengoba idatha esifundayo ayidinga ukuguqulwa.
# 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)
I-Data ibhekwa emkhakheni yayo futhi i-tensor entsha ifakwe, ne-shape efakiwe, enikeza idatha efanayo. I-TensorFlow isetshenziselwa ukuguqulwa kwe-memory ye-C-style "row-major", lapho ukuguqulwa kwe-indicate ebusweni kulinganiswa ne-step eyodwa emkhakheni.
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)
Uma u-tensor ufakwe, ungakwazi ukubona ukuthi isigaba esekelwe ku-memory.
# 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)
Ngokuvamile, ukusetshenziswa okuhle kuphelatf.reshape
Ukubambisana noma ukwahlukanisa izindandatho zangaphakathi (noma ukongeza / ukuthatha1
Ukubuyekezwa
Ukuze lokhu 3x2x5 tensor, ukuguqulwa ku (3x2)x5 noma 3x(2x5) zihlanganisa izinto ezinhle ukwenza, njengoba izikhwama zihlanganisa:
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. |
---|---|---|
|
|
|
Reshaping "ukusebenza" noma iyiphi isakhiwo esitsha nge inani elifanayo yama-elements, kodwa akuyona yini elithakazelisayo uma ungacacacindezeleka inqubo ye-axes.
Ukushintshwa kwama-axistf.reshape
akufanele; kufaneletf.transpose
Ukuze lokhu.
# 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. |
---|---|---|
|
|
|
Ungayisebenzisa izakhiwo ezingenalutho ezingenalutho. Noma isakhiwo ifakweNone
(umlinganiselo we-axis-length ayikho) noma isakhiwo epheleleNone
(Ukuhla lwe-tensor akuyona).
Ngaphandle kwe-tf.RaggedTensor, izakhiwo ezivela kuphela ku-TensorFlow API ye-symbolic, i-graph-building:
- Ukubuyekezwa
- I-Hard Functional API yokusebenza.
OkuningiDTypes
Ukuhlobisa
Ukubuyekeza atf.Tensor
I-Data Type isebenzisa i-Tensor.dtype
imikhiqizo
Ukwakhiwa atf.Tensor
ukusuka ku-object ye-Python, ungathola i-data type.
Uma akufanele, i-TensorFlow uye ukhethe uhlobo idatha enokuthumela idatha yakho. I-TensorFlow i-Python integer yakhelwe kutf.int32
futhi i-Python i-floating point numbers ukuzetf.float32
Ngaphandle kwalokho, i-TensorFlow isetshenziselwa izinhlelo ezivamile ezisetshenziselwa i-NumPy ekuguqulwa ku-array.
Ungathola ukusuka ku-type kuya ku-type.
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)
Ukuhlobisa
Ukubuyekezwa kuyinto umqondo owenziwe kusuka ku-umphumela we-Equivalent in NumPyNgokuvamile, ngaphansi kwezimo ezithile, ama-tensors ezincinane zihlanganisa ngokuvamile ukuze zihlanganise ama-tensors ezincinane lapho zihlanganisa izinsizakalo ezihlanganisiwe.
Isibonelo esisodwa futhi esithakazelisayo kuyinto lapho udinga ukuguqulwa noma ukongeza isilinganiso ku-scalar. Kulesi isilinganiso, isilinganiso esithunyelwe ukuba yintoni esifanayo ne-argument eyodwa.
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)
Ngaphezu kwalokho, isixazululo esikhulu se-1 ingahlukaniswa ukuze zihlanganise nezinye iziqu ze-arguments. Zonke iziqu ze-arguments zingahlukaniswa ngama-calculation efanayo.
Kulokhu, i-matrix ye-3x1 iyahlukaniswa ngama-element ngokuvamile nge-matrix ye-1x4 ukukhiqiza i-matrix ye-3x4. Qaphela ukuthi i-leading 1 iyatholakala okungagunyaziwe: Umbala we-y[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 |
---|
|
Ngiyazi i-operation efanayo ngaphandle kokudluliselwa:
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)
Ngokuvamile, ukudluliselwa kuyinto isikhathi futhi indawo efanelekayo, njengoba ukusebenza ukudluliselwa akufanele ukufakelwa tensors ku-memory.
You can see what broadcasting looks like usebenzisatf.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)
Ngokungafani nomathemikhali op, isibonelo,broadcast_to
Ukusebenza okungenani okungenani okungenani okungenani okungenani okungenani okungenani okungenani okungenani okungenani okungenani.
Ngaba kungenziwa ngakumbi kakhulu.IsigabaUmbhali we-Jake VanderPlasUmhlahlandlela we-Python Data ScienceUkubonisa izintambo ezininzi zokuhamba (ngcono ku-NumPy).
tf.convert_to ku_tensor
Okuningi ops, liketf.matmul
Wazetf.reshape
Thola Arguments of Classtf.Tensor
Kodwa-ke, uzothola imiphumela emangalisayo, ama-Python ama-objects ebonakalayo njenge-tensors zihlanganisa.
Iningi, kodwa akuyona zonke, i-Ops Callconvert_to_tensor
ku-non-tensor arguments. Kukhona isiteshi se-conversions, futhi ama-object classes, njenge-NumPy'sndarray
Ngena ngemvaTensorShape
, izibuyekezo ze-Python, futhitf.Variable
Zonke izindlela zokusebenza ngokushesha.
Wazetf.register_tensor_conversion_function
Ukuze uthole okwengeziwe, futhi uma unayo uhlobo yakho yakho ufuna ukuguqulwa ngokushesha ku-tensor.
Ukuhlobisa Tensors
I-tensor enezinhlayiyana eziningana nezinhlayiyana eziningana nezinhlayiyana zibizwa ngokuthi i-"ragged".tf.ragged.RaggedTensor
Ukuze RAGED data.
Ngokwesibonelo, This ayikwazi ukubonisa njenge-tensor ebonakalayo:
A |
---|
|
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.
Ukwenza Atf.RaggedTensor
Ukusebenzisatf.ragged.constant
:
ragged_tensor = tf.ragged.constant(ragged_list)
print(ragged_tensor)
<tf.RaggedTensor [[0, 1, 2, 3], [4, 5], [6, 7, 8], [9]]>
Umbala we-atf.RaggedTensor
will contain a few axes with unknown lengths:
print(ragged_tensor.shape)
(4, None)
Ukuhlobisa
tf.string
I-Adtype
, okungenani ungakwazi ukubonisa idatha njenge-strings (i-variable-length byte array) ku-tensors.
I-strings iyona-atomic futhi ayikwazi ukufakelwa ngokufana ne-Python strings. Ubude we-string ayikho omunye ama-axis ye-tensor. Funda kabanzitf.strings
izidingo zokusebenza kubo.
Ngiya ku-Scalar String Tensor:
# 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)
I-vector ye-strings ye-
A vector of strings, shape: |
---|
|
# 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)
Ngaphezu kwalokho, i-printb
I-prefix ibonisa ukuthitf.string
dtype is a unicode string, kodwa i-byte-string.I-Unicode TutorialUkuze uthole okwengeziwe mayelana nokusebenza ne-unicode text ku-TensorFlow.
Uma uxhumane ama-unicode ama-characters, kukhona ama-utf-8 encoded.
tf.constant("🥳👍")
<tf.Tensor: shape=(), dtype=string, numpy=b'\xf0\x9f\xa5\xb3\xf0\x9f\x91\x8d'>
Izinhlelo ezithakazelisayo ze-strings zingatholakala ku-tf.strings
Ukubambisanatf.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: |
---|
|
Yinitf.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)
Nangona ungasebenzisatf.cast
Ukuguqulwa kwe-string tensor ku-number, ungakwazi ukuguqulwa ku-byte, bese ku-number.
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)
Wazetf.string
i-dtype isetshenziselwa zonke ama-byte yedatha e-TensorFlow.tf.io
Module iqukethe umsebenzi ukuguqulwa idatha ku-byte kanye kusuka ku-byte, kuhlanganise ukucubungula izithombe kanye nokuguqulwa kwe-csv.
Ukuhlobisa Tensile
Ngezinye izikhathi, idatha yakho kuyinto emangalisayo, njenge-embedding indawo eningi kakhulu. I-TensorFlow isekelwetf.sparse.SparseTensor
kanye nezinsizakalo ezihlobene ukugcina idatha e-sparse ngokushesha.
A |
---|
|
# 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)
Okwakhiwa kwebhizinisi le-TensorFlow, le ncwadi ifumaneke lapha ngaphansi kwebhizinisi elisha futhi isetshenziswe ngaphansi kwe-CC BY 4.0. Isampula se-codes ifakwe ngaphansi kwe-Apache 2.0 License.
Okwakhiwa kwebhizinisi le-TensorFlow, le ncwadi ifumaneke lapha ngaphansi kwebhizinisi elisha futhi isetshenziswe ngaphansi kwe-CC BY 4.0. Isampula se-codes ifakwe ngaphansi kwe-Apache 2.0 License.
Iwebhusayithi ye-TensorFlow