def model(X, Y, learning_rate=0.05, num_iterations=100000, print_cost=True, lambd=0): grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 50, 20, 1] #print(layers_dims) parameters = initialize_parameters(layers_dims) #print(parameters) for i in range(0, num_iterations): a3, cache = forward_propagation(X, parameters) cost = compute_cost_with_regularization(a3, Y, parameters, lambd) grads = backward_propagation_with_regularization(X, Y, cache, lambd) parameters = update_parameters(parameters, grads, learning_rate) if print_cost and i % 1000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 10 == 0: costs.append(cost) # plot the cost plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, is_plot=True, lambd=0, keep_prob=1): grads = {} costs = [] #params = {} m = X.shape[0] layer_dims = [X.shape[0], 20, 3, 1] # initialize parameters params = reg_utils.initialize_parameters(layer_dims) # Training for i in range(num_iterations): # Forward Prop if keep_prob == 1: a3, cache = reg_utils.forward_propagation(X, params) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout(X, params, keep_prob) else: print("Wrong keep_prob value. Try again.") exit # Compute cost if lambd == 0: cost = reg_utils.compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, params, lambd) # Back prop if (lambd == 0 and keep_prob == 1): grads = reg_utils.backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # update parameters params = reg_utils.update_parameters(params, grads, learning_rate) # print cost if (i % 1000 == 0): costs.append(cost) if (print_cost and i % 10000) == 0: print("Iteration " + str(i) + ": " + str(cost)) # Plot cost curve if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('#iterations') plt.title('learning rate=' + str(learning_rate)) plt.show() return params
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, lambd=0, keep_prob=1): grads = {} costs = [] # to keep track of the cost m = X.shape[1] # number of examples layers_dims = [X.shape[0], 20, 3, 1] # Initialize parameters dictionary. parameters = initialize_parameters(layers_dims) # Loop (gradient descent) for i in range(0, num_iterations): # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID. if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) # Cost function if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # Backward propagation. assert (lambd == 0 or keep_prob == 1 ) # it is possible to use both L2 regularization and dropout, # but this assignment will only explore one at a time if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # Update parameters. parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # plot the cost plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X,Y,learning_rate=0.3,num_iterations=30000,print_cost=True,is_plot=True,lambd=0,keep_prob=1): """ 实现一个三层神经网络:LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID 参数: X -输入的数据,维度(2,训练/测试的数量) Y -标签[0(蓝色)|1(红色)],维度为(1,对应输入数据的标签) lambd -正则化的超参数,实数 keep_prob 随机删除节点的概率 返回: parameters -学习后的参数 """ grads={} costs=[] m=X.shape[0] layers_dims=[X.shape[0],20,3,1] parameters=reg_utils.initialize_parameters(layers_dims) for i in range(0,num_iterations): #forward propagation if keep_prob==1: a3,cache=reg_utils.forward_propagation(X,parameters) elif keep_prob<1: a3,cache=forward_propagation_with_dropout(X,parameters,keep_prob) else: print("keep_prob error,quit!") exit if lambd==0: cost=reg_utils.compute_cost(a3,Y) else: cost=compute_cost_with_regularization(a3,Y,parameters,lambd) #backward propagation assert(lambd==0 or keep_prob==1) if(lambd==0 and keep_prob==1): grads=reg_utils.backward_propagation(X,Y,cache) elif lambd!=0: grads=backward_propagation_with_regularization(X,Y,cache,lambd) elif keep_prob<1: grads=backward_propagation_with_dropout(X,Y,cache,keep_prob) parameters=reg_utils.update_parameters(parameters,grads,learning_rate) if i%1000==0:# print cost costs.append(cost) if(print_cost and i%10000==0): print("number time"+str(i)+" cost is "+str(cost)) if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations(*1000)') plt.title('Learing rate='+str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, keep_prob=1.0, lambd=0.0, print_cost=True): grads = {} costs = [] # to keep track of the cost layers_dims = [X.shape[0], 20, 3, 1] # Initialize parameters dictionary. parameters = initialize_parameters(layers_dims) for i in range(0, num_iterations): # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID. cost = None if lambd == 0 and keep_prob == 1: a3, cache = forward_propagation(X, parameters) cost = compute_cost(a3, Y, parameters) grads = backward_propagation(X, Y, cache) elif lambd != 0 and keep_prob == 1: a3, cache = forward_propagation(X, parameters) cost = compute_cost(a3, Y, parameters, lambd) grads = backward_propagation(X, Y, cache, lambd=lambd) elif lambd == 0 and keep_prob < 1: a3, cache = forward_propagation(X, parameters, keep_prob=keep_prob) cost = compute_cost(a3, Y, parameters) grads = backward_propagation(X, Y, cache, keep_prob=keep_prob) elif lambd != 0 and keep_prob < 1: a3, cache = forward_propagation(X, parameters) cost = compute_cost(a3, Y, parameters, lambd) grads = backward_propagation(X, Y, cache, lambd=lambd, keep_prob=keep_prob) # Update parameters. parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # plot the cost # plt.plot(costs) # plt.ylabel('cost') # plt.xlabel('iterations (x1,000)') # plt.title("Learning rate =" + str(learning_rate)) # plt.show() return parameters
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, lambd = 0, keep_prob = 1): """ Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID. Arguments: X -- input data, of shape (input size, number of examples) Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples) learning_rate -- learning rate of the optimization num_iterations -- number of iterations of the optimization loop print_cost -- If True, print the cost every 10000 iterations lambd -- regularization hyperparameter, scalar keep_prob - probability of keeping a neuron active during drop-out, scalar. Returns: parameters -- parameters learned by the model. They can then be used to predict. """ grads = {} layers_dims = [X.shape[0], 20, 3, 1] parameters = initialize_parameters(layers_dims) for i in range(0, num_iterations): if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob) if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) parameters = update_parameters(parameters, grads, learning_rate) if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, is_plot=True, lambd=0, keep_prob=1): losses = [] layers_dims = [X.shape[0], 20, 3, 1] # 选择初始化参数的类型 parameters = reg_utils.initialize_parameters(layers_dims) for i in range(num_iterations): # 前向传播 if keep_prob == 1: A3, cache = reg_utils.forward_propagation(X, parameters) else: A3, cache = regulation.forward_propagation_drop( X, parameters, keep_prob) # 计算Loss if lambd == 0: loss = reg_utils.compute_cost(A3, Y) else: loss = regulation.compute_cost_l2(A3, Y, parameters, lambd) # 反向传播 if keep_prob == 1 and lambd == 0: gradients = reg_utils.backward_propagation(X, Y, cache) elif keep_prob != 1: gradients = regulation.backward_propagation_drop( X, Y, cache, keep_prob) elif lambd != 0: gradients = regulation.backward_propagation_l2(X, Y, cache, lambd) # 更新权重 parameters = reg_utils.update_parameters(parameters, gradients, learning_rate) if i % 1000 == 0: losses.append(loss) # 打印成本 if (print_cost and i % 10000 == 0): print("第" + str(i) + "次迭代,成本值为:" + str(loss)) # 学习完毕,绘制成本曲线 if is_plot: plt.plot(losses) plt.ylabel('cost') plt.xlabel('iterations (per hundreds)') plt.title("Learning rate =" + str(learning_rate)) plt.show() # 返回学习完毕后的参数 return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, lambd=0, keep_prob=1): grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] parameters = initialize_parameters(layers_dims) for i in range(0, num_iterations): if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) assert (lambd == 0 or keep_prob == 1) if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) parameters = update_parameters(parameters, grads, learning_rate) if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, layer_dims, keep_prob, lamda, learning_rate, num_iteration, norm=False): parameters = initialize_parameters(layer_dims) if norm == False and keep_prob == 1: for i in range(num_iteration): AL, cache = forward_propagation(X, parameters) grads = backward_propagation(X, Y, cache) parameters = update_parameters(parameters, grads, learning_rate) if i % 1000 == 0: cost = compute_cost(AL, Y) print(f'cost after {i} iteration:{cost}') return parameters if norm == False and keep_prob != 1: for i in range(num_iteration): AL, cache = drop_forward(X, parameters, keep_prob) grads = back_dropout(Y, AL, cache, keep_prob) parameters = update_parameters(parameters, grads, learning_rate) if i % 1000 == 0: cost = compute_cost(AL, Y) print(f'cost after{i} iterations:{cost}') return parameters if norm == True and keep_prob == 1: for i in range(num_iteration): AL, cache = forward_propagation(X, parameters) grads = l2_back_propagation(X, Y, AL, cache, lamda) parameters = update_parameters(parameters, grads, learning_rate) if i % 1000 == 0: cost = compute_l2_cost(AL, Y, parameters, lamda) print(f'cost after {i} iteration:{cost}') return parameters
def model(X,Y,learning_rate=0.3,num_iterations=30000,print_cost=True,is_plot=True,lambd=0,keep_prob=1): grads={} costs=[] m=X.shape[1] layers_dims=[X.shape[0],20,3,1] # 每一层的维度 parameters=reg_utils.initialize_parameters(layers_dims) for i in range(0,num_iterations): # 前向传播,计算A3 if keep_prob==1: # 执行正常的前向传播 a3,cache=reg_utils.forward_propagation(X,parameters) elif keep_prob<1: # 执行dropout,随机失活结点 a3,cache=forward_propagation_with_dropout(X,parameters,keep_prob) else : print("keep_prob参数错误,程序退出") exit # 计算Cost if lambd==0: # 不使用正则化 cost=reg_utils.compute_cost(a3,Y) else: # 使用L2正则化 cost=compute_cost_with_regularization(a3,Y,parameters,lambd) assert(lambd==0 or keep_prob==1) # 反向传播,计算梯度 if (lambd==0 and keep_prob==1): # 不使用L2正则化和Dropout grads=reg_utils.backward_propagation(X,Y,cache) elif lambd!=0:# 使用L2正则化,不使用随机删除结点 grads=backward_propagation_with_regularization(X,Y,cache,lambd) elif keep_prob<1: # 使用dropout随机删除结点,不使用L2正则化 grads=backward_propagation_with_dropout(X,Y,cache,keep_prob) # 更新参数 parameters=reg_utils.update_parameters(parameters,grads,learning_rate) if i % 1000==0: costs.append(cost) if print_cost: print("第"+str(i)+"次迭代,成本值为"+str(cost)) if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iteration (per 100)') plt.title('learning_rate='+str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, lambd = 0, keep_prob = 1): """ Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR>SIGMOID. Arguments: X -- input data, of shape (input size, number of examples) Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples) learning_rate -- learning rate of the optimization num_iterations -- number of iterations of the optimization loop print_cost -- If True, print the cost every 10000 iterations lambd -- regularization hyperparameter, scalar lambd -- regularization hyperparameter, scalar keep_prob - probability of keeping a neuron active during drop-out, scalar. Returns: parameters -- parameters learned by the model. They can then be used to pred ict. """ grads = {} costs = [] # to keep track of the cost m = X.shape[1] # number of examples layers_dims = [X.shape[0], 20, 3, 1] # Initialize parameters dictionary. parameters = initialize_parameters(layers_dims) # Loop (gradient descent) for i in range(0, num_iterations): # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIG MOID. if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob) # Cost function if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # Backward propagation. assert(lambd==0 or keep_prob==1) # it is possible to use both L2 regu larization and dropout, # but this assignment will only expl ore one at a time if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # Update parameters. parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # plot the cost plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model_reg(X, Y, alpha=0.05, loops=10000, lambd=0, keep_prob=1, init_method='he'): grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] # inintial params if init_method == 'zeros': params = init_zeros(layers_dims) elif init_method == 'random': params = init_random(layers_dims) elif init_method == 'he': params = init_he(layers_dims) else: print('Error: unexcepted init_method!') # start train for i in range(loops): a3, cache = forward_propagate_with_reg(X, params, keep_prob=keep_prob) cost = compute_loss_with_reg(a3, Y, params, lambd=lambd) costs.append(cost) grads = backward_propagate_with_reg(X, Y, cache, lambd=lambd, keep_prob=keep_prob) params = reg_utils.update_parameters(params, grads, alpha) if (i + 1) % 100 == 0: print(f'No.{i+1} iteration\'s loss: {cost}') plt.plot(costs) plt.xlabel('step') plt.ylabel('loss') plt.title('loss circle') plt.show() return params
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, lambd=0, keep_prob=1): grads = {} costs = [] m = X.shape[1] layer_dims = [X.shape[0], 20, 3, 1] parameters = initialize_parameters(layer_dims) for i in range(num_iterations): if keep_prob == 1: A3, cache = forward_propagation(X, parameters) elif keep_prob < 1: A3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) if lambd == 0: cost = compute_cost(A3, Y) else: cost = compute_cost_with_regulations(A3, Y, parameters, lambd) assert (lambd == 0 or keep_prob == 1) if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) parameters = update_parameters(parameters, grads, learning_rate) if print_cost and i % 1000 == 0: print("Cost After iterations {} : {}".format(i, cost)) costs.append(cost) plt.plot(costs) plt.title("Learning rate = " + str(learning_rate)) plt.ylabel("Cost") plt.xlabel("iterations(x1,000)") #plt.show() plt.savefig('/home/guanlingh/local2server/homework6/image/h6-2.jpg') return parameters
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, lambd = 0, keep_prob = 1): """ Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID. Arguments: X -- input data, of shape (input size, number of examples) Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples) learning_rate -- learning rate of the optimization num_iterations -- number of iterations of the optimization loop print_cost -- If True, print the cost every 10000 iterations lambd -- regularization hyperparameter, scalar keep_prob - probability of keeping a neuron active during drop-out, scalar. Returns: parameters -- parameters learned by the model. They can then be used to predict. """ grads = {} costs = [] # to keep track of the cost m = X.shape[1] # number of examples layers_dims = [X.shape[0], 20, 3, 1] # Initialize parameters dictionary. parameters = initialize_parameters(layers_dims) # Loop (gradient descent) for i in range(0, num_iterations): # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID. if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob) # Cost function if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # Backward propagation. assert(lambd==0 or keep_prob==1) # it is possible to use both L2 regularization and dropout, # but this assignment will only explore one at a time if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # Update parameters. parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # plot the cost plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, is_plot=True, lambd=0, keep_prob=1): ''' 实现一个三层的神经网络:linear->relu->linear->relu->linear->sigmoid :param X: 输入的数据,维度为(2,要训练/测试的数量) :param Y: 标签,[0(蓝色)|1(红色)],维度为(1,对应的是输入的数据的标签) :param learing_rate: 学习速率 :param num_iterations: 迭代次数 :param print_cost: 是否打印成本值 :param lambd: 正则化的超参数,实数 :param keep_prob: 随机删除节点的概率 :return: parameters-学习后的参数 ''' grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] #初始化参数 parameters = reg_utils.initialize_parameters(layers_dims) #开始学习 for i in range(0, num_iterations): #前向传播 ##是否删除随机节点 if keep_prob == 1: ###不删除随机节点 a3, cache = reg_utils.forward_propagation(X, parameters) elif keep_prob < 1: ###随即删除节点 a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) else: print('keep_prob参数错误,程序退出.') exit #计算成本 ##是否使用二范数 if lambd == 0: ###不使用L2正则化 cost = reg_utils.compute_cost(a3, Y) else: ###使用L2正则化 cost = compute_cost_with_regularization(a3, Y, parameters, lambd) ##反向传播 ##可以同时使用L2正则化和随机删除节点,但本次实验不同时使用 assert (lambd == 0 or keep_prob == 1) ##两个参数的使用情况 if (lambd == 0 and keep_prob == 1): ###不适用L2正则化和不使用随即删除节点 grads = reg_utils.backward_propagation(X, Y, cache) elif lambd != 0: ###使用L2正则化,不使用随即删除节点 grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: ###使用随即删除节点,不使用L2正则化 grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) #更新参数 parameters = reg_utils.update_parameters(parameters, grads, learning_rate) #记录并打印成本 if i % 1000 == 0: ##记录成本 costs.append(cost) if (print_cost and i % 10000 == 0): #打印成本 print('第' + str(i) + '次迭代,成本值为:' + str(cost)) #是否绘制成本曲线图 if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations(x1,000)') plt.title('Learning rate = ' + str(learning_rate)) plt.show() #返回学习后的参数 return parameters
def model(X,Y,learning_rate = 0.3,num_iterations = 30000,print_cost = True,is_plot = True,lambd = 0,keep_prob = 1): ''' 实现一个三层的神经网络:linear->relu->linear->relu->linear->sigmoid :param X: 输入的数据,维度为(2,要训练/测试的数量) :param Y: 标签,[0(蓝色)|1(红色)],维度为(1,对应的是输入的数据的标签) :param learing_rate: 学习速率 :param num_iterations: 迭代次数 :param print_cost: 是否打印成本值 :param lambd: 正则化的超参数,实数 :param keep_prob: 随机删除节点的概率 :return: parameters-学习后的参数 ''' grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0],20,3,1] #初始化参数 parameters = reg_utils.initialize_parameters(layers_dims) #开始学习 for i in range(0,num_iterations): #前向传播 ##是否删除随机节点 if keep_prob == 1: ###不删除随机节点 a3,cache = reg_utils.forward_propagation(X,parameters) elif keep_prob<1: ###随即删除节点 a3 , cache = forward_propagation_with_dropout(X,parameters,keep_prob) else: print('keep_prob参数错误,程序退出.') exit #计算成本 ##是否使用二范数 if lambd == 0: ###不使用L2正则化 cost = reg_utils.compute_cost(a3,Y) else: ###使用L2正则化 cost = compute_cost_with_regularization(a3,Y,parameters,lambd) ##反向传播 ##可以同时使用L2正则化和随机删除节点,但本次实验不同时使用 assert(lambd == 0 or keep_prob == 1) ##两个参数的使用情况 if(lambd == 0 and keep_prob == 1): ###不适用L2正则化和不使用随即删除节点 grads = reg_utils.backward_propagation(X,Y,cache) elif lambd!=0: ###使用L2正则化,不使用随即删除节点 grads = backward_propagation_with_regularization(X,Y,cache,lambd) elif keep_prob<1: ###使用随即删除节点,不使用L2正则化 grads = backward_propagation_with_dropout(X,Y,cache,keep_prob) #更新参数 parameters = reg_utils.update_parameters(parameters,grads,learning_rate) #记录并打印成本 if i%1000 == 0: ##记录成本 costs.append(cost) if(print_cost and i%10000==0): #打印成本 print('第'+str(i)+'次迭代,成本值为:'+str(cost)) #是否绘制成本曲线图 if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations(x1,000)') plt.title('Learning rate = '+str(learning_rate)) plt.show() #返回学习后的参数 return parameters
def L_layer_model(X, Y, layers_dims, learning_rate=0.0075, num_iterations=3000, print_cost=False): #lr was 0.009 """ Implements a L-layer neural network: [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID. Arguments: X -- data, numpy array of shape (number of examples, num_px * num_px * 3) Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) layers_dims -- list containing the input size and each layer size, of length (number of layers + 1). learning_rate -- learning rate of the gradient descent update rule num_iterations -- number of iterations of the optimization loop print_cost -- if True, it prints the cost every 100 steps Returns parameters -- parameters learnt by the model. They can then be used to predict. """ np.random.seed(1) costs = [] # keep track of cost # Parameters initialization. ### START CODE HERE ### parameters = initialize_parameters_deep(layers_dims) ### END CODE HERE ### # Loop (gradient descent) for i in range(0, num_iterations): # Forward propagation: [LINEAR -> RELU]*(L-1) -> LINEAR -> SIGMOID. ### START CODE HERE ### (~ 1 line of code) AL, caches = L_model_forward(X, parameters) ### END CODE HERE ### # Compute cost. ### START CODE HERE ### (~ 1 line of code) cost = compute_cost(AL, Y) ### END CODE HERE ### # Backward propagation. ### START CODE HERE ### (~ 1 line of code) grads = L_model_backward(AL, Y, caches) ### END CODE HERE ### # Update parameters. ### START CODE HERE ### (~ 1 line of code) parameters = update_parameters(parameters, grads, learning_rate) ### END CODE HERE ### # Print the cost every 100 training example if print_cost and i % 1 == 0: print("Cost after iteration %i: %f" % (i, cost)) if print_cost and i % 100 == 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 parameters
cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # Backward propagation. assert(lambd==0 or keep_prob==1) # it is possible to use both L2 regularization and dropout, # but this assignment will only explore one at a time if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # Update parameters. parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # plot the cost plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, lambd=0, keep_prob=1): """ 实现一个三层神经网络模型: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID. 可以:-- 不使用正则化 -- 使用 L2 正则化 -- 使用 dropout 正则化 :param X: 输入数据, of shape (input size, number of examples) :param Y: 正确的标签向量 (1 for blue dot / 0 for red dot), of shape (output size, number of examples) :param learning_rate: 优化的学习率 :param num_iterations: 优化循环的迭代次数 :param print_cost: If True, print the cost every 10000 iterations :param lambd: 正则化的超参数 λ :param keep_prob: drop-out 正则化中随机保留节点的概率 Returns: parameters -- 神经网络模型学习得到的参数,可以用来预测 """ grads = {} costs = [] # to keep track of the cost m = X.shape[1] # number of examples layers_dims = [X.shape[0], 20, 3, 1] # 初始化参数字典 parameters = initialize_parameters(layers_dims) # 学习循环(梯度下降) for i in range(0, num_iterations): # 前向传播: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID. # 不使用 dropout 正则化 if keep_prob == 1: a3, cache = forward_propagation(X, parameters) # 使用 dropout 正则化 elif keep_prob < 1: a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) # 计算 cost # 不使用 L2 正则化 if lambd == 0: cost = compute_cost(a3, Y) # 使用 L2 正则化 else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # 后向传播 assert (lambd == 0 or keep_prob == 1 ) # 可以同时使用 L2 正则化和 dropout, 但是本程序只使用其中一个或不使用正则化 # 不使用正则化 if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) # 使用 L2 正则化 elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) # 使用 dropout 正则化 elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # 更新参数 parameters = update_parameters(parameters, grads, learning_rate) # Print the loss every 10000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 1000 == 0: costs.append(cost) # 绘制 cost 变化曲线图 plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, lambd=0, keep_prob=1): """ implements a three layer neural network: linear -> relu -> linear -> relu -> linear -> sigmoid arguments: X -- input data, shape(input size,number of examples) Y -- true label vector, shape(output size,number of examples) learning_rate -- learn rate of the optimization num_iterations -- number of iterations of the optimization loop print_cost -- if true, print the cost every 10000 iterations lambd -- regularization hhyperparameter keep_prob -- probability of keeping a neuron activa during drop-out returns: parameters -- parameters learned by the model, they can then be used to predict """ grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] #initialize parameters dictionary parameters = initialize_parameters(layers_dims) #loop for i in range(0, num_iterations): #forward if keep_prob == 1: a3, cache = forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) #cost function if lambd == 0: cost = compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # assert (lambd == 0 or keep_prob == 1) if lambd == 0 and keep_prob == 1: grads = backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) #update parameters parameters = update_parameters(parameters, grads, learning_rate) #print the loss every 1000 iterations if print_cost and i % 10000 == 0: print("Cost after iteration {}: {}".format(i, cost)) if print_cost and i % 100 == 0: costs.append(cost) #plot the cost plt.figure(2) plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title('learning rate = ' + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, is_plot=True, lambd=0, keep_prob=1): """ 实现一个三层的神经网络:LINEAR ->RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID 参数: X - 输入的数据,维度为(2, 要训练/测试的数量) Y - 标签,【0(蓝色) | 1(红色)】,维度为(1,对应的是输入的数据的标签) learning_rate - 学习速率 num_iterations - 迭代的次数 print_cost - 是否打印成本值,每迭代10000次打印一次,但是每1000次记录一个成本值 is_polt - 是否绘制梯度下降的曲线图 lambd - 正则化的超参数,实数 keep_prob - 随机删除节点的概率 返回 parameters - 学习后的参数 """ grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] # 初始化参数 parameters = reg_utils.initialize_parameters(layers_dims) # 开始学习 for i in range(0, num_iterations): # 前向传播 ##是否随机删除节点 if keep_prob == 1: ###不随机删除节点 a3, cache = reg_utils.forward_propagation(X, parameters) elif keep_prob < 1: ###随机删除节点 a3, cache = reg_utils.forward_propagation_with_dropout(X, parameters, keep_prob) else: print("keep_prob参数错误!程序退出。") exit # 计算成本 ## 是否使用二范数 if lambd == 0: ###不使用L2正则化 cost = reg_utils.compute_cost(a3, Y) else: ###使用L2正则化 cost = reg_utils.compute_cost_with_regularization(a3, Y, parameters, lambd) # 反向传播 ##可以同时使用L2正则化和随机删除节点,但是本次实验不同时使用。 assert (lambd == 0 or keep_prob == 1) ##两个参数的使用情况 if (lambd == 0 and keep_prob == 1): ### 不使用L2正则化和不使用随机删除节点 grads = reg_utils.backward_propagation(X, Y, cache) elif lambd != 0: ### 使用L2正则化,不使用随机删除节点 grads = reg_utils.backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: ### 使用随机删除节点,不使用L2正则化 grads =reg_utils. backward_propagation_with_dropout(X, Y, cache, keep_prob) # 更新参数 parameters = reg_utils.update_parameters(parameters, grads, learning_rate) # 记录并打印成本 if i % 1000 == 0: ## 记录成本 costs.append(cost) if (print_cost and i % 10000 == 0): # 打印成本 print("第" + str(i) + "次迭代,成本值为:" + str(cost)) # 是否绘制成本曲线图 if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() # 返回学习后的参数 return parameters
def model2(X, Y, learning_rate=0.3, num_iterations=30000, print_cost=True, is_plot=True, lambd=0, keep_prob=1): grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] parameters = reg_utils.initialize_parameters(layers_dims) for i in range(0, num_iterations): # 前向传播 # 询问是否删除节点 if keep_prob == 1: # 不随机删除节点 a3, cache = reg_utils.forward_propagation(X, parameters) elif keep_prob < 1: # 随即删除节点 a3, cache = forward_propagation_with_dropout( X, parameters, keep_prob) else: print("keep_prob参数错误!程序退出") exit # 计算成本 # 是否正则化 if lambd == 0: cost = reg_utils.compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) # 本次实验不同时使用正则化和随即删除节点 assert (lambd == 0 or keep_prob == 1) if (lambd == 0 and keep_prob == 1): grads = reg_utils.backward_propagation(X, Y, cache) elif lambd != 0: grads = backward_propagation_with_regularization( X, Y, cache, lambd) elif keep_prob < 1: grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # 更新参数 parameters = reg_utils.update_parameters(parameters, grads, learning_rate) if i % 1000 == 0: # 要记录的 costs.append(cost) if (print_cost and i % 10000 == 0): # 要输出的 print("第" + str(i) + "次迭代, 成本为:" + str(cost)) if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations(x1,000)') plt.title("learning rate =" + str(learning_rate)) plt.show() return parameters
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, is_plot = True, lambd = 0, keep_prob = 1): """ 实现一个三层的神经网络:ReLU->ReLU->Sigmoid :param lambd:正则化的超参数 :param keep_prob:随机删除结点的概率 :return: parameters - 学习后的参数 """ parameter = {} grads = {} costs = [] m = X.shape[1] layers_dims = [X.shape[0], 20, 3, 1] # 初始化参数 parameters = reg_utils.initialize_parameters(layers_dims) for l in range(num_iterations): # 前向传播 # 是否随机删除结点 if keep_prob == 1: # 前向传播 a3, cache = reg_utils.forward_propagation(X, parameters) elif keep_prob < 1: a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob) else: print("keep_prob参数错误!程序退出") exit() # 计算成本 # 是否使用正则化 if lambd == 0: cost = reg_utils.compute_cost(a3, Y) else: cost = compute_cost_with_regularization(a3, Y, parameters, lambd) if l % 1000 == 0: costs.append(cost) if print_cost: print(f'第{l}次迭代,成本值为:{cost}') # 反向传播 # 可以同时使用L2正则化和随机删除节点,但本次实验不同时使用 assert(lambd == 0 or keep_prob == 1) if lambd == 0 and keep_prob == 1: # 不使用L2正则化和随机删除节点 grads = reg_utils.backward_propagation(X, Y, cache) elif lambd != 0: # 使用L2正则化,不使用dropout grads = backward_propagation_with_regularization(X, Y, cache, lambd) elif keep_prob < 1: # 使用dropout,不使用L2正则化 grads = backward_propagation_with_dropout(X, Y, cache, keep_prob) # 更新参数 parameters = reg_utils.update_parameters(parameters, grads, learning_rate) if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (x1,000)') plt.title("Learning rate =" + str(learning_rate)) plt.show() return parameters