Dense网络与卷积神经网络那个收敛速度更快?对于MNIST的手写数字识别,很明显是Dense更快一些。大概相差1秒。为什么会有这样的结果?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import layers
import time
def load_data(path='mnist.npz'):
origin_folder = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/'
path = tf.keras.utils.get_file(
path,
origin=origin_folder + 'mnist.npz',
cache_dir='DataSet/',
cache_subdir=""
)
with np.load(path, allow_pickle=True) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
return (x_train, y_train), (x_test, y_test)
(x_train,y_train),(x_test,y_test)=load_data(path='mnist.npz')
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)
num_filters=48
kernel_size=5
hidden_dim=10
num_classes=10
model = tf.keras.Sequential([
layers.Conv1D(num_filters,kernel_size, padding='same', activation=tf.nn.relu,name="a2"),
layers.Flatten(name="a4"),
layers.Dense(hidden_dim,name="a5",activation=tf.nn.relu),#leaky_relu
layers.Dense(num_classes,activation=tf.nn.softmax)])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
start=time.time()
model.fit(x_train, y_train, epochs=3)
stop=time.time()
print("cnn计算所有时间%.2f秒"%(stop-start))
val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss)
print(val_acc)
model_dense = tf.keras.models.Sequential()
model_dense.add(tf.keras.layers.Flatten())
model_dense.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model_dense.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model_dense.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model_dense.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
start=time.time()
model_dense.fit(x_train, y_train, epochs=3)
stop=time.time()
print("Dense计算所有时间%.2f秒"%(stop-start))
val_loss, val_acc = model_dense.evaluate(x_test, y_test)
print(val_loss)
print(val_acc)
i = np.random.randint(0,10000)
plt.imshow(x_test[i],cmap=plt.cm.binary)
plt.show()
predictions = model.predict(x_test)
print("cnn 结果:%d"%np.argmax(predictions[i]))
predictions = model_dense.predict(x_test)
print("Dense 结果:%d"%np.argmax(predictions[i]))