def train_Linear(x_list, gt_y_list, batch_size, lr, max_iter): """ :param x_list: :param gt_y_list: :param batch_size: 全部的值 :param lr: 学习率 :param max_iter: 迭代次数,要更新的次数 :return: """ w, b = 0, 0 num_samples = len(x_list) # plt.ion() # fig, ax = plt.subplots() # plt.rcParams['lines.markersize'] = 3 for i in range(max_iter): # 随机抽取(batch_size)个样本 batch_idxs = np.random.choice(num_samples, batch_size) batch_x = [x_list[j] for j in batch_idxs] batch_y = [gt_y_list[j] for j in batch_idxs] w, b = cal_step_gradient(batch_x, batch_y, w, b, lr) s1 = 'w:{}, b:{}'.format(w, b) s2 = 'loss is {}'.format(eval_loss(w, b, x_list, gt_y_list)) log('s_1', s1, '\n', 's_2', s2) # ax.cla() # time.sleep(1) return w, b
def gen_sample_data_Logisitic(clas): """ 数据生成条件 :clas: 类别数量 :return: """ # w边界 w_s = [0] * 4 w_e = [10, 10, -10, -10] # b边界 b_s = [0] * 4 b_e = [10] * 4 # 数据规模 num_pample = 50 # x范围 x_s = [0, 60, 0, 60] x_e = [40, 80, 40, 80] x_list = [] y_list = [] c = [0, 50] for i in range(clas): log('i', i) x, y = gen_sample_data(w_s[i], w_e[i], b_s[i], b_e[i], x_s[i], x_e[i], num_pample, c[i]) x_list += x y_list += y # log(x_list) # log(y_list) return x_list, y_list
def logistic_regression(datax, label, num_steps, learning_rate): real_datax = np.mat(np.insert(datax, 0, 1, axis=1)) log('real_datax', real_datax) real_label = np.mat(label).transpose() #将一维数组转换为二维数组,并转置 log('real_label', real_label) params = np.ones( (np.shape(real_datax)[1], 1)) #生成一个数据为1的,real_xdata行 1 列的二维数组。 # log('params', params) plt.ion() #起到多开窗口的作用 fig, ax = plt.subplots() plt.rcParams['lines.markersize'] = 3 for step in range(num_steps): scores = real_datax * params predictions = sigmoid(scores) params = params + learning_rate * real_datax.transpose() * ( real_label - predictions) log('params', params) x1_min = np.min(datax[:, 0]) x1_max = np.max(datax[:, 0]) x2_min = np.min(datax[:, 1]) x2_max = np.max(datax[:, 1]) plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) x_line = np.linspace(x1_min, x1_max, 1000) y_line = (-params[0, 0] - params[1, 0] * x_line) / params[2, 0] plt.plot(x_line, y_line) plt.title(str(step) + '--iterations', fontsize='xx-large') plt.scatter(real_xdata[:, 0], real_xdata[:, 1], c=label, alpha=.4) plt.pause(1) ax.cla() return params
def eval_loss(w, b, x_list, gt_y_list): """ cost function, 做一个Loss计算 :param w: 线性回归的斜率,x的系数 :param b: 与x轴的截距 :param x_list: x的集合 :param gt_y_list: 实际的y值集合 gt : ground trues 实际值 :return: 所有的gt_y和pred_y的方差和 """ avg_loss = 0 for i in range(len(x_list)): pred_y = sigmoid(w, b, x_list[i]) avg_loss += -gt_y_list[i] * np.log(pred_y) - ( 1 - gt_y_list[i]) * log(1 - pred_y) avg_loss /= len(gt_y_list) return avg_loss
import numpy as np import matplotlib.pyplot as plt from out_put import log np.random.seed(125) sample_num = 1000 x1 = np.random.multivariate_normal([0, 1], [[1, 0.5], [0.5, 1]], sample_num) #多元正太分布 log('x1', x1) x2 = np.random.multivariate_normal([4, 7], [[1.7, 1.5], [1.5, 1.7]], sample_num) log('x2', x2) real_xdata = np.vstack((x1, x2)).astype(np.float32) #x log('real_xdata', real_xdata) real_label = np.hstack((np.zeros(sample_num), np.ones(sample_num))) log('real_label', real_label) plt.figure(figsize=(10, 6)) plt.scatter(real_xdata[:, 0], real_xdata[:, 1], c=real_label, alpha=.4) plt.show() log('---------------------------------------------------------') def sigmoid(scores): return 1 / (1 + np.exp(-scores)) def logistic_regression(datax, label, num_steps, learning_rate):
def high_medianblur_2(lay): row, colomn = lay.shape # log('row', row, 'colomn', colomn) j = 1 v = 0 count = array() end_n = np.empty(shape=[0, colomn - 2], dtype=np.uint8) #二维空数组创建 for i in range(1, row - 1): middle_n = [0] * (colomn - 2) # log('row', i) #使用蛇形方式取值 if j == 0: j += 1 elif j == (colomn - 1): j -= 1 while j > 0 and j < (colomn - 1): # log('colomn', j) #对第一个3*3矩阵取值 if i == 1 and j == 1: i_i = i - 1 j_j = j - 1 for k in range(3): for d in range(3): s = lay[i_i + k][j_j + d] count = count_add(s, count) j += 1 v += 1 # 第一行之后的后边缘窗口处理 elif j == (colomn - 2) and (i % 2) == 0: j_j = j - 1 for s in range(3): count = count_minus(lay[i - 2][j_j + s], count) count = count_add(lay[i + 1][j_j + s], count) j -= 1 # 第一行之后的前边缘窗口处理 elif j == 1 and (i % 2) != 0: j_j = j - 1 for s in range(3): count = count_minus(lay[i - 2][j_j + s], count) count = count_add(lay[i + 1][j_j + s], count) j += 1 # 偶数行窗口处理,窗口逆向向行进 elif (i % 2) == 0: i_i = i - 1 for s in range(3): count = count_minus(lay[i_i + s][j + 2], count) count = count_add(lay[i_i + s][j - 1], count) j -= 1 v -= 1 # 奇数行窗口处理,窗口正向行进 else: i_i = i - 1 for s in range(3): count = count_minus(lay[i_i + s][j - 2], count) count = count_add(lay[i_i + s][j + 1], count) j += 1 v += 1 #取数组中间值 middle = median(count) # log('middle', middle) # 将中间值组成二维数组的行数组 middle_n[v - 1] = middle # i = 340 # log('middle_n', middle_n) # 将中间值的行数组填充入二维数组 end_n = np.append(end_n, [middle_n], axis=0) log('end_n_hing_2', end_n) return end_n
def high_medianblur_1(lay): row, colomn = lay.shape # log('row', row, 'colomn', colomn) j = 0 v = 0 end_n = np.empty(shape=[0, colomn - 2], dtype=np.uint8) #二维空数组建立 for i in range(1, row - 1): # log('row', i) middle_n = [0] * (colomn - 2) #使用蛇形方式取值 if j == 0: j += 1 elif j == (colomn - 1): j -= 1 while j > 0 and j < (colomn - 1): # log('colomn', j) #初始窗口遍历 if i == 1 and j == 1: a = [] i_i = i - 1 j_j = j - 1 for k in range(3): for d in range(3): s = lay[i_i + k][j_j + d] a.append(s) # log(a) j += 1 v += 1 #第一行之后的后边缘窗口处理 elif j == (colomn - 2) and (i % 2) == 0: for s in range(6): a[s] = a[s + 3] a[6] = lay[i + 1][j - 1] a[7] = lay[i + 1][j] a[8] = lay[i + 1][j + 1] j -= 1 # 第一行之后的前边缘窗口处理 elif j == 1 and (i % 2) != 0: for s in range(6): a[s] = a[s + 3] a[6] = lay[i + 1][j - 1] a[7] = lay[i + 1][j] a[8] = lay[i + 1][j + 1] j += 1 #偶数行窗口处理,窗口逆向向行进 elif (i % 2) == 0: a[2] = a[1] a[5] = a[4] a[8] = a[7] a[1] = a[0] a[4] = a[3] a[7] = a[6] a[0] = lay[i - 1][j - 1] a[3] = lay[i][j - 1] a[6] = lay[i + 1][j - 1] j -= 1 v -= 1 #奇数行窗口处理,窗口正向行进 else: a[0] = a[1] a[3] = a[4] a[6] = a[7] a[1] = a[2] a[4] = a[5] a[7] = a[8] a[2] = lay[i - 1][j + 1] a[5] = lay[i][j + 1] a[8] = lay[i + 1][j + 1] j += 1 v += 1 # log('a', a) # log('j', j, 'v', v) #取数组中间值 middle = median(a) # log('middle', middle) #将中间值组成二维数组的行数组 middle_n[v - 1] = middle # i = 340 # log('middle_n', middle_n) #将中间值的行数组填充入二维数组 end_n = np.append(end_n, [middle_n], axis=0) log('end_n_hing_1', end_n) return end_n