Logistic-Regression-with-a-Neural-Network-mindset

版权声明:流浪者 https://blog.csdn.net/gz153016/article/details/83829808
Logistic_regression.py

"""
You will learn to:
- Build the general architecture of a learning algorithm, including:
- Initializing parameters
- Calculating the cost function and its gradient
- Using an optimization algorithm (gradient descent)
- Gather all three functions above into a main model function, in the right order.
"""
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
import skimage
"""
Problem Statement: You are given a dataset (“data.h5”) containing: 
- a training set of m_train images labeled as cat (y=1) or non-cat (y=0) 
- a test set of m_test images labeled as cat or non-cat 
- each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB).
   Thus, each image is square (height = num_px) and (width = num_px).
   图片都是正方形的,也就是width=height=num_px
   我们的目的就是构建一个准确的分类方法,来区分输入的图片究竟是否是猫。
"""

# Loading the data(cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
"""
之所以在上面的输入后面都加上_orig是因为接下来我们会对输入进行预处理,预处理之后,我们将其命名为train_set_x,test_set_x。
train_set_orig和test_set_orig的每一行数据都代表着一张图片,我们可以采用索引的方式来查看一下这个图片:
"""
# Example of a picture
index = 208
plt.imshow(train_set_x_orig[index])
print("y = " + str(train_set_y[:,index])+"it is a "+classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")
plt.show()
print("------------------------------------------------------------")
"""
Exercise: Find the values for: 
- m_train (number of training examples) 
- m_test (number of test examples) 
- num_px (= height = width of a training image) 
Remember that train_set_x_orig is a numpy-array of shape (m_train, num_px, num_px, 3). 
For instance, you can access m_train by writing train_set_x_orig.shape[0].
"""
### START CODE HERE ### (≈ 3 lines of code)
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
### END CODE HERE ###

print ("Number of training examples: m_train = " + str(m_train))
print("Number of testing examples: m_test = " + str(m_test))
print("Height/Width of each image: num_px = " + str(num_px))

print("Each image is of size:("+str(num_px)+","+str(num_px)+",3)")
print("train_set_x shape:"+str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print("------------------------------------------------------------")
"""
For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px ∗∗ num_px ∗∗ 3, 1).
After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. 
There should be m_train (respectively m_test) columns.

Exercise: Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into
 single vectors of shape (num_px ∗∗ num_px ∗∗ 3, 1).
 A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b∗c∗d, a) is to use:
 X_flatten = X.reshape(X.shape[0], -1).T      # X.T is the transpose of X
"""
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T

print("train_set_x_flatten shape:"+str(train_set_x_flatten.shape))
print("train_set_y shape:"+str(train_set_y.shape))
print("test_set_x_flatten shape:"+str(test_set_x_flatten.shape))
print("test_set_y shape:"+str(test_set_y.shape))
print("sanity check after reshaping:"+str(train_set_x_flatten[0:5,0]))
print("------------------------------------------------------------")
"""
但是在图像中有一种简单的额归一化方法,效果也不错,就是每个样本都除以255(每个通道像素的最大值)。
归一化图像数据的方法为:
"""
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255

"""
The main steps for building a Neural Network are: 
1. Define the model structure (such as number of input features) 
2. Initialize the model’s parameters 
3. Loop: 
- Calculate current loss (forward propagation) 
- Calculate current gradient (backward propagation) 
- Update parameters (gradient descent)
"""
#4.1 - Helper functions
# graded function:sigmoid(激活函数)

def sigmoid(z):
    """
    Compute the sigmoid of z
    Arguments:
    z -- A scalar or numpy array of any size
    :param z:
    :return s-- sigmoid(z):
    """
    s = 1/(1+np.exp(-z))
    return s
print("sigmod([0,2])"+str(sigmoid(np.array([0,2]))))
print("-----------------------------------------------")
"""
4.2 - Initializing parameters
Exercise: Implement parameter initialization in the cell below. 
You have to initialize w as a vector of zeros. 
If you don’t know what numpy function to use, 
look up np.zeros() in the Numpy library’s documentation.
"""
# Graded Function:Iinitialize_with_zeros
def initialize_with_zeros(dim):
    """
    This function creates a vector of zeros of shape (dim,1)for w and initializes b to 0
    :param dim -- size of the w vector we want (or number of parameters in this case):
    :return:
    w -- initialized vetor of shape(dim,1)
    b -- initialized scalar (corrsponds to the bias)
    """
    w = np.zeros((dim,1))
    b = 0
    assert(w.shape == (dim,1))
    assert(isinstance(b,float)or isinstance(b,int))
    return w,b

dim = 2
w,b = initialize_with_zeros(dim)
print("w = " + str(w))
print("b = " + str(b))
print("----------------------------------")
"""
4.3 - Forward and Backward propagation
Now that your parameters are initialized, 
you can do the “forward” and “backward” propagation steps for learning the parameters.
Exercise: Implement a function propagate() that computes the cost function and its gradient.
"""

def propagate(w, b, X, Y):
    """
    Implement the cost function ans its gradient for the propagation explained above
    Arguments:
    w -- weights,a numpy array of size (num_px*num_px*3,1)
    b -- bias,a scalar
    X -- data of size(num_px * num_px * 3,number of examples)
    Y -- true "label" vector (containing 0 if non-cat,1 if cat) of size(1,number of examples)
    Return:
    cost -- negative log-likelihood cost for logistic regression
    dw -- gradient of the loss with respect to w,thus same shape as w
    db -- gradient of the loss with respect to b,thus same shape as b
    Tips:
    - Write your code step by step for the propagation. np.dot()
    """
    m = X.shape[1]
   # print("m:"+str(m))

    # FORWARD PROPAGATION (FROM X TO COST)
    ### START CODE HERE ### (≈ 2 lines of code)
    A = sigmoid(np.dot(w.T, X) + b)  # compute activation
    cost = -(1 / m) * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))  # compute cost
    ### END CODE HERE ###

    # BACKWARD PROPAGATION (TO FIND GRAD)
    ### START CODE HERE ### (≈ 2 lines of code)
    dw = (1 / m) * np.dot(X, (A - Y).T)
    db = (1 / m) * np.sum(A - Y)
    ### END CODE HERE ###

    assert (dw.shape == w.shape)
    assert (db.dtype == float)
    cost = np.squeeze(cost)
    assert (cost.shape == ())

    grads = {"dw": dw,
             "db": db}

    return grads, cost

w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
print("---------------------------------------")
"""Optimization
You have initialized your parameters.
You are also able to compute a cost function and its gradient.
Now, you want to update the parameters using gradient descent.
Exercise: Write down the optimization function. 
The goal is to learn w and b by minimizing the cost function J.
For a parameter θ, the update rule is θ=θ−α*dθ, where αis the learning rate.
"""
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
    """
    This function optimizes w and b by running a gradient descent algorithm

    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of shape (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, number of examples)
    num_iterations -- number of iterations of the optimization loop
    learning_rate -- learning rate of the gradient descent update rule
    print_cost -- True to print the loss every 100 steps

    Returns:
    params -- dictionary containing the weights w and bias b
    grads -- dictionary containing the gradients of the weights and bias with respect to the cost function
    costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve.

    Tips:
    You basically need to write down two steps and iterate through them:
        1) Calculate the cost and the gradient for the current parameters. Use propagate().
        2) Update the parameters using gradient descent rule for w and b.
    """

    costs = []

    for i in range(num_iterations):


        # Cost and gradient calculation (≈ 1-4 lines of code)
        ### START CODE HERE ###
        grads, cost = propagate(w,b,X,Y)
        ### END CODE HERE ###

        # Retrieve derivatives from grads
        dw = grads["dw"]
        db = grads["db"]

        # update rule (≈ 2 lines of code)
        ### START CODE HERE ###
        w = w - learning_rate*dw
        b = b - learning_rate*db
        ### END CODE HERE ###

        # Record the costs
        if i % 100 == 0:
            costs.append(cost)

        # Print the cost every 100 training examples
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))

    params = {"w": w,
              "b": b}

    grads = {"dw": dw,
             "db": db}

    return params, grads, costs

params, grads, costs = optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009, print_cost = False)

print ("w = " + str(params["w"]))
print ("b = " + str(params["b"]))
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print("----------------------------------------------")

"""
Exercise: The previous function will output the learned w and b. 
We are able to use w and b to predict the labels for a dataset X. 
Implement the predict() function. There is two steps to computing predictions:
Calculate Y^=A=σ(wTX+b)
Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector Y_prediction. If you wish, you can use an if/else statement in a for loop (though there is also a way to vectorize this).
"""
# GRADED FUNCTION: predict
def predict(w, b, X):
    m = X.shape[1]
    Y_prediction = np.zeros((1,m))
    w = w.reshape(X.shape[0], 1)
    A = sigmoid(np.dot(w.T, X) + b)
    for i in range(A.shape[1]):
        if (A[0, i] > 0.5):
            Y_prediction[0, i] = 1
        else:
            Y_prediction[0, i] = 0

    assert (Y_prediction.shape == (1, m))
    return Y_prediction

print("predictions ="+ str(predict(w,b,X)))
print("-----------------------------------------")

# GRADED FUNCTION: model
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
    w, b = np.zeros((X_train.shape[0], 1)), 0
    parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = parameters["w"]
    b = parameters["b"]
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
    print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}

    return d

d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)

"""
训练的准确性接近100%,测试的准确性为70%,说明出现了过拟合的情况,后面会介绍方法解决这个问题。
如果想知道测试数据集中具体的图片预测情况:
"""
index = 1
plt.imshow(test_set_x[:,index].reshape((num_px,num_px,3)))
print("y = " + str(test_set_y[0,index])+", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") +  "\" picture.")
plt.show()

"""
我们还可以打印出梯度和损失函数:
"""
# Plot learning curve (with costs)
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()


learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
    print ("learning rate is: " + str(i))
    models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False)
    print ('\n' + "-------------------------------------------------------" + '\n')

for i in learning_rates:
    plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))

plt.ylabel('cost')
plt.xlabel('iterations')

legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()



## START CODE HERE ## (PUT YOUR IMAGE NAME)
my_image = "my_image2.jpg"   # change this to the name of your image file
# We preprocess the image to fit your algorithm.
fname = "D:/BaiduYunDownload/image/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)

plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")
lr_utils.py
import numpy as np
import h5py
def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes





第二版:逻辑回归代码

import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset

train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
# Example of a picture
index = 208
plt.imshow(train_set_x_orig[index])
#这个代码不清楚
print ("y = " + str(train_set_y[:,index]) + ", it's a '" + classes[np.squeeze(train_set_y[:,index])].decode("utf-8") +  "' picture.")
m_train = None
m_test = None
num_px = None
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

print("-----------数据集信息------------------")
print(str(m_train))
print(str(m_test))
print(str(num_px))
print("(" + str(num_px) + "," + str(num_px) + ",3"+")")
print(str(train_set_x_orig.shape))
print(train_set_y.shape)
print(str(test_set_x_orig.shape))
print(test_set_y.shape)
print("-----------------------------")


train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

print("---------------reshape---------")
print(str(train_set_x_flatten.shape))
print(str(train_set_y.shape))
print(str(test_set_x_flatten.shape))
print(str(test_set_y.shape))
print(str(train_set_x_flatten[0:5,0]))
print("------------------------")

train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255

def sigmoid(z):
    s = 1/(1 + np.exp(-z))
    return s
print("------------测试激活函数-------------")
print("sigmoid([0, 2]) = " + str(sigmoid(np.array([0,2]))))
print("------------测试激活函数------------")

def initialize_with_zeros(dim):
    w, b = np.zeros((dim,1)), 0
    assert(w.shape == (dim, 1))
    assert (isinstance(b, float) or isinstance(b, int))
    return w, b

print("----------测试初始化-----------")
dim = 2
w, b = initialize_with_zeros(dim)
print("w = " + str(w))
print(str(w.shape))
print("b = " + str(b))
print("----------测试初始化-----------")

def propagate(w, b, X, Y):
    m = X.shape[1]
    A = sigmoid(np.dot(w.T, X)+b)
    cost = -(1/m)*np.sum(Y*np.log(A) + (1-Y)*np.log(1-A))

    dw = 1/m*np.dot(X,(A - Y).T)
    db = 1/m*np.sum(A - Y)

    assert (dw.shape == w.shape)
    assert (db.dtype == float)
    cost = np.squeeze(cost)
    assert (cost.shape == ())
    grads = {"dw": dw,
             "db": db}
    return grads, cost

print("------------传播---------------------")
w, b, X, Y, = np.array([[1.],[2.]]), 2., np.array([[1.,2.,-1.], [3.,4.,-3.2]]), np.array([[1,0,1]])
grads, cost = propagate(w, b, X, Y)
print("dw = " + str(grads["dw"]))
print("db = " + str(grads["db"]))
print("cost = " + str(cost))
print("------------传播---------------------")

def optimizie(w, b, X, Y,num_iterations, learning_rate, print_cost = False):

    costs = []
    for i in range(num_iterations):
        grads, cost = propagate(w, b, X, Y)
        dw = grads["dw"]
        db =grads["db"]
        w = w - learning_rate * dw
        b = b - learning_rate * db

        if i % 100 == 0:
            costs.append(cost)
        if print_cost and i % 100 == 0:
            print("Cost after iteration %i: %f" % (i, cost))

    params ={"w": w,
             "b": b}
    grads = {
             "dw": dw,
             "db": db}

    return params, grads, costs

params, grads, costs = optimizie(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)

print("-----------------优化---------------")
print("w = " + str(params["w"]))
print("b = " + str(params["b"]))
print("dw= " + str(grads["dw"]))
print("db = " + str(grads["db"]))
print("-----------------优化---------------")

def predict(w, b, X):
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)
    A = sigmoid(np.dot(w.T, X) + b)

    # print("A.shape:" + str(A.shape))
    # print("A[0,0]"+ str(A[0,2]))
    # A[0,0],A[0,1],A[0,2] 遍历了 A

    for i in range(A.shape[1]):
        if A[0, i]<=0.5:
            Y_prediction[0, i] = 0
        else:
            Y_prediction[0, i] = 1

    assert(Y_prediction.shape == (1, m))
    return Y_prediction

print("--------------------预测--------------")
w = np.array([[0.1124579],[0.23106775]])
b = -0.3
x = np.array([[1.,-1.1,-3.2],[1.2,2.,0.1]])
print ("predictions = " + str(predict(w, b, X)))
print("--------------------预测--------------")

def model(X_train, Y_train, X_test, Y_test, num_iterations = 200, learning_rate = 0.5, print_cost = False):
    w, b =initialize_with_zeros(X_train.shape[0])
    parameters, grads, costs = optimizie(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = parameters["w"]
    b = parameters["b"]

    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
    print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}

    return d


d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations =1000, learning_rate = 0.005, print_cost = True)

print("-------------尝试不同的学习率-------------")
learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
    print ("learning rate is: " + str(i))
    models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False)
    print ('\n' + "-------------------------------------------------------" + '\n')

for i in learning_rates:
    plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))

plt.ylabel('cost')
plt.xlabel('iterations')

legend = plt.legend(loc='upper center', shadow= True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()



## START CODE HERE ## (PUT YOUR IMAGE NAME)
my_image = "my_cat.jpg"   # change this to the name of your image file
## END CODE HERE ##

# We preprocess the image to fit your algorithm.
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T   #待解释
my_predicted_image = predict(d["w"], d["b"], my_image)

plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")
















猜你喜欢

转载自blog.csdn.net/gz153016/article/details/83829808