"An end-to-end open source machine learning platform."
%tensorflow_version 2.x
import tensorflow as tf
print(tf.version.VERSION)
2.0.0-rc2
a = tf.constant(3) # dtype=int32
b = tf.constant([[3., 1., 4.], [1., 5., 9.]])
print(a, b)
print(b.__class__)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([[3. 1. 4.]
[1. 5. 9.]], shape=(2, 3), dtype=float32)
<class 'tensorflow.python.framework.ops.EagerTensor'>
c = b.numpy()
print(c, c.__class__)
[[3. 1. 4.]
[1. 5. 9.]] <class 'numpy.ndarray'>
d = tf.constant(c)
print(d == b)
tf.Tensor([[ True True True]
[ True True True]], shape=(2, 3), dtype=bool)
b = tf.constant([3., 1., 4., 1., 5., 9.])
b[0] = 4
TypeError: 'tensorflow.python.framework.ops.EagerTensor'
object does not support item assignment
with tf.device("/device:GPU:0"):
a = tf.constant([3., 1., 4., 1., 5., 9.])
print(a.device)
/job:localhost/replica:0/task:0/device:GPU:0
b = tf.Variable([3., 1., 4., 1., 5., 9.])
b.assign_add(tf.ones_like(b))
print(b)
<tf.Variable 'Variable:0' shape=(6,) dtype=float32,
numpy=array([ 4., 2., 5., 2., 6., 10.], dtype=float32)>
a_true = tf.constant(-0.25) # intercept
b_true = tf.constant(0.5) # slope
x = tf.random.uniform((96,))
e = tf.random.normal((96,), 0, 0.1)
y = a_true + b_true*x + e
a = tf.Variable(0.)
b = tf.Variable(tf.random.uniform(()))
lr = tf.constant(0.5)
for epoch in range(100): # training loop
with tf.GradientTape() as t:
loss = tf.reduce_mean((a + b*x - y)**2) # (*)
[dloss_da, dloss_db] = t.gradient(loss, [a, b])
a.assign_sub(lr*dloss_da) # "a -= lr*dloss_da"
b.assign_sub(lr*dloss_db)
print(a.numpy(), b.numpy()) # a_true = -0.25, b_true = 0.5
-0.2548124 0.5260704
a_true = tf.constant(-0.25) # true intercept
b_true = tf.constant(0.5) # true slope
x = tf.random.uniform((96,))
e = tf.random.normal((96,), 0, 0.1) # errors in y
y = a_true + b_true*x + e
dataset = tf.data.Dataset.from_tensor_slices((x, y))
a = tf.Variable(0.) # initial intercept
b = tf.Variable(tf.random.uniform(())) # initial slope
lr = tf.constant(0.25) # learning rate
for x, y in dataset.shuffle(96) \
.repeat(100) \
.batch(3):
with tf.GradientTape() as t:
loss = tf.reduce_mean((a + b*x - y)**2)
[dloss_da, dloss_db] = t.gradient(loss, [a, b])
a.assign_sub(lr*dloss_da) # "a -= lr*dloss_da"
b.assign_sub(lr*dloss_db)
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Dense, Input
model = Sequential([Input(2), Dense(3), Dense(3), Dense(1)])
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 3) 9
_________________________________________________________________
dense_1 (Dense) (None, 3) 12
_________________________________________________________________
dense_2 (Dense) (None, 1) 4
=================================================================
Total params: 25
Trainable params: 25
Non-trainable params: 0
_________________________________________________________________
[<tensorflow.python.keras.layers.core.Dense at 0x13f3a0f50>,
<tensorflow.python.keras.layers.core.Dense at 0x13f3b5410>,
<tensorflow.python.keras.layers.core.Dense at 0x13f3b5990>]
Easy to get input about a given layer:
L = model.layers[0]
print(f"L.name = {layer.name}, L.units = {layer.units},
L.input_shape = {L.input_shape},
L.output_shape = {L.output_shape}")
L.name = dense, L.units = 3,
L.input_shape = (None, 2), L.output_shape = (None, 3)
tf.random.set_seed(666)
x = tf.random.uniform((4, 2))
y_pred = model.predict(x)
x = [[0.7861 0.4992]
[0.2993 0.8064]
[0.8418 0.4165]
[0.0332 0.8668]],
y_pred = [[ 0.5011]
[-1.06 ]
[ 0.7761]
[-1.695 ]]
layers[0].get_weights()
weights = [layer.get_weights() for layer in layers]
y = x
for [w, b] in weights:
y = y @ w + b
y = [[ 0.5011]
[-1.06 ]
[ 0.7761]
[-1.695 ]]
z = x
for layer in layers:
z = layer(z)
z = [[ 0.5011]
[-1.06 ]
[ 0.7761]
[-1.695 ]]
[[w1, b1], [w2, b2], [w3, b3]] = weights
w = w1 @ w2 @ w3
b = b1 @ w2 @ w3 + b2 @ w3 + b3
y = x @ w + b
y = [[ 0.5011]
[-1.06 ]
[ 0.7761]
[-1.695 ]]
import tensorflow.keras.activations
Name | Shorthand | Formula | Use |
identity | linear | $h(x)=x$ | output layer in regression problems |
sigmoid | sigmoid | $h(x)=\dfrac{1}{1 + e^{-x}}$ | output layer in two-class classification problems |
softmax | softmax | $h_i(x)=\dfrac{e^{x_i}}{\sum_{j=1}^K e^{x_j}}$ | output layer in $K$-class classification problems |
rectified linear unit (ReLU) | relu | $h(x)=\max(x, 0)$ | hidden layers of deep networks |
model = Sequential([Input(2),
Dense(10, activation="relu"),
Dense(10, activation="relu"),
Dense(1, activation="sigmoid")])
model = Sequential([Input(2),
Dense(10), Activation("relu"),
Dense(10), Activation("relu"),
Dense(1), Activation("sigmoid")])
Name | Shorthand | Use |
Mean squared error | mse | regression problems, continuous response |
Binary cross-entropy | binary_crossentropy | two-class classification problems |
Categorical cross-entropy | categorical_crossentropy | $K$-class classification problems |
import tensorflow.keras.losses
from tensorflow.keras.optimizers import SGD
optimizer = SGD(lr=0.005) # default lr = 0.01
model.compile(loss="mse", optimizer="sgd")
model.compile(loss="binary_crossentropy",
optimizer=SGD(lr=0.005))
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.fit(x_train, y_train,
epochs=20,
batch_size=64,
validation_split=0.2)
a = 1/np.sqrt(8)
x_train = np.random.uniform(
size=(512, 2))
y_train = np.logical_and(
np.abs(x_train[:,0] - 0.5) < a,
np.abs(x_train[:,1] - 0.5) < a))
model = Sequential([Input(2),
Dense(10, activation="relu"),
Dense(10, activation="relu"),
Dense(1, activation="sigmoid")])
model.compile(
loss="binary_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.fit(x_train, y_train,
epochs=500)
model.evaluate(x_test, y_test)
[0.100700, 0.9628906]
model.save("squares.h5")
from tensorflow.keras.models import load_model
model = load_model("square.h5")
from tensorflow.keras.applications.vgg16 import VGG16