人生苦短,我用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