Andrew Ng 吴恩达的深度学习课程作业 TensorFlow Tutorial (TF2原生)

 使用TensorFlow 2.6.0版本改写TensorFlow 1的代码,使用TF2的Eager Execution动态执行图。

1 - Exploring the Tensorflow Library

1.1 导入相关库
import math
import h5py
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tf_utils import convert_to_one_hot, load_dataset, predict, random_mini_batches

%matplotlib inline
np.random.seed(1)
%matplotlib inline
np.random.seed(1)
1.2 计算 Loss
# 定义变量
y_hat = tf.constant(36, name="y_hat")
y = tf.constant(39, name="y")

# 计算损失
loss = (y - y_hat) ** 2
print(loss.numpy())
1.3 初始化变量
a = tf.constant(2)
b = tf.constant(10)
c = tf.multiply(a, b)
print(c)

# Output 输出
# Tensor("Mul:0", shape=(), dtype=int32)
1.4 运行
print(c.numpy())

# Output 输出
# 20
1.5 变量占位并赋值
# 直接定义x的值为3
x = tf.constant(3, dtype=tf.int64)

# 计算2倍的x
result = 2 * x

# 打印结果
print(result.numpy())

# Output 输出
# 6
1.6 Linear function 线性函数
# GRADED FUNCTION: linear_function

def linear_function():
    """
    Implements a linear function:
            Initializes W to be a random tensor of shape (4,3)
            Initializes X to be a random tensor of shape (3,1)
            Initializes b to be a random tensor of shape (4,1)
    Returns:
    result -- runs the session for Y = WX + b
    """

    np.random.seed(1)

    ### START CODE HERE ### (4 lines of code)
    X = tf.constant(np.random.randn(3, 1), name="X")
    W = tf.constant(np.random.randn(4, 3), name="W")
    b = tf.constant(np.random.randn(4, 1), name="b")
    Y = tf.add(tf.matmul(W, X), b)

    result = Y.numpy()
    
    return result

print("result = " + str(linear_function()))

# Output 输出
'''
result = [[-2.15657382]
 [ 2.95891446]
 [-1.08926781]
 [-0.84538042]]
'''
1.7 计算 Sigmoid
# GRADED FUNCTION: sigmoid

def sigmoid(z):
    """
    Computes the sigmoid of z

    Arguments:
    z -- input value, scalar or vector

    Returns:
    results -- the sigmoid of z
    """
    
    x = tf.Variable(z, dtype=tf.float32)
    
    return tf.math.sigmoid(x)


print("sigmoid(0) = " + str(sigmoid(0)))
print("sigmoid(12) = " + str(sigmoid(12)))

# Output 输出
'''
sigmoid(0) = tf.Tensor(0.5, shape=(), dtype=float32)
sigmoid(12) = tf.Tensor(0.9999939, shape=(), dtype=float32)
'''
1.8 计算成本Cost
# GRADED FUNCTION: cost

def cost(logits, labels):
    """
    Computes the cost using the sigmoid cross entropy

    Arguments:
    logits -- vector containing z, output of the last linear unit (before the final sigmoid activation)
    labels -- vector of labels y (1 or 0)

    Note: What we've been calling "z" and "y" in this class are respectively called "logits" and "labels"
    in the TensorFlow documentation. So logits will feed into z, and labels into y.

    Returns:
    cost -- runs the session of the cost (formula (2))
    """

    # Create the placeholders for "logits" (z) and "labels" (y) (approx. 2 lines)
    z = tf.Variable(logits, dtype=tf.float32, name="x")
    y = tf.Variable(labels, dtype=tf.float32, name="y")

    # Use the loss function (approx. 1 line)
    cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=z, labels=y)

    cost_value = cost.numpy()

    ### END CODE HERE ###

    return cost_value

logits = sigmoid(np.array([0.2,0.4,0.7,0.9]))
cost = cost(logits, np.array([0,0,1,1]))
print("cost = " + str(cost))

# Output 输出
# cost = [1.0053872  1.0366409  0.41385433 0.39956614]
1.9 独热编码 One Hot Encoding
# GRADED FUNCTION: one_hot_matrix

def one_hot_matrix(labels, C):
    """
    Creates a matrix where the i-th row corresponds to the ith class number and the jth column
                     corresponds to the jth training example. So if example j had a label i. Then entry (i,j)
                     will be 1.

    Arguments:
    labels -- vector containing the labels
    C -- number of classes, the depth of the one hot dimension

    Returns:
    one_hot -- one hot matrix
    """

    ### START CODE HERE ###

    # Create a tf.constant equal to C (depth), name it 'C'. (approx. 1 line)
    C = tf.constant(value=C, name="C")

    # Use tf.one_hot, be careful with the axis (approx. 1 line)
    one_hot_matrix = tf.one_hot(labels, C, axis=0)

    one_hot = one_hot_matrix.numpy()

    ### END CODE HERE ###

    return one_hot

labels = np.array([1, 2, 3, 0, 2, 1])
one_hot = one_hot_matrix(labels, C=4)
print("one_hot = " + str(one_hot))

# Output 输出
'''
one_hot = [[0. 0. 0. 1. 0. 0.]
 [1. 0. 0. 0. 0. 1.]
 [0. 1. 0. 0. 1. 0.]
 [0. 0. 1. 0. 0. 0.]]
'''
1.10 初始化向量
# GRADED FUNCTION: ones

def ones(shape):
    """
    Creates an array of ones of dimension shape

    Arguments:
    shape -- shape of the array you want to create

    Returns:
    ones -- array containing only ones
    """

    ### START CODE HERE ###

    # Create "ones" tensor using tf.ones(...). (approx. 1 line)
    ones = tf.ones(shape).numpy()


    ### END CODE HERE ###
    return ones

print("ones = " + str(ones([3])))

# Output 输出
# ones = [1. 1. 1.]

2 - Building your first neural network in tensorflow

2.1 读取数据
# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Example of a picture
index = 11
plt.imshow(X_train_orig[index])
print("y = " + str(np.squeeze(Y_train_orig[:, index])))

# Output 输出
# y = 1
2.2 数据预处理
# Normalize image vectors
X_train = X_train_orig / 255.0
X_test = X_test_orig / 255.0
# Convert training and test labels to one hot matrices
Y_train = to_categorical(Y_train_orig[0], num_classes=6)
Y_test = to_categorical(Y_test_orig[0], num_classes=6)


print("number of training examples = " + str(X_train.shape[0]))
print("number of test examples = " + str(X_test.shape[0]))
print("X_train shape: " + str(X_train.shape))
print("Y_train shape: " + str(Y_train.shape))
print("X_test shape: " + str(X_test.shape))
print("Y_test shape: " + str(Y_test.shape))

# Output 输出
'''
number of training examples = 1080
number of test examples = 120
X_train shape: (1080, 64, 64, 3)
Y_train shape: (1080, 6)
X_test shape: (120, 64, 64, 3)
Y_test shape: (120, 6)
'''
2.3 构建模型
def model(X, Y, X_test, Y_test, num_epochs=10, batch_size=32, learning_rate=0.0001):
    # Get the number of training examples
    m = X.shape[0]
    
    # Define a sequential model
    model = tf.keras.models.Sequential(
        [
            # Flatten layer to convert input to a 1D array
            tf.keras.layers.Flatten(),
            
            # First dense layer with 25 neurons and ReLU activation function
            tf.keras.layers.Dense(25, activation=tf.nn.relu),
            
            # Second dense layer with 12 neurons and ReLU activation function
            tf.keras.layers.Dense(12, activation=tf.nn.relu),
            
            # Output layer with 6 neurons (assuming 6 classes) and softmax activation function
            tf.keras.layers.Dense(6, activation=tf.nn.softmax),
        ]
    )
    
    # Compile the model with the Adam optimizer, categorical crossentropy loss function,
    # and tracking categorical accuracy as a metric
    model.compile(
        optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
        loss=tf.keras.losses.categorical_crossentropy,
        metrics=[tf.keras.metrics.CategoricalAccuracy()],
    )
    
    # Fit the model to the training data
    model.fit(X, Y, epochs=num_epochs, batch_size=batch_size, verbose=2)
    
    # Evaluate and print the accuracy on the training set
    print("Train accuracy = ", model.evaluate(X, Y))
    
    # Evaluate and print the accuracy on the test set
    print("Test accuracy = ", model.evaluate(X_test, Y_test))
    
    # Return the trained model
    return model
2.7 训练模型
model = model(
    X_train,
    Y_train,
    X_test,
    Y_test,
    num_epochs=1500,
    batch_size=64,
    learning_rate=0.0001,
)

# Output 输出
'''
Train accuracy =  [0.058349739760160446, 0.9935185313224792]
Test accuracy =  [0.5819722414016724, 0.8166666626930237]
'''
2.8 测试自己的图片
import imageio.v2 as im
import scipy
from PIL import Image

## START CODE HERE ## (PUT YOUR IMAGE NAME)
my_image = "thumbs_up.jpg"
## END CODE HERE ##

# We preprocess your image to fit your algorithm.
fname = "images/" + my_image
image = np.array(np.array(im.imread(fname)))
my_image = np.array(Image.fromarray(image).resize((64, 64))) / 255.0
image_input = np.expand_dims(my_image, axis=0)

my_image_prediction = model.predict(image_input)
predicted_class = np.argmax(my_image_prediction, axis=1)
plt.imshow(image)

print("Your algorithm predicts: y =", str(predicted_class[0]))

# Output 输出
# Your algorithm predicts: y = 3

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值