1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| def sigmod(z): s= 1/(1+np.exp(-z)) return s def cast_TD(w,b,X,Y): """ 实现前向和后向传播的成本函数及其梯度。 参数: w - 权重,大小不等的数组(num_px * num_px * 3,1) b - 偏差,一个标量 X - 矩阵类型为(num_px * num_px * 3,训练数量) Y - 真正的“标签”矢量(如果非猫则为0,如果是猫则为1),矩阵维度为(1,训练数据数量)
返回: cost- 逻辑回归的负对数似然成本 dw - 相对于w的损失梯度,因此与w相同的形状 db - 相对于b的损失梯度,因此与b的形状相同 """ m = X.shape[1] A = sigmod(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 == ()) grade = { "dw" : dw, "db" : db } return (grade , cost) def changew(w , b , X , Y , times , alpha , print_cost = False): """ 此函数通过运行梯度下降算法来优化w和b 参数: w - 权重,大小不等的数组(num_px * num_px * 3,1) b - 偏差,一个标量 X - 维度为(num_px * num_px * 3,训练数据的数量)的数组。 Y - 真正的“标签”矢量(如果非猫则为0,如果是猫则为1),矩阵维度为(1,训练数据的数量) times - 优化循环的迭代次数 alpha - 梯度下降更新规则的学习率 print_cost - 每100步打印一次损失值 返回: params - 包含权重w和偏差b的字典 grads - 包含权重和偏差相对于成本函数的梯度的字典 成本 - 优化期间计算的所有成本列表,将用于绘制学习曲线。 提示: 我们需要写下两个步骤并遍历它们: 1)计算当前参数的成本和梯度,使用propagate()。 2)使用w和b的梯度下降法则更新参数。 """ costs = [] for i in range(times): grads,cost =cast_TD(w,b,X,Y) dw = grads["dw"] db = grads["db"] w= w - alpha*dw b = b - alpha*db if i % 100 == 0: costs.append(cost) if(print_cost) and (i%100==0): print("迭代次数:%i , 误差值:%f" % (i,cost)) p = { "w":w, "b":b } grads = { "dw":dw, "db":db } return (p,grads,costs) def initnet(a): w = np.zeros(shape = (a,1)) b= 0 assert(w.shape == (a,1)) assert(isinstance(b,float) or isinstance(b,int)) return (w,b) def yuce(w,b,X): """ 使用学习逻辑回归参数logistic (w,b)预测标签是0还是1, 参数: w - 权重,大小不等的数组(num_px * num_px * 3,1) b - 偏差,一个标量 X - 维度为(num_px * num_px * 3,训练数据的数量)的数据 返回: Y_prediction - 包含X中所有图片的所有预测【0 | 1】的一个numpy数组(向量) """ m = X.shape[1] Yyuce = np.zeros((1,m)) w = w.reshape(X.shape[0],1) A = sigmod(np.dot(w.T,X)+b) for i in range(A.shape[1]): Yyuce[0,i] = 1 if A[0,i]> 0.5 else 0 assert(Yyuce.shape == (1,m)) return Yyuce def logitic(X_train,Y_train,times=2000,alpha = 0.1,print_cost = False): """ 通过调用之前实现的函数来构建逻辑回归模型 参数: X_train - numpy的数组,维度为(num_px * num_px * 3,m_train)的训练集 Y_train - numpy的数组,维度为(1,m_train)(矢量)的训练标签集 X_test - numpy的数组,维度为(num_px * num_px * 3,m_test)的测试集 Y_test - numpy的数组,维度为(1,m_test)的(向量)的测试标签集 times - 表示用于优化参数的迭代次数的超参数 alpha - 表示logitic()更新规则中使用的学习速率的超参数 print_cost - 设置为true以每100次迭代打印成本 返回: res - 包含有关模型信息的字典。 """ w , b = initnet(X_train.shape[0]) p,grads,costs = changew(w,b,X_train,Y_train,times,alpha,print_cost) w , b = p["w"],p["b"]
Yyuce_train = yuce(w,b,X_train)
print("训练集准确性:",format(100 - np.mean(np.abs(Y_train-Yyuce_train))*100)+"%") d = { "costs":costs, "Yyuce_train":Yyuce_train, "w":w, "b":b, "alpha":alpha, "times":times } return d
|