Introduzione a TensorFlow in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
Fonte: Public Domain Vectors
import tensorflow as tf
# 0D Tensor
d0 = tf.ones((1,))
# 1D Tensor
d1 = tf.ones((2,))
# 2D Tensor
d2 = tf.ones((2, 2))
# 3D Tensor
d3 = tf.ones((2, 2, 2))
# Stampa il tensore 3D
print(d3.numpy())
[[[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]]]
from tensorflow import constant
# Definisci una costante 2x3.
a = constant(3, shape=[2, 3])
# Definisci una costante 2x2.
b = constant([1, 2, 3, 4], shape=[2, 2])
| Operazione | Esempio |
|---|---|
tf.constant() |
constant([1, 2, 3]) |
tf.zeros() |
zeros([2, 2]) |
tf.zeros_like() |
zeros_like(input_tensor) |
tf.ones() |
ones([2, 2]) |
tf.ones_like() |
ones_like(input_tensor) |
tf.fill() |
fill([3, 3], 7) |
import tensorflow as tf
# Definisci una variabile
a0 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.float32)
a1 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.int16)
# Definisci una costante
b = tf.constant(2, tf.float32)
# Calcola il loro prodotto
c0 = tf.multiply(a0, b)
c1 = a0*b
Introduzione a TensorFlow in Python