请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3365785 新帖:17

神经网络多因子选股模型-L2(relu-sigmoid)

Tango发表于:5 月 9 日 21:07回复(1)

应用二层神经网络模型进行多因子选股

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import jqdata
import datetime
from jqlib.alpha101 import *
stocks = get_index_stocks('000300.XSHG')
factors = ['B/M','PEG','ROE','ROA','GP/R','P/R','L/A','FAP','CMV',]
#从聚宽数据库中正确读取股票及因子数据,以及对应的前五日收益率数据
def get_factors(fdate,factors):
    q = query(
        valuation.code,
        balance.total_owner_equities/valuation.market_cap/100000000,
        valuation.pe_ratio,
        income.net_profit/balance.total_owner_equities,
        income.net_profit/balance.total_assets,
        income.total_profit/income.operating_revenue,
        income.net_profit/income.operating_revenue,
        balance.total_liability/balance.total_assets,
        balance.fixed_assets/balance.total_assets,
        valuation.circulating_market_cap
        ).filter(
        valuation.code.in_(stocks),
        valuation.circulating_market_cap
    )
    fdf = get_fundamentals(q, date=fdate)
    fdf.index = fdf['code']
    fdf.columns = ['code'] + factors
    return fdf.iloc[:,-9:]

#整理前五日收益率数据,并分类为是否上涨超过0.1%
def timereturn(fdate):
    d1 = datetime.datetime.strptime(fdate, '%Y-%m-%d')
    delta1 = datetime.timedelta(days=1)
    delta2 = datetime.timedelta(days=15)
    d2=d1+delta1
    d3=d1+delta2
    ndate=d2.strftime('%Y-%m-%d')
    bdate=d3.strftime('%Y-%m-%d')
    getprice=get_price(stocks, start_date=ndate, end_date=bdate,frequency='daily', fields="open")["open"]
    getprice_array=getprice.values
    qw=np.matrix(getprice_array)
    arrt=((qw[-1]-qw[0])/qw[0])/qw.shape[0]
    arr=np.asarray(arrt)
    qwer= np.zeros((1,arr.shape[1]))
    for i in range(0, arr.shape[1]):
        if arr[0,i] > 0.001:
            qwer[0,i] = 1
        else:
            qwer[0,i] = 0
    return qwer

#把股票因子数据标准化,以及调整好对应的矩阵格式
def normalize(fdate, factors):
    x_origin=get_factors(fdate,factors).values.T
    x_norm=np.linalg.norm(x_origin,ord=2,axis=1,keepdims=True)
    x_normalize=np.asarray(np.matrix(x_origin/x_norm))
    return x_normalize
fdf = get_factors('2017-05-01',factors).T
fdf
code 000001.XSHE 000002.XSHE 000008.XSHE 000009.XSHE 000060.XSHE 000063.XSHE 000069.XSHE 000100.XSHE 000156.XSHE 000157.XSHE ... 601966.XSHG 601985.XSHG 601988.XSHG 601989.XSHG 601992.XSHG 601997.XSHG 601998.XSHG 603160.XSHG 603858.XSHG 603993.XSHG
B/M 1.345791 0.752503 0.272918 0.459739 0.361204 0.561015 0.735843 1.095866 0.438841 1.103120 ... 0.307625 0.614951 1.407096 0.447343 0.730436 0.609225 1.334635 0.064358 0.236535 0.470045
PEG 6.790000 10.310000 43.680000 76.180000 70.370000 -35.510000 9.490000 23.960000 38.510000 -182.260000 ... 27.060000 25.670000 6.420000 169.240000 28.990000 9.880000 7.040000 49.620000 30.400000 51.100000
ROE 0.029913 0.006847 0.001545 0.010635 0.020260 0.031588 0.016507 0.014312 0.014598 0.002669 ... 0.030912 0.029301 0.021850 0.003697 0.000952 0.044537 0.028975 0.093027 0.017053 0.028595
ROA 0.002067 0.001251 0.001199 0.003895 0.009809 0.009331 0.005143 0.004476 0.010663 0.001076 ... 0.013331 0.007616 0.001790 0.001135 0.000286 0.002632 0.001981 0.079176 0.012880 0.011337
GP/R 0.296983 0.084540 0.063890 0.083721 0.056622 0.067274 0.184716 0.036511 0.211030 0.020359 ... 0.084875 0.324687 0.344780 0.027263 0.023045 0.370259 0.392027 0.290730 0.118384 0.258205
P/R 0.224235 0.059667 0.047270 0.069608 0.041793 0.051174 0.131967 0.026503 0.209829 0.019206 ... 0.075649 0.272489 0.283633 0.020301 0.005291 0.316497 0.301551 0.265697 0.090828 0.172710
L/A 0.930896 0.817327 0.223980 0.633788 0.515829 0.704621 0.688432 0.687253 0.269552 0.596915 ... 0.568762 0.740073 0.918062 0.693024 0.699342 0.940901 0.931645 0.148898 0.244703 0.603509
FAP 0.002701 0.007352 0.022440 0.133168 0.299620 0.054354 0.089995 0.244376 0.175995 0.077610 ... 0.495244 0.443188 0.010739 0.175845 0.215733 0.006693 0.003074 0.046492 0.074343 0.303987
CMV 1520.930100 1893.270000 162.410000 177.870000 227.350000 608.380000 499.120000 315.950000 161.360000 281.260000 ... 44.740000 327.370000 7566.480000 1292.660000 658.140000 78.550000 1920.689900 43.000000 55.990000 569.960000

9 rows × 300 columns

exa=timereturn("2017-05-01")
exa
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,
         0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,
         1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,
         0.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,
         0.,  1.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,
         0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,
         0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,
         1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,
         1.,  1.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,
         0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,
         0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,
         0.,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,
         0.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,
         0.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  0.,
         1.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,
         0.,  1.,  0.,  1.,  1.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,
         0.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  1.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,
         0.]])
normalize("2017-05-01", factors)
array([[ 0.13021727,  0.07281136,  0.02640728, ...,  0.00622721,
         0.02288686,  0.04548103],
       [ 0.00251526,  0.0038192 ,  0.01618066, ...,  0.01838105,
         0.01126127,  0.0189293 ],
       [ 0.04550646,  0.01041694,  0.00235047, ...,  0.14152401,
         0.02594237,  0.04350142],
       ..., 
       [ 0.08855955,  0.07775528,  0.02130801, ...,  0.01416521,
         0.02327952,  0.05741403],
       [ 0.00066104,  0.00179946,  0.00549241, ...,  0.01137935,
         0.0181961 ,  0.07440379],
       [ 0.0546309 ,  0.06800513,  0.00583367, ...,  0.00154453,
         0.00201113,  0.02047262]])
#开始构建两层神经网络
# 定义处理好train_data, test_data, train_data
train_x=normalize("2017-05-09", factors)
train_y=timereturn("2017-05-20")
test_x=normalize("2017-05-09", factors)
test_y=timereturn('2017-05-20')

### 定义初始设定 ###
n_x = 9     # feature数量
n_h = 7
n_y = 1
layers_dims = (n_x, n_h, n_y)
# initialize_parameters
def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- size of the input layer
    n_h -- size of the hidden layer
    n_y -- size of the output layer
    
    Returns:
    parameters -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """
    
    np.random.seed(1)
    
    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = np.random.randn(n_h, n_x)*0.01
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(n_y, n_h)*0.01
    b2 = np.zeros((n_y, 1))
    ### END CODE HERE ###
    
    assert(W1.shape == (n_h, n_x))
    assert(b1.shape == (n_h, 1))
    assert(W2.shape == (n_y, n_h))
    assert(b2.shape == (n_y, 1))
    
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}
    
    return parameters   
# GRADED FUNCTION: initialize_parameters_deep
def initialize_parameters_deep(layer_dims):
    """
    Arguments:
    layer_dims -- python array (list) containing the dimensions of each layer in our network
    
    Returns:
    parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
                    Wl -- weight matrix of shape (layer_dims[l], layer_dims[l-1])
                    bl -- bias vector of shape (layer_dims[l], 1)
    """
    
    np.random.seed(3)
    parameters = {}
    L = len(layer_dims)            # number of layers in the network
    for l in range(1, L):
        ### START CODE HERE ### (≈ 2 lines of code)
        parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])*0.01
        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
        ### END CODE HERE ###
        
        assert(parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l-1]))
        assert(parameters['b' + str(l)].shape == (layer_dims[l], 1))
        
    return parameters
# GRADED FUNCTION: linear_forward
def linear_forward(A, W, b):
    """
    Implement the linear part of a layer's forward propagation.
    Arguments:
    A -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    Returns:
    Z -- the input of the activation function, also called pre-activation parameter 
    cache -- a python dictionary containing "A", "W" and "b" ; stored for computing the backward pass efficiently
    """
    
    ### START CODE HERE ### (≈ 1 line of code)
    Z = np.dot(W, A)+b
    ### END CODE HERE ###
    
    assert(Z.shape == (W.shape[0], A.shape[1]))
    cache = (A, W, b)
    
    return Z, cache
# GRADED FUNCTION: linear_activation_forward
def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer
    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
    Returns:
    A -- the output of the activation function, also called the post-activation value 
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """
    
    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
        ### END CODE HERE ###
    
    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
        ### END CODE HERE ###
    
    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)
    return A, cache
# GRADED FUNCTION: L_model_forward
def L_model_forward(X, parameters):
    """
    Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation
    
    Arguments:
    X -- data, numpy array of shape (input size, number of examples)
    parameters -- output of initialize_parameters_deep()
    
    Returns:
    AL -- last post-activation value
    caches -- list of caches containing:
                every cache of linear_relu_forward() (there are L-1 of them, indexed from 0 to L-2)
                the cache of linear_sigmoid_forward() (there is one, indexed L-1)
    """
    caches = []
    A = X
    L = len(parameters) // 2                  # number of layers in the neural network
    
    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    for l in range(1, L):
        A_prev = A 
        ### START CODE HERE ### (≈ 2 lines of code)
        A, cache = linear_activation_forward(A_prev, parameters["W1"], parameters["b1"], activation = "relu")
        caches.append(cache)
        
        ### END CODE HERE ###
    
    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    ### START CODE HERE ### (≈ 2 lines of code)
    AL, cache = linear_activation_forward(A, parameters["W2"], parameters["b2"], activation = "sigmoid")
    caches.append(cache)
    ### END CODE HERE ###
    
    assert(AL.shape == (1,X.shape[1]))
            
    return AL, caches
# GRADED FUNCTION: compute_cost
def compute_cost(AL, Y):
    """
    Implement the cost function defined by equation (7).
    Arguments:
    AL -- probability vector corresponding to your label predictions, shape (1, number of examples)
    Y -- true "label" vector (for example: containing 0 if non-cat, 1 if cat), shape (1, number of examples)
    Returns:
    cost -- cross-entropy cost
    """
    
    m = Y.shape[1]
    # Compute loss from aL and y.
    ### START CODE HERE ### (≈ 1 lines of code)
    cost = -(np.dot(Y, np.log(AL).T)+np.dot((1-Y), np.log(1-AL).T))/m
    ### END CODE HERE ###
    
    cost = np.squeeze(cost)      # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).
    assert(cost.shape == ())
    return cost
# GRADED FUNCTION: linear_backward
def linear_backward(dZ, cache):
    """
    Implement the linear portion of backward propagation for a single layer (layer l)
    Arguments:
    dZ -- Gradient of the cost with respect to the linear output (of current layer l)
    cache -- tuple of values (A_prev, W, b) coming from the forward propagation in the current layer
    Returns:
    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
    dW -- Gradient of the cost with respect to W (current layer l), same shape as W
    db -- Gradient of the cost with respect to b (current layer l), same shape as b
    """
    A_prev, W, b = cache
    m = A_prev.shape[1]
    ### START CODE HERE ### (≈ 3 lines of code)
    dW = np.dot(dZ, A_prev.T)/m
    db = np.sum(dZ, axis=1, keepdims=True)/m
    dA_prev = np.dot(W.T, dZ)
    ### END CODE HERE ###
    
    assert (dA_prev.shape == A_prev.shape)
    assert (dW.shape == W.shape)
    assert (db.shape == b.shape)
    
    return dA_prev, dW, db
# GRADED FUNCTION: linear_activation_backward
def linear_activation_backward(dA, cache, activation):
    """
    Implement the backward propagation for the LINEAR->ACTIVATION layer.
    
    Arguments:
    dA -- post-activation gradient for current layer l 
    cache -- tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
    
    Returns:
    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
    dW -- Gradient of the cost with respect to W (current layer l), same shape as W
    db -- Gradient of the cost with respect to b (current layer l), same shape as b
    """
    linear_cache, activation_cache = cache
    
    if activation == "relu":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = relu_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward(dZ, linear_cache)
        ### END CODE HERE ###
        
    elif activation == "sigmoid":
        ### START CODE HERE ### (≈ 2 lines of code)
        dZ = sigmoid_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward(dZ, linear_cache)
        ### END CODE HERE ###
    
    return dA_prev, dW, db
# GRADED FUNCTION: L_model_backward
def L_model_backward(AL, Y, caches):
    """
    Implement the backward propagation for the [LINEAR->RELU] * (L-1) -> LINEAR -> SIGMOID group
    
    Arguments:
    AL -- probability vector, output of the forward propagation (L_model_forward())
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat)
    caches -- list of caches containing:
                every cache of linear_activation_forward() with "relu" (it's caches[l], for l in range(L-1) i.e l = 0...L-2)
                the cache of linear_activation_forward() with "sigmoid" (it's caches[L-1])
    
    Returns:
    grads -- A dictionary with the gradients
             grads["dA" + str(l)] = ... 
             grads["dW" + str(l)] = ...
             grads["db" + str(l)] = ... 
    """
    grads = {}
    L = len(caches) # the number of layers
    m = AL.shape[1]
    Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL
    
    # Initializing the backpropagation
    ### START CODE HERE ### (1 line of code)
    dAL = -(np.divide(Y, AL)-np.divide(1-Y, 1-AL))
    ### END CODE HERE ###
    
    # Lth layer (SIGMOID -> LINEAR) gradients. Inputs: "AL, Y, caches". Outputs: "grads["dAL"], grads["dWL"], grads["dbL"]
    ### START CODE HERE ### (approx. 2 lines)
    current_cache = caches[1]
    grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL, current_cache, activation = "sigmoid")
    ### END CODE HERE ###
    
    for l in reversed(range(L-1)):
        # lth layer: (RELU -> LINEAR) gradients.
        # Inputs: "grads["dA" + str(l + 2)], caches". Outputs: "grads["dA" + str(l + 1)] , grads["dW" + str(l + 1)] , grads["db" + str(l + 1)] 
        ### START CODE HERE ### (approx. 5 lines)
        current_cache = caches[0]
        dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(L)], current_cache, activation = "relu")
        grads["dA" + str(l + 1)] = dA_prev_temp
        grads["dW" + str(l + 1)] = dW_temp
        grads["db" + str(l + 1)] = db_temp
        ### END CODE HERE ###
    return grads
# GRADED FUNCTION: update_parameters
def update_parameters(parameters, grads, learning_rate):
    """
    Update parameters using gradient descent
    
    Arguments:
    parameters -- python dictionary containing your parameters 
    grads -- python dictionary containing your gradients, output of L_model_backward
    
    Returns:
    parameters -- python dictionary containing your updated parameters 
                  parameters["W" + str(l)] = ... 
                  parameters["b" + str(l)] = ...
    """
    
    L = len(parameters) // 2 # number of layers in the neural network
    # Update rule for each parameter. Use a for loop.
    ### START CODE HERE ### (≈ 3 lines of code)
    for l in range(L):
        parameters["W" + str(l+1)] = parameters["W" + str(l+1)]-grads["dW" + str(l+1)]*learning_rate
        parameters["b" + str(l+1)] = parameters["b" + str(l+1)]-grads["db" + str(l+1)]*learning_rate
    ### END CODE HERE ###
    
    return parameters
def sigmoid(Z):
    """
    Implements the sigmoid activation in numpy
    
    Arguments:
    Z -- numpy array of any shape
    
    Returns:
    A -- output of sigmoid(z), same shape as Z
    cache -- returns Z as well, useful during backpropagation
    """
    
    A = 1/(1+np.exp(-Z))
    cache = Z
    
    return A, cache
def relu(Z):
    """
    Implement the RELU function.

    Arguments:
    Z -- Output of the linear layer, of any shape

    Returns:
    A -- Post-activation parameter, of the same shape as Z
    cache -- a python dictionary containing "A" ; stored for computing the backward pass efficiently
    """
    
    A = np.maximum(0,Z)
    
    assert(A.shape == Z.shape)
    
    cache = Z 
    return A, cache
def relu_backward(dA, cache):
    """
    Implement the backward propagation for a single RELU unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """
    
    Z = cache
    dZ = np.array(dA, copy=True) # just converting dz to a correct object.
    
    # When z <= 0, you should set dz to 0 as well. 
    dZ[Z <= 0] = 0
    
    assert (dZ.shape == Z.shape)
    
    return dZ
def sigmoid_backward(dA, cache):
    """
    Implement the backward propagation for a single SIGMOID unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """
    
    Z = cache
    
    s = 1/(1+np.exp(-Z))
    dZ = dA * s * (1-s)
    
    assert (dZ.shape == Z.shape)
    
    return dZ
def predict(X, y, parameters):
    """
    This function is used to predict the results of a  L-layer neural network.
    
    Arguments:
    X -- data set of examples you would like to label
    parameters -- parameters of the trained model
    
    Returns:
    p -- predictions for the given dataset X
    """
    
    m = X.shape[1]
    n = len(parameters) // 2 # number of layers in the neural network
    p = np.zeros((1,m))
    
    # Forward propagation
    probas, caches = L_model_forward(X, parameters)

    
    # convert probas to 0/1 predictions
    for i in range(0, probas.shape[1]):
        if probas[0,i] > 0.5:
            p[0,i] = 1
        else:
            p[0,i] = 0
    
    #print results
    #print ("predictions: " + str(p))
    #print ("true labels: " + str(y))
    print("Accuracy: "  + str(np.sum((p == y)/m)))
        
    return p
def print_mislabeled_images(classes, X, y, p):
    """
    Plots images where predictions and truth were different.
    X -- dataset
    y -- true labels
    p -- predictions
    """
    a = p + y
    mislabeled_indices = np.asarray(np.where(a == 1))
    plt.rcParams['figure.figsize'] = (40.0, 40.0) # set default size of plots
    num_images = len(mislabeled_indices[0])
    for i in range(num_images):
        index = mislabeled_indices[1][i]
        
        plt.subplot(2, num_images, i + 1)
        plt.imshow(X[:,index].reshape(64,64,3), interpolation='nearest')
        plt.axis('off')
        plt.title("Prediction: " + classes[int(p[0,index])].decode("utf-8") + " \n Class: " + classes[y[0,index]].decode("utf-8"))
 
# GRADED FUNCTION: two_layer_model

def two_layer_model(X, Y, layers_dims, learning_rate = 0.2, num_iterations = 3000, print_cost=False):
    """
    Implements a two-layer neural network: LINEAR->RELU->LINEAR->SIGMOID.
    
    Arguments:
    X -- input data, of shape (n_x, number of examples)
    Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)
    layers_dims -- dimensions of the layers (n_x, n_h, n_y)
    num_iterations -- number of iterations of the optimization loop
    learning_rate -- learning rate of the gradient descent update rule
    print_cost -- If set to True, this will print the cost every 100 iterations 
    
    Returns:
    parameters -- a dictionary containing W1, W2, b1, and b2
    """
    
    np.random.seed(1)
    grads = {}
    costs = []                              # to keep track of the cost
    m = X.shape[1]                           # number of examples
    (n_x, n_h, n_y) = layers_dims
    
    # Initialize parameters dictionary, by calling one of the functions you'd previously implemented
    ### START CODE HERE ### (≈ 1 line of code)
    parameters = initialize_parameters(n_x, n_h, n_y)
    ### END CODE HERE ###
    
    # Get W1, b1, W2 and b2 from the dictionary parameters.
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    
    # Loop (gradient descent)

    for i in range(0, num_iterations):

        # Forward propagation: LINEAR -> RELU -> LINEAR -> SIGMOID. Inputs: "X, W1, b1". Output: "A1, cache1, A2, cache2".
        ### START CODE HERE ### (≈ 2 lines of code)
        A1, cache1 = linear_activation_forward(X, W1, b1, activation="relu")
        A2, cache2 = linear_activation_forward(A1, W2, b2, activation="sigmoid")
        ### END CODE HERE ###
        
        # Compute cost
        ### START CODE HERE ### (≈ 1 line of code)
        cost = compute_cost(A2, Y)
        ### END CODE HERE ###
        
        # Initializing backward propagation
        dA2 = - (np.divide(Y, A2) - np.divide(1 - Y, 1 - A2))
        
        # Backward propagation. Inputs: "dA2, cache2, cache1". Outputs: "dA1, dW2, db2; also dA0 (not used), dW1, db1".
        ### START CODE HERE ### (≈ 2 lines of code)
        dA1, dW2, db2 = linear_activation_backward(dA2, cache2, activation="sigmoid")
        dA0, dW1, db1 = linear_activation_backward(dA1, cache1, activation="relu")
        ### END CODE HERE ###
        
        # Set grads['dWl'] to dW1, grads['db1'] to db1, grads['dW2'] to dW2, grads['db2'] to db2
        grads['dW1'] = dW1
        grads['db1'] = db1
        grads['dW2'] = dW2
        grads['db2'] = db2
        
        # Update parameters.
        ### START CODE HERE ### (approx. 1 line of code)
        parameters = update_parameters(parameters, grads, learning_rate)
        ### END CODE HERE ###

        # Retrieve W1, b1, W2, b2 from parameters
        W1 = parameters["W1"]
        b1 = parameters["b1"]
        W2 = parameters["W2"]
        b2 = parameters["b2"]
        
        # Print the cost every 100 training example
        if print_cost and i % 1000 == 0:
            print("Cost after iteration {}: {}".format(i, np.squeeze(cost)))
        if print_cost and i % 1000 == 0:
            costs.append(cost)
       
    # plot the cost

    plt.plot(np.squeeze(costs))
    plt.ylabel('cost')
    plt.xlabel('iterations (per tens)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()    
    return A2
parameters = two_layer_model(train_x, train_y, layers_dims = (n_x, n_h, n_y), num_iterations = 40000, print_cost=True)
Cost after iteration 0: 0.6931474560430294
Cost after iteration 1000: 0.6896322636297301
Cost after iteration 2000: 0.6177002000589205
Cost after iteration 3000: 0.5139853996403102
Cost after iteration 4000: 0.49530723473136806
Cost after iteration 5000: 0.48936556143299487
Cost after iteration 6000: 0.4911387984519265
Cost after iteration 7000: 0.49108552182848625
Cost after iteration 8000: 0.48838602114796115
Cost after iteration 9000: 0.4872810150676333
Cost after iteration 10000: 0.4859748945933749
Cost after iteration 11000: 0.4849573879674542
Cost after iteration 12000: 0.4841495375127325
Cost after iteration 13000: 0.4836529063518739
Cost after iteration 14000: 0.48293192060104845
Cost after iteration 15000: 0.482524175809503
Cost after iteration 16000: 0.48217032895009565
Cost after iteration 17000: 0.48187907081194564
Cost after iteration 18000: 0.4816243318717754
Cost after iteration 19000: 0.48140263384125054
Cost after iteration 20000: 0.4810400959234755
Cost after iteration 21000: 0.4807993603749547
Cost after iteration 22000: 0.4805072243313054
Cost after iteration 23000: 0.47998415604475003
Cost after iteration 24000: 0.479282751740753
Cost after iteration 25000: 0.47812768259641025
Cost after iteration 26000: 0.47560888830450426
Cost after iteration 27000: 0.47856945948033286
Cost after iteration 28000: 0.4730530826810976
Cost after iteration 29000: 0.470476695853979
Cost after iteration 30000: 0.46805489426579217
Cost after iteration 31000: 0.4663006642145685
Cost after iteration 32000: 0.4635863983349724
Cost after iteration 33000: 0.4652154201897752
Cost after iteration 34000: 0.45970389094614383
Cost after iteration 35000: 0.45930813699955897
Cost after iteration 36000: 0.45763168796341913
Cost after iteration 37000: 0.4558411927638473
Cost after iteration 38000: 0.4550571917242682
Cost after iteration 39000: 0.4533721688936412
parameters
array([[  9.92049251e-01,   9.82050105e-01,   3.86612067e-02,
          1.80540010e-01,   3.44724537e-01,   7.88011214e-01,
          6.45808984e-01,   5.12363141e-01,   1.61467501e-01,
          7.14602137e-01,   6.73875289e-01,   9.93229711e-01,
          6.85118419e-01,   6.06116136e-01,   3.61976884e-01,
          5.36258504e-01,   6.89820750e-01,   2.23353844e-01,
          1.78223705e-10,   7.96845229e-01,   6.00693778e-01,
          6.98769974e-02,   4.12338999e-01,   7.72169208e-01,
          5.57436744e-01,   7.91929633e-01,   3.37982657e-01,
          2.22359010e-01,   9.84604272e-01,   5.06607353e-01,
          4.52914197e-01,   6.26069703e-01,   1.81825567e-01,
          9.40079096e-01,   5.26889558e-01,   3.82842854e-02,
          5.34776475e-01,   8.06238295e-02,   9.41080087e-01,
          6.59759995e-01,   5.36919366e-01,   7.34794695e-02,
          4.07253806e-01,   7.55607485e-02,   9.88308968e-01,
          5.26176085e-01,   6.91526160e-01,   2.04321424e-01,
          1.43628732e-01,   5.18576487e-01,   3.62840390e-01,
          4.45570089e-01,   1.69130732e-01,   5.33030225e-01,
          7.34998745e-01,   6.49166060e-01,   3.74210846e-01,
          1.67110194e-01,   7.40718014e-01,   3.37953576e-03,
          1.11555667e-01,   6.38608582e-01,   3.94400619e-01,
          5.26712899e-01,   1.77796080e-01,   9.30585045e-01,
          6.49608025e-01,   4.41909067e-01,   6.00284410e-02,
          3.29155937e-01,   4.03099911e-01,   4.28514736e-01,
          3.64249539e-01,   8.30680884e-02,   4.51112136e-01,
          4.70992328e-01,   5.56822158e-01,   8.24648152e-02,
          8.53406692e-02,   9.19787275e-01,   1.17240734e-01,
          2.69017633e-01,   2.23305435e-01,   2.05963323e-01,
          9.80581518e-01,   3.85999035e-01,   1.45069715e-01,
          6.82250329e-01,   4.05090287e-01,   6.08990668e-02,
          7.41914717e-01,   4.73173378e-01,   4.15451662e-01,
          3.72207128e-01,   5.09049226e-01,   4.15218821e-01,
          4.89413899e-01,   3.86197784e-01,   2.12102555e-01,
          5.22672158e-01,   3.85349124e-01,   6.02122584e-01,
          4.30929262e-01,   1.29288747e-01,   2.80103544e-01,
          4.38956915e-01,   2.10201212e-01,   2.67071882e-01,
          8.28436259e-02,   4.25985918e-02,   4.96503602e-01,
          3.03118224e-01,   3.23221255e-01,   5.07595162e-01,
          8.94201101e-02,   4.81502077e-01,   2.98409787e-01,
          5.19784758e-01,   8.29712316e-02,   3.52282516e-01,
          1.93892126e-01,   9.99939037e-01,   2.78308751e-01,
          6.76899767e-01,   1.98631041e-01,   9.80893081e-01,
          9.99457951e-01,   9.09935968e-01,   9.63519124e-01,
          3.31593016e-01,   6.36050960e-01,   9.99999259e-01,
          7.81335146e-01,   9.77106857e-01,   6.42977523e-01,
          9.99990131e-01,   1.78768708e-01,   2.05983223e-01,
          9.32796358e-01,   1.66525826e-01,   2.90838647e-01,
          9.87982885e-01,   4.67438619e-01,   6.92692386e-01,
          3.84202070e-01,   5.97247932e-01,   6.47734873e-01,
          1.54679137e-01,   9.99746551e-01,   4.63061671e-01,
          3.74457150e-02,   7.75951822e-01,   1.35070477e-01,
          2.26785072e-01,   5.85329982e-01,   1.94424924e-01,
          5.85641793e-01,   8.25655465e-01,   7.44137718e-01,
          7.19140732e-01,   6.55952732e-01,   5.80178922e-01,
          2.42247225e-01,   1.21420730e-01,   5.25708359e-01,
          9.37037267e-01,   5.60068248e-01,   6.61439322e-01,
          3.94472741e-01,   9.39767826e-01,   6.16498697e-01,
          2.58652138e-01,   2.50866426e-01,   9.70866057e-02,
          5.58586636e-01,   5.07811587e-01,   7.50293051e-01,
          1.24791800e-01,   5.66140225e-01,   5.32708008e-01,
          1.75417241e-02,   9.61779879e-02,   1.62354413e-02,
          1.93717544e-01,   3.35650356e-01,   8.52597520e-01,
          9.99999239e-01,   3.74177196e-01,   5.58239210e-01,
          3.58672556e-01,   2.06365012e-01,   1.62354413e-02,
          2.89191536e-02,   7.98564492e-01,   9.65847169e-03,
          7.27143994e-01,   3.29547520e-01,   8.42909672e-01,
          6.54923322e-01,   7.12490245e-01,   9.99232901e-01,
          8.85233304e-02,   5.28170834e-03,   6.03190909e-01,
          7.96538694e-01,   8.26371241e-01,   4.27087046e-01,
          8.36136933e-01,   5.59485231e-02,   3.82490472e-01,
          6.13788804e-01,   8.12721814e-01,   7.97878852e-01,
          4.09626818e-01,   7.84143620e-01,   4.95203747e-01,
          3.98040708e-01,   9.58794053e-01,   1.06046634e-03,
          8.61193509e-01,   8.57206753e-01,   1.27894539e-01,
          7.02042769e-01,   9.46690097e-01,   2.55469105e-01,
          7.39546741e-01,   6.86222945e-01,   7.12530917e-01,
          1.41909980e-01,   2.75197628e-01,   8.72423735e-01,
          9.49304798e-01,   9.36699198e-01,   5.98381538e-01,
          3.94414799e-01,   9.99866649e-01,   5.01826383e-02,
          7.53731972e-01,   4.61116809e-01,   3.84155326e-02,
          2.59511446e-01,   2.84455832e-01,   1.80668731e-01,
          9.99921006e-01,   9.90627960e-01,   9.71199455e-01,
          4.45306844e-01,   9.21929848e-01,   6.97423539e-01,
          8.92215486e-01,   7.90482920e-01,   1.00000000e+00,
          9.99988628e-01,   9.99411083e-01,   1.25207236e-01,
          8.98499987e-01,   2.01596558e-01,   7.83474975e-01,
          9.76804602e-01,   1.00000000e+00,   5.85438915e-01,
          4.20538287e-01,   9.78503224e-01,   6.48132726e-01,
          2.13973404e-01,   2.51857443e-01,   8.40715381e-01,
          9.99998926e-01,   7.98993948e-01,   9.99488404e-01,
          8.41542987e-01,   8.93395880e-01,   3.22429236e-01,
          6.43930407e-01,   9.88246068e-01,   7.26814665e-01,
          9.89265089e-01,   9.95433533e-01,   1.00000000e+00,
          5.52615775e-01,   5.41228961e-01,   3.70327024e-01,
          4.42948886e-01,   6.24519229e-01,   7.12666413e-01,
          6.28158896e-01,   5.37944435e-01,   5.44977746e-01,
          9.44582944e-01,   1.64516098e-02,   3.33290042e-01,
          6.82261650e-01,   1.00000000e+00,   5.68448188e-01,
          4.81705014e-01,   7.22617424e-01,   9.97547197e-01,
          3.08037974e-01,   1.06906653e-01,   6.61431005e-01]])
 

全部回复

0/140

量化课程

    移动端课程