keras篇(1)--model.fit()的输入数据

人生苦短,我用keras!!!

大家都知道keras极大的简化了神经网络的搭建,但是大家知道如何输入数据吗,数据大时,直接numpy矩阵输入使内存爆满?有试过生成器吗?有试过tf.data吗?想知道这几着的差距和优劣势吗?往下看吧!!!

一、简介

我们先来看看keras官方的代码中对model.fit()中的数据的输入的描述:

x: Input data. It could be:
 - A Numpy array (or array-like), or a list of arrays
  (in case the model has multiple inputs).
 - A TensorFlow tensor, or a list of tensors
  (in case the model has multiple inputs).
 - A dict mapping input names to the corresponding array/tensors,
  if the model has named inputs.
 - A `tf.data` dataset. Should return a tuple
  of either `(inputs, targets)` or
  `(inputs, targets, sample_weights)`.
 - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
  or `(inputs, targets, sample_weights)`.
A more detailed description of unpacking behavior for iterator types
(Dataset, generator, Sequence) is given below.

从描述中我们可以看到输入的方式有:

  • Numpy array
  • List Numpy Array
  • Tensors
  • Dict
  • tf.data
  • generator
  • keras.utils.Sequeue

二、Numpy Array

从标题就可以看到,model.fit()的输入为numpy矩阵

适合数据量比较小的时候,直接把全部数据读入内存

import keras
from keras.layers import *
import numpy as np


# 第一种喂入数据的方式,直接全部喂入
(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()
print("x_train.shape:", x_train.shape)
print("y_test.shape:", x_test.shape)
print(y_train.shape)

# 归一化,不然不容易收敛
x_train = x_train / 255.0
x_test = x_test / 255.0

model = keras.Sequential(
    [
        Flatten(input_shape=(28, 28)),
        Dense(units=256, activation='relu'),
        Dense(units=128, activation='relu'),
        Dense(units=10, activation='softmax')
    ]
)

model.summary()

optimizer = keras.optimizers.Adam(lr=0.001)
model.compile(
    optimizer=optimizer,
    metrics=['accuracy'],
    loss=keras.losses.sparse_categorical_crossentropy)


model.fit(x_train, y_train, batch_size=32, epochs=1, verbose=1)

三、Tensors

使用tf.convert_to_tensor()函数将Numpy矩阵变成Tensor张量,在Tensorflow框架中Numpy和Tensor的转换很容易

import keras
from keras.layers import *
import numpy as np
import tensorflow as tf

(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()

# 归一化,不然不容易收敛
x_train = x_train / 255.0
x_test = x_test / 255.0

x_train = tf.convert_to_tensor(x_train)
x_test = tf.convert_to_tensor(x_test)

model = keras.Sequential(
    [
        Flatten(input_shape=(28, 28)),
        Dense(units=256, activation='relu'),
        Dense(units=128, activation='relu'),
        Dense(units
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值