:)
[DL Specialization] C2W3A1 본문
TensorFlow
Basic Optimization with GradientTape
import h5py
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.framework.ops import EagerTensor
from tensorflow.python.ops.resource_variable_ops import ResourceVariable
import time
train_dataset = h5py.File('train데이터 경로', "r")
test_dataset = h5py.File('test데이터 경로', "r")
x_train = tf.data.Dataset.from_tensor_slices(train_dataset['train_set_x'])
y_train = tf.data.Dataset.from_tensor_slices(train_dataset['train_set_y'])
x_test = tf.data.Dataset.from_tensor_slices(test_dataset['test_set_x'])
y_test = tf.data.Dataset.from_tensor_slices(test_dataset['test_set_y'])
print(x_train.element_spec)
TensorSpec(shape=(64, 64, 3), dtype=tf.uint8, name=None)
print(next(iter(x_train)))
tf.Tensor(
[[[227 220 214]
[227 221 215]
[227 222 215]
...
[232 230 224]
[231 229 222]
[230 229 221]]
[[227 221 214]
[227 221 215]
[228 221 215]
...
[232 230 224]
[231 229 222]
[231 229 221]]
[[227 221 214]
[227 221 214]
[227 221 215]
...
[232 230 224]
[231 229 223]
[230 229 221]]
...
[[119 81 51]
[124 85 55]
[127 87 58]
...
[210 211 211]
[211 212 210]
[210 211 210]]
[[119 79 51]
[124 84 55]
[126 85 56]
...
[210 211 210]
[210 211 210]
[209 210 209]]
[[119 81 51]
[123 83 55]
[122 82 54]
...
[209 210 210]
[209 210 209]
[208 209 209]]], shape=(64, 64, 3), dtype=uint8)
unique_labels = set()
for element in y_train:
unique_labels.add(element.numpy())
print(unique_labels)
{0, 1, 2, 3, 4, 5}
images_iter = iter(x_train)
labels_iter = iter(y_train)
plt.figure(figsize=(10, 10))
for i in range(25):
ax = plt.subplot(5, 5, i + 1)
plt.imshow(next(images_iter).numpy().astype("uint8"))
plt.title(next(labels_iter).numpy().astype("uint8"))
plt.axis("off")
def normalize(image):
image = tf.cast(image, tf.float32) / 255.0
image = tf.reshape(image, [-1,])
return image
new_train = x_train.map(normalize)
new_test = x_test.map(normalize)
linear func
def linear_function():
X = np.random.randn(3, 1)
W = np.random.randn(4, 3)
b = np.random.randn(4, 1)
Y = tf.matmul(W, X) + b
return Y
result = linear_function()
print(result)
assert type(result) == EagerTensor, "Use the TensorFlow API"
assert np.allclose(result, [[-2.15657382], [ 2.95891446], [-1.08926781], [-0.84538042]]), "Error"
print("\033[92mAll test passed")
tf.Tensor(
[[-2.15657382]
[ 2.95891446]
[-1.08926781]
[-0.84538042]], shape=(4, 1), dtype=float64)
sigmoid
def sigmoid(z):
z = tf.cast(z, dtype=tf.float32)
a = tf.keras.activations.sigmoid(z)
return a
result = sigmoid(-1)
print ("type: " + str(type(result)))
print ("dtype: " + str(result.dtype))
print ("sigmoid(-1) = " + str(result))
print ("sigmoid(0) = " + str(sigmoid(0.0)))
print ("sigmoid(12) = " + str(sigmoid(12)))
def sigmoid_test(target):
result = target(0)
assert(type(result) == EagerTensor)
assert (result.dtype == tf.float32)
assert sigmoid(0) == 0.5, "Error"
assert sigmoid(-1) == 0.26894143, "Error"
assert sigmoid(12) == 0.9999939, "Error"
print("\033[92mAll test passed")
sigmoid_test(sigmoid)
type: <class 'tensorflow.python.framework.ops.EagerTensor'>
dtype: <dtype: 'float32'>
sigmoid(-1) = tf.Tensor(0.26894143, shape=(), dtype=float32)
sigmoid(0) = tf.Tensor(0.5, shape=(), dtype=float32)
sigmoid(12) = tf.Tensor(0.9999939, shape=(), dtype=float32)
one-hot matrix
def one_hot_matrix(label, C=6):
one_hot = tf.reshape(tf.one_hot(indices=label, depth=C), (C,))
return one_hot
def one_hot_matrix_test(target):
label = tf.constant(1)
C = 4
result = target(label, C)
print("Test 1:",result)
assert result.shape[0] == C, "Use the parameter C"
assert np.allclose(result, [0., 1. ,0., 0.] ), "Wrong output. Use tf.one_hot"
label_2 = [2]
C = 5
result = target(label_2, C)
print("Test 2:", result)
assert result.shape[0] == C, "Use the parameter C"
assert np.allclose(result, [0., 0. ,1., 0., 0.] ), "Wrong output. Use tf.reshape as instructed"
print("\033[92mAll test passed")
one_hot_matrix_test(one_hot_matrix)
Test 1: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
Test 2: tf.Tensor([0. 0. 1. 0. 0.], shape=(5,), dtype=float32)
initialize parameters
def initialize_parameters():
W1 = tf.Variable(initializer(shape=(25, 12288)))
W2 = tf.Variable(initializer(shape=(12, 25)))
W3 = tf.Variable(initializer(shape=(6, 12)))
b1 = tf.Variable(initializer(shape=(25, 1)))
b2 = tf.Variable(initializer(shape=(12, 1)))
b3 = tf.Variable(initializer(shape=(6, 1)))
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2,
"W3": W3,
"b3": b3}
return parameters
def initialize_parameters_test(target):
parameters = target()
values = {"W1": (25, 12288),
"b1": (25, 1),
"W2": (12, 25),
"b2": (12, 1),
"W3": (6, 12),
"b3": (6, 1)}
for key in parameters:
print(f"{key} shape: {tuple(parameters[key].shape)}")
assert type(parameters[key]) == ResourceVariable, "All parameter must be created using tf.Variable"
assert tuple(parameters[key].shape) == values[key], f"{key}: wrong shape"
assert np.abs(np.mean(parameters[key].numpy())) < 0.5, f"{key}: Use the GlorotNormal initializer"
assert np.std(parameters[key].numpy()) > 0 and np.std(parameters[key].numpy()) < 1, f"{key}: Use the GlorotNormal initializer"
print("\033[92mAll test passed")
initialize_parameters_test(initialize_parameters)
W1 shape: (25, 12288)
b1 shape: (25, 1)
W2 shape: (12, 25)
b2 shape: (12, 1)
W3 shape: (6, 12)
b3 shape: (6, 1)
Building Your First Neural Network in TensorFlow
def forward_propagation(X, parameters):
# Retrieve the parameters from the dictionary "parameters"
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
Z1 = tf.matmul(W1, X) + b1
A1 = tf.nn.relu(Z1)
Z2 = tf.matmul(W2, A1) + b2
A2 = tf.nn.relu(Z2)
Z3 = tf.matmul(W3, A2) + b3
return Z3
def forward_propagation_test(target, examples):
minibatches = examples.batch(2)
parametersk = initialize_parameters()
W1 = parametersk['W1']
b1 = parametersk['b1']
W2 = parametersk['W2']
b2 = parametersk['b2']
W3 = parametersk['W3']
b3 = parametersk['b3']
index = 0
minibatch = list(minibatches)[0]
with tf.GradientTape() as tape:
forward_pass = target(tf.transpose(minibatch), parametersk)
print(forward_pass)
fake_cost = tf.reduce_mean(forward_pass - np.ones((6,2)))
assert type(forward_pass) == EagerTensor, "Your output is not a tensor"
assert forward_pass.shape == (6, 2), "Last layer must use W3 and b3"
assert np.allclose(forward_pass,
[[-0.13430887, 0.14086473],
[ 0.21588647, -0.02582335],
[ 0.7059658, 0.6484556 ],
[-1.1260961, -0.9329492 ],
[-0.20181894, -0.3382722 ],
[ 0.9558965, 0.94167566]]), "Output does not match"
index = index + 1
trainable_variables = [W1, b1, W2, b2, W3, b3]
grads = tape.gradient(fake_cost, trainable_variables)
assert not(None in grads), "Wrong gradients. It could be due to the use of tf.Variable whithin forward_propagation"
print("\033[92mAll test passed")
forward_propagation_test(forward_propagation, new_train)
tf.Tensor(
[[-0.13430887 0.14086473]
[ 0.21588647 -0.02582335]
[ 0.7059658 0.6484556 ]
[-1.1260961 -0.9329492 ]
[-0.20181894 -0.3382722 ]
[ 0.9558965 0.94167566]], shape=(6, 2), dtype=float32)
def compute_total_loss(logits, labels):
total_loss = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(labels, logits, from_logits=True))
return total_loss
def compute_total_loss_test(target, Y):
pred = tf.constant([[ 2.4048107, 5.0334096 ],
[-0.7921977, -4.1523376 ],
[ 0.9447198, -0.46802214],
[ 1.158121, 3.9810789 ],
[ 4.768706, 2.3220146 ],
[ 6.1481323, 3.909829 ]])
minibatches = Y.batch(2)
for minibatch in minibatches:
result = target(pred, tf.transpose(minibatch))
break
print("Test 1: ", result)
assert(type(result) == EagerTensor), "Use the TensorFlow API"
assert (np.abs(result - (0.50722074 + 1.1133534) / 2.0) < 1e-7), "Test 1 does not match. Did you get the reduce sum of your loss functions?"
### Test 2
labels = tf.constant([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
logits = tf.constant([[1., 0., 0.], [1., 0., 0.], [1., 0., 0.]])
result = compute_total_loss(logits, labels)
print("Test 2: ", result)
assert np.allclose(result, 3.295837 ), "Test 2 does not match."
print("\033[92mAll test passed")
compute_total_loss_test(compute_total_loss, new_y_train )
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
num_epochs = 1500, minibatch_size = 32, print_cost = True):
costs = []
train_acc = []
test_acc = []
parameters = initialize_parameters()
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
optimizer = tf.keras.optimizers.Adam(learning_rate)
# The CategoricalAccuracy will track the accuracy for this multiclass problem
test_accuracy = tf.keras.metrics.CategoricalAccuracy()
train_accuracy = tf.keras.metrics.CategoricalAccuracy()
dataset = tf.data.Dataset.zip((X_train, Y_train))
test_dataset = tf.data.Dataset.zip((X_test, Y_test))
# We can get the number of elements of a dataset using the cardinality method
m = dataset.cardinality().numpy()
minibatches = dataset.batch(minibatch_size).prefetch(8)
test_minibatches = test_dataset.batch(minibatch_size).prefetch(8)
#X_train = X_train.batch(minibatch_size, drop_remainder=True).prefetch(8)# <<< extra step
#Y_train = Y_train.batch(minibatch_size, drop_remainder=True).prefetch(8) # loads memory faster
# Do the training loop
for epoch in range(num_epochs):
epoch_total_loss = 0.
#We need to reset object to start measuring from 0 the accuracy each epoch
train_accuracy.reset_states()
for (minibatch_X, minibatch_Y) in minibatches:
with tf.GradientTape() as tape:
# 1. predict
Z3 = forward_propagation(tf.transpose(minibatch_X), parameters)
# 2. loss
minibatch_total_loss = compute_total_loss(Z3, tf.transpose(minibatch_Y))
# We accumulate the accuracy of all the batches
train_accuracy.update_state(minibatch_Y, tf.transpose(Z3))
trainable_variables = [W1, b1, W2, b2, W3, b3]
grads = tape.gradient(minibatch_total_loss, trainable_variables)
optimizer.apply_gradients(zip(grads, trainable_variables))
epoch_total_loss += minibatch_total_loss
# We divide the epoch total loss over the number of samples
epoch_total_loss /= m
# Print the cost every 10 epochs
if print_cost == True and epoch % 10 == 0:
print ("Cost after epoch %i: %f" % (epoch, epoch_total_loss))
print("Train accuracy:", train_accuracy.result())
# We evaluate the test set every 10 epochs to avoid computational overhead
for (minibatch_X, minibatch_Y) in test_minibatches:
Z3 = forward_propagation(tf.transpose(minibatch_X), parameters)
test_accuracy.update_state(minibatch_Y, tf.transpose(Z3))
print("Test_accuracy:", test_accuracy.result())
costs.append(epoch_total_loss)
train_acc.append(train_accuracy.result())
test_acc.append(test_accuracy.result())
test_accuracy.reset_states()
return parameters, costs, train_acc, test_acc
parameters, costs, train_acc, test_acc = model(new_train, new_y_train, new_test, new_y_test, num_epochs=100)
Cost after epoch 0: 0.569186
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 10: 2.502600
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 20: 11.844829
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 30: 33.861053
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 40: 73.509453
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 50: 134.513321
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 60: 219.395447
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 70: 329.978455
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 80: 467.702850
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Cost after epoch 90: 633.799866
Train accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
Test_accuracy: tf.Tensor(0.16666667, shape=(), dtype=float32)
# Plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per fives)')
plt.title("Learning rate =" + str(0.0001))
plt.show()
# Plot the train accuracy
plt.plot(np.squeeze(train_acc))
plt.ylabel('Train Accuracy')
plt.xlabel('iterations (per fives)')
plt.title("Learning rate =" + str(0.0001))
# Plot the test accuracy
plt.plot(np.squeeze(test_acc))
plt.ylabel('Test Accuracy')
plt.xlabel('iterations (per fives)')
plt.title("Learning rate =" + str(0.0001))
plt.show()
'Coursera' 카테고리의 다른 글
[DL Specialization] C2W2A1 (0) | 2024.09.17 |
---|---|
[DL Specialization] C2W1A3 (0) | 2024.09.14 |
[DL Specialization] C2W1A2 (0) | 2024.09.14 |
[DL Specialization] C2W1A1 (1) | 2024.09.07 |
[DL Specialization] C1W4A2 (0) | 2024.08.30 |