吴恩达深度学习——编程1

1.squeeze 函数

从数组的形状中删除单维度条目,即把shape中为1的维度去掉
squeeze的用法

在这里插入图片描述

2.降维操作

X_flatten = X.reshape(X.shape [0],-1).T #X.T是X的转置

reshape讲解

3.数据集的中心化和标准化

在回归问题和一些机器学习算法中, 以及训练神经网络的过程中, 通常需要对原始数据进行中心化 (Zero-centered 或者 Mean-subtraction) 处理和标准化 (Standardization 或 Normalization) 处理.

意义:

数据中心化和标准化在回归分析中是取消由于量纲不同、自身变异或者数值相差较大所引起的误差.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面的代码哪里有问题,帮我改一下from __future__ import print_function import numpy as np import tensorflow import keras from keras.models import Sequential from keras.layers import Dense,Dropout,Flatten from keras.layers import Conv2D,MaxPooling2D from keras import backend as K import tensorflow as tf import datetime import os np.random.seed(0) from sklearn.model_selection import train_test_split from PIL import Image import matplotlib.pyplot as plt from keras.datasets import mnist images = [] labels = [] (x_train,y_train),(x_test,y_test)=mnist.load_data() X = np.array(images) print (X.shape) y = np.array(list(map(int, labels))) print (y.shape) x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0) print (x_train.shape) print (x_test.shape) print (y_train.shape) print (y_test.shape) ############################ ########## batch_size = 20 num_classes = 4 learning_rate = 0.0001 epochs = 10 img_rows,img_cols = 32 , 32 if K.image_data_format() =='channels_first': x_train =x_train.reshape(x_train.shape[0],1,img_rows,img_cols) x_test = x_test.reshape(x_test.shape[0],1,img_rows,img_cols) input_shape = (1,img_rows,img_cols) else: x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,1) x_test = x_test.reshape(x_test.shape[0],img_rows,img_cols,1) input_shape =(img_rows,img_cols,1) x_train =x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 print('x_train shape:',x_train.shape) print(x_train.shape[0],'train samples') print(x_test.shape[0],'test samples')
05-25
代码中存在以下问题: 1. 变量 images 和 labels 没有被赋值,而后面却使用了它们来构造 X 和 y,导致 X 和 y 的值都为空。 2. 导入的 mnist 数据集没有被使用,而是使用了未定义的 images 和 labels,需要将其替换为 mnist 数据集。 3. input_shape 的值应该是三元组 (img_rows, img_cols, 1),而代码中缺少了最后一个 1。 4. 在 if K.image_data_format() =='channels_first': 分支中,x_train 和 x_test 被改变了形状,但 y_train 和 y_test 却没有被改变,需要将其同步修改。 下面是修改后的代码: ``` from __future__ import print_function import numpy as np import tensorflow import keras from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K import tensorflow as tf import datetime import os np.random.seed(0) from sklearn.model_selection import train_test_split from PIL import Image import matplotlib.pyplot as plt from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) print(y_train.shape) ############################ ########## batch_size = 20 num_classes = 10 learning_rate = 0.0001 epochs = 10 img_rows, img_cols = 32, 32 if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (img_rows, img_cols, 1) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') print('input_shape:', input_shape) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值