作业思路整理
复习的时候,把之前的思路给忘了,就重新整理了一下,重点在于搞清楚各个数据和参数的shape!
数据预处理
1. 导入数据
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
Number of training examples: m_train = 209
Number of testing examples: m_test = 50
Height/Width of each image: num_px = 64
Each image is of size: (64, 64, 3)
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)
2.把train和test的格式变一致了
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
train_set_x_flatten shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x_flatten shape: (12288, 50)
test_set_y shape: (1, 50)
sanity check after reshaping: [17 31 56 22 33]
3.standardlize
train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.
算法构建
For one example :
The cost is then computed by summing over all training examples:
Key steps
1. Initialize the parameter of the model
2. Learn the parameters for the model by minimizing the cost
3. Use the learned parameters to make predictions(on the test set)
4. Analyse the results and conclude
步骤
- Define the model structure (such as number of input features)
- Initialize the model’s parameters
- Loop:
- Calculate current loss (forward propagation)
- Calculate current gradient (backward propagation)
- Update parameters (gradient descent)
1.sigmoid
Arguments:
z – A scalar or numpy array of any size.
Return:
s -- sigmoid(z)
2. initializing parameters
Argument:
dim -- size of the w vector we want (or number of parameters in this case)
Returns:
w -- initialized vector of shape (dim, 1)
b -- initialized scalar (corresponds to the bias)
3. Forward and Backward propagation
Forward Propagation:
1. get
2. computa
3. calculate the cost function :
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
4. optimization
update the parameters using gradient descent
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
**参数W是一列**
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.
5.predict
use the learned
,
to predict the labels for a dataset
1. calculate
2. 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
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)
Returns:
Y_prediction -- a numpy array (vector) containing all predictions (0/1) for the examples in X
6. merge all function into a model
Arguments:
X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)
Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train)
X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test)
Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test)
num_iterations -- hyperparameter representing the number of iterations to optimize the parameters
learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize()
print_cost -- Set to true to print the cost every 100 iterations
Returns:
d -- dictionary containing information about the model.