python输入数据的维度_keras分类模型中的输入数据与标签的维度实例

本文通过mnist十分类和IMDB二分类案例,详细解析keras中输入数据和标签的维度处理,包括数据预处理、one-hot编码及数据增强。讨论了不同分类任务下,数据形状和数据类型的转换,以及与损失函数和激活函数的匹配关系。
摘要由CSDN通过智能技术生成

在《python深度学习》这本书中。

一、21页mnist十分类

导入数据集

from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

初始数据维度:

>>> train_images.shape

(60000, 28, 28)

>>> len(train_labels)

60000

>>> train_labels

array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

数据预处理:

train_images = train_images.reshape((60000, 28 * 28))

train_images = train_images.astype('float32') / 255

train_labels = to_categorical(train_labels)

之后:

print(train_images, type(train_images), train_images.shape, train_images.dtype)

print(train_labels, type(train_labels), train_labels.shape, train_labels.dtype)

结果:

[[0. 0. 0. ... 0. 0. 0.]

[0. 0. 0. ... 0. 0. 0.]

[0. 0. 0. ... 0. 0. 0.]</

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值