text: #-*- coding: UTF-8 -*-
  """
  # WANGZHE12
  """
  import numpy as np
  import h5py
  
  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 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 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 load_dataset():
      """
      # 加载数据集
      """
      train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")  # 读取H5文件
      train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
      train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels
  
      test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
      test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
      test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels
  
      classes = np.array(test_dataset["list_classes"][:])  # the list of classes
  
      train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))  # 对训练集和测试集标签进行reshape
      test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
  
      return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes