Sequential Sequential Model Guidelines

Get started with Keras Sequential sequence model

The sequential model is a linear stack of multiple network layers.

You can Sequential create a Sequential model by passing the list of network layer instances to  the constructor  :

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

You can also simply use  .add() methods to add layers to the model:

model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))

Specify the size of the input data

The model needs to know the size of the input it expects. For this reason, the first layer in the sequential model (and only the first layer, because the layer below can automatically infer the size) needs to receive information about its input size. There are several ways to do this:

  • Pass a  input_shape parameter to the first layer. It is a tuple representing size (a tuple consisting of integers or integers  None , where the  None representation may be any positive integer). In  input_shape the batch size of the data is not included.
  • Some 2D layers, for example  Dense, support the input_dim input size specified by parameters  , and some 3D timing layers support  input_dim and  input_length parameters.
  • If you need to specify a fixed batch size for your input (which is useful for stateful RNNs), you can pass a  batch_size parameter to a layer. If you at the same time  batch_size=32 and  input_shape=(6, 8) passed to a layer, then the size of the input on each batch is  (32,6,8).

Therefore, the following code fragments are equivalent:

model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, input_dim=784))

Model compilation

Before training the model, you need to configure the learning process, which is done by the  compile method. It receives three parameters:

  • Optimizer. It can be a string identifier of an existing optimizer, such as  rmsprop or  adagrad, or an instance of the Optimizer class. See: optimizers for details .
  • The loss function, the objective function that the model tries to minimize. It can be a string identifier of an existing loss function, such as  categorical_crossentropy or mse, or  it can be an objective function. See: losses for details .
  • Evaluation criteria metrics. For any classification problem, you want to set it to  metrics = ['accuracy']. The evaluation standard can be an existing standard string identifier or a custom evaluation standard function.
# 多分类问题
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# 二分类问题
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# 均方误差回归问题
model.compile(optimizer='rmsprop',
              loss='mse')

# 自定义评估标准函数
import keras.backend as K

def mean_pred(y_true, y_pred):
    return K.mean(y_pred)

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy', mean_pred])

Model training

The Keras model is trained on the Numpy matrix of input data and labels. To train a model, you usually use  fit functions. See the documentation here .

# 对于具有 2 个类的单输入模型(二进制分类):

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# 生成虚拟数据
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

# 训练模型,以 32 个样本为一个 batch 进行迭代
model.fit(data, labels, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 0s 255us/step - loss: 0.7057 - accuracy: 0.5310
Epoch 2/10
1000/1000 [==============================] - 0s 40us/step - loss: 0.6923 - accuracy: 0.5420
Epoch 3/10
1000/1000 [==============================] - 0s 42us/step - loss: 0.6866 - accuracy: 0.5690
Epoch 4/10
1000/1000 [==============================] - 0s 44us/step - loss: 0.6825 - accuracy: 0.5630
Epoch 5/10
1000/1000 [==============================] - 0s 48us/step - loss: 0.6804 - accuracy: 0.5600
Epoch 6/10
1000/1000 [==============================] - 0s 46us/step - loss: 0.6746 - accuracy: 0.5720
Epoch 7/10
1000/1000 [==============================] - 0s 46us/step - loss: 0.6744 - accuracy: 0.5790
Epoch 8/10
1000/1000 [==============================] - 0s 48us/step - loss: 0.6696 - accuracy: 0.5870
Epoch 9/10
1000/1000 [==============================] - 0s 50us/step - loss: 0.6682 - accuracy: 0.5880
Epoch 10/10
1000/1000 [==============================] - 0s 48us/step - loss: 0.6632 - accuracy: 0.6090

 

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# 生成虚拟数据
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))

# 将标签转换为分类的 one-hot 编码
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

# 训练模型,以 32 个样本为一个 batch 进行迭代
model.fit(data, one_hot_labels, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 0s 173us/step - loss: 2.3313 - accuracy: 0.1140
Epoch 2/10
1000/1000 [==============================] - 0s 33us/step - loss: 2.3126 - accuracy: 0.0990
Epoch 3/10
1000/1000 [==============================] - 0s 31us/step - loss: 2.3012 - accuracy: 0.1110
Epoch 4/10
1000/1000 [==============================] - 0s 30us/step - loss: 2.2929 - accuracy: 0.1210
Epoch 5/10
1000/1000 [==============================] - 0s 30us/step - loss: 2.2833 - accuracy: 0.1420
Epoch 6/10
1000/1000 [==============================] - 0s 36us/step - loss: 2.2740 - accuracy: 0.1400
Epoch 7/10
1000/1000 [==============================] - 0s 32us/step - loss: 2.2673 - accuracy: 0.1470
Epoch 8/10
1000/1000 [==============================] - 0s 35us/step - loss: 2.2580 - accuracy: 0.1540
Epoch 9/10
1000/1000 [==============================] - 0s 31us/step - loss: 2.2505 - accuracy: 0.1560
Epoch 10/10
1000/1000 [==============================] - 0s 37us/step - loss: 2.2441 - accuracy: 0.1710

Sample

Here are a few examples that can help you get started!

In the  examples directory  , you can find example models of real data sets:

  • CIFAR10 Small Picture Classification: Convolutional Neural Network (CNN) with real-time data enhancement
  • IMDB movie review sentiment classification: LSTM based on word sequence
  • Reuters News Topic Category: Multilayer Perceptron (MLP)
  • MNIST handwritten digit classification: MLP & CNN
  • Character-level text generation based on LSTM

Softmax multi-classification based on multilayer perceptron (MLP):

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

# 生成虚拟数据
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) 是一个具有 64 个隐藏神经元的全连接层。
# 在第一层必须指定所期望的输入数据尺寸:
# 在这里,是一个 20 维的向量。
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
Epoch 1/20
1000/1000 [==============================] - 0s 252us/step - loss: 2.3638 - accuracy: 0.1150
Epoch 2/20
1000/1000 [==============================] - 0s 17us/step - loss: 2.3619 - accuracy: 0.1040
Epoch 3/20
1000/1000 [==============================] - 0s 17us/step - loss: 2.3307 - accuracy: 0.1080
Epoch 4/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3192 - accuracy: 0.1050
Epoch 5/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3044 - accuracy: 0.1260
Epoch 6/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3141 - accuracy: 0.1040
Epoch 7/20
1000/1000 [==============================] - 0s 20us/step - loss: 2.3065 - accuracy: 0.1150
Epoch 8/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3047 - accuracy: 0.1140
Epoch 9/20
1000/1000 [==============================] - 0s 20us/step - loss: 2.3123 - accuracy: 0.1110
Epoch 10/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3075 - accuracy: 0.1120
Epoch 11/20
1000/1000 [==============================] - 0s 17us/step - loss: 2.3060 - accuracy: 0.1200
Epoch 12/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.3011 - accuracy: 0.1080
Epoch 13/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.2964 - accuracy: 0.1150
Epoch 14/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.2976 - accuracy: 0.1230
Epoch 15/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3007 - accuracy: 0.1230
Epoch 16/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3073 - accuracy: 0.1190
Epoch 17/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.2943 - accuracy: 0.1210
Epoch 18/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.2996 - accuracy: 0.1170
Epoch 19/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.2941 - accuracy: 0.1140
Epoch 20/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.2935 - accuracy: 0.1230
100/100 [==============================] - 0s 928us/step

Two classifications based on multilayer perceptron:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout

# 生成虚拟数据
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))

model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
Epoch 1/20
1000/1000 [==============================] - 0s 349us/step - loss: 0.7133 - accuracy: 0.5050
Epoch 2/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.7098 - accuracy: 0.5080
Epoch 3/20
1000/1000 [==============================] - 0s 21us/step - loss: 0.7020 - accuracy: 0.4910
Epoch 4/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6961 - accuracy: 0.5400
Epoch 5/20
1000/1000 [==============================] - 0s 24us/step - loss: 0.7036 - accuracy: 0.5000
Epoch 6/20
1000/1000 [==============================] - 0s 21us/step - loss: 0.7004 - accuracy: 0.4930
Epoch 7/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6954 - accuracy: 0.5160
Epoch 8/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6979 - accuracy: 0.4940
Epoch 9/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.7021 - accuracy: 0.4840
Epoch 10/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6940 - accuracy: 0.5210
Epoch 11/20
1000/1000 [==============================] - 0s 23us/step - loss: 0.6954 - accuracy: 0.5190
Epoch 12/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6892 - accuracy: 0.5310
Epoch 13/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6910 - accuracy: 0.5240
Epoch 14/20
1000/1000 [==============================] - 0s 23us/step - loss: 0.6949 - accuracy: 0.5090
Epoch 15/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6894 - accuracy: 0.5240
Epoch 16/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6879 - accuracy: 0.5460
Epoch 17/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6872 - accuracy: 0.5470
Epoch 18/20
1000/1000 [==============================] - 0s 19us/step - loss: 0.6862 - accuracy: 0.5600
Epoch 19/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6876 - accuracy: 0.5430
Epoch 20/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6914 - accuracy: 0.5400
100/100 [==============================] - 0s 997us/step

Convolutional neural network similar to VGG:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD

# 生成虚拟数据
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)

model = Sequential()
# 输入: 3 通道 100x100 像素图像 -> (100, 100, 3) 张量。
# 使用 32 个大小为 3x3 的卷积滤波器。
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)
Epoch 1/10
100/100 [==============================] - 2s 20ms/step - loss: 2.4515
Epoch 2/10
100/100 [==============================] - 1s 15ms/step - loss: 2.2862
Epoch 3/10
100/100 [==============================] - 2s 16ms/step - loss: 2.2754
Epoch 4/10
100/100 [==============================] - 2s 16ms/step - loss: 2.2698
Epoch 5/10
100/100 [==============================] - 2s 15ms/step - loss: 2.2889
Epoch 6/10
100/100 [==============================] - 2s 16ms/step - loss: 2.2597
Epoch 7/10
100/100 [==============================] - 2s 18ms/step - loss: 2.2715
Epoch 8/10
100/100 [==============================] - 2s 19ms/step - loss: 2.2686
Epoch 9/10
100/100 [==============================] - 2s 18ms/step - loss: 2.2981
Epoch 10/10
100/100 [==============================] - 2s 19ms/step - loss: 2.2965
20/20 [==============================] - 0s 11ms/step

LSTM-based sequence classification:

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTM

x_train = np.random.random((1000, 256))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100,256))
y_test = np.random.randint(2, size=(100, 1))

max_features = 1024

model = Sequential()
model.add(Embedding(max_features, output_dim=256))
model.add(LSTM(128))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)
Epoch 1/10
1000/1000 [==============================] - 12s 12ms/step - loss: 0.6958 - accuracy: 0.5120
Epoch 2/10
1000/1000 [==============================] - 11s 11ms/step - loss: 0.6935 - accuracy: 0.5200
Epoch 3/10
1000/1000 [==============================] - 13s 13ms/step - loss: 0.6928 - accuracy: 0.5190
Epoch 4/10
1000/1000 [==============================] - 12s 12ms/step - loss: 0.6935 - accuracy: 0.5320
Epoch 5/10
1000/1000 [==============================] - 11s 11ms/step - loss: 0.6908 - accuracy: 0.5270
Epoch 6/10
1000/1000 [==============================] - 10s 10ms/step - loss: 0.6942 - accuracy: 0.5170
Epoch 7/10
1000/1000 [==============================] - 10s 10ms/step - loss: 0.6927 - accuracy: 0.5270
Epoch 8/10
1000/1000 [==============================] - 10s 10ms/step - loss: 0.6935 - accuracy: 0.5310
Epoch 9/10
1000/1000 [==============================] - 11s 11ms/step - loss: 0.6929 - accuracy: 0.5240
Epoch 10/10
1000/1000 [==============================] - 11s 11ms/step - loss: 0.6934 - accuracy: 0.5170
100/100 [==============================] - 0s 4ms/step

Sequence classification based on 1D convolution:

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D

x_train = np.random.random((1000, 64,100))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100,64,100))
y_test = np.random.randint(2, size=(100, 1))

seq_length = 64

model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(seq_length, 100)))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(128, 3, activation='relu'))
model.add(Conv1D(128, 3, activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)

 

Epoch 1/10
1000/1000 [==============================] - 1s 996us/step - loss: 0.6983 - accuracy: 0.5090
Epoch 2/10
1000/1000 [==============================] - 0s 493us/step - loss: 0.6965 - accuracy: 0.4880
Epoch 3/10
1000/1000 [==============================] - 1s 513us/step - loss: 0.6976 - accuracy: 0.4870
Epoch 4/10
1000/1000 [==============================] - 1s 505us/step - loss: 0.6945 - accuracy: 0.4960
Epoch 5/10
1000/1000 [==============================] - 0s 495us/step - loss: 0.6939 - accuracy: 0.4990
Epoch 6/10
1000/1000 [==============================] - 1s 530us/step - loss: 0.6951 - accuracy: 0.5040
Epoch 7/10
1000/1000 [==============================] - 1s 592us/step - loss: 0.6949 - accuracy: 0.5090
Epoch 8/10
1000/1000 [==============================] - 1s 647us/step - loss: 0.6967 - accuracy: 0.5140
Epoch 9/10
1000/1000 [==============================] - 1s 756us/step - loss: 0.6941 - accuracy: 0.5040
Epoch 10/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.6948 - accuracy: 0.4990
100/100 [==============================] - 0s 2ms/step

Sequence classification based on stacked LSTM

In this model, we layer 3 LSTMs together, so that the model can learn higher-level temporal representations.

The first two LSTMs return the complete output sequence, but the last one only returns the last step of the output sequence, thereby reducing the time dimension (ie converting the input sequence into a single vector).

stacked LSTM

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

data_dim = 16
timesteps = 8
num_classes = 10

# 期望输入数据尺寸: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # 返回维度为 32 的向量序列
model.add(LSTM(32, return_sequences=True))  # 返回维度为 32 的向量序列
model.add(LSTM(32))  # 返回维度为 32 的单个向量
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# 生成虚拟训练数据
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))

# 生成虚拟验证数据
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))

model.fit(x_train, y_train,
          batch_size=64, epochs=5,
          validation_data=(x_val, y_val))

 

Train on 1000 samples, validate on 100 samples
Epoch 1/5
1000/1000 [==============================] - 4s 4ms/step - loss: 11.9861 - accuracy: 0.0930 - val_loss: 12.1464 - val_accuracy: 0.0900
Epoch 2/5
1000/1000 [==============================] - 1s 699us/step - loss: 12.7460 - accuracy: 0.0900 - val_loss: 12.4198 - val_accuracy: 0.0900
Epoch 3/5
1000/1000 [==============================] - 1s 669us/step - loss: 12.9024 - accuracy: 0.0900 - val_loss: 12.4619 - val_accuracy: 0.0900
Epoch 4/5
1000/1000 [==============================] - 1s 641us/step - loss: 12.9497 - accuracy: 0.0900 - val_loss: 12.4803 - val_accuracy: 0.0900
Epoch 5/5
1000/1000 [==============================] - 1s 627us/step - loss: 12.9648 - accuracy: 0.0900 - val_loss: 12.4982 - val_accuracy: 0.0900

Stacked LSTM model rendered by "stateful"

In a stateful recurrent neural network model, after processing a batch of samples, its internal state (memory) will be recorded and used as the initial state of the next batch of samples. This allows processing of longer sequences while maintaining controllability of computational complexity.

You can find more information about stateful RNNs in the FAQ.

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

data_dim = 16
timesteps = 8
num_classes = 10
batch_size = 32

# 期望输入数据尺寸: (batch_size, timesteps, data_dim)
# 请注意,我们必须提供完整的 batch_input_shape,因为网络是有状态的。
# 第 k 批数据的第 i 个样本是第 k-1 批数据的第 i 个样本的后续。
model = Sequential()
model.add(LSTM(32, return_sequences=True, stateful=True,
               batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# 生成虚拟训练数据
x_train = np.random.random((batch_size * 10, timesteps, data_dim))
y_train = np.random.random((batch_size * 10, num_classes))

# 生成虚拟验证数据
x_val = np.random.random((batch_size * 3, timesteps, data_dim))
y_val = np.random.random((batch_size * 3, num_classes))

model.fit(x_train, y_train,
          batch_size=batch_size, epochs=5, shuffle=False,
          validation_data=(x_val, y_val))

 

Train on 320 samples, validate on 96 samples
Epoch 1/5
320/320 [==============================] - 2s 7ms/step - loss: 11.7630 - accuracy: 0.0906 - val_loss: 12.4489 - val_accuracy: 0.0625
Epoch 2/5
320/320 [==============================] - 0s 880us/step - loss: 12.7446 - accuracy: 0.1063 - val_loss: 13.0136 - val_accuracy: 0.0625
Epoch 3/5
320/320 [==============================] - 0s 972us/step - loss: 13.1789 - accuracy: 0.1063 - val_loss: 13.2969 - val_accuracy: 0.0625
Epoch 4/5
320/320 [==============================] - 0s 1ms/step - loss: 13.3936 - accuracy: 0.1063 - val_loss: 13.4358 - val_accuracy: 0.0625
Epoch 5/5
320/320 [==============================] - 0s 1ms/step - loss: 13.4951 - accuracy: 0.1063 - val_loss: 13.4968 - val_accuracy: 0.0625

 

130 original articles published · Like 30 · Visits 40,000+

Guess you like

Origin blog.csdn.net/W_H_M_2018/article/details/105546537