我直接比较了具有相同参数和相同数据集(MNIST)的TensorFlow与Keras。
奇怪的是Keras在10个周期内实现96%的性能,而TensorFlow在10个周期内实现约70%的性能。我在同一实例中多次运行了此代码,并且总是会出现这种不一致的情况。
即使为TensorFlow设置了50个纪元,最终性能也达到了90%。
代码:
import keras
from keras.datasets import mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# One hot encoding
from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
# Changing the shape of input images and normalizing
x_train = x_train.reshape((60000,784))
x_test = x_test.reshape((10000,784))
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
import keras
from keras.models import Sequential
from keras.layers import Dense,Dropout,activation
# Creating the neural network
model = Sequential()
model.add(Dense(30,input_dim=784,kernel_initializer='normal',activation='relu'))
model.add(Dense(30,activation='relu'))
model.add(Dense(10,activation='softmax'))
# Optimizer
optimizer = keras.optimizers.Adam()
# Loss function
model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['acc'])
# Training
model.fit(x_train,y_train,epochs=10,batch_size=200,validation_data=(x_test,y_test),verbose=1)
# Checking the final accuracy
accuracy_final = model.evaluate(x_test,y_test,verbose=0)
print('Model accuracy: ',accuracy_final)
TensorFlow代码:(x_train,x_test,y_train,y_test与上面的Keras代码的输入相同)
import tensorflow as tf
# Epochs parameters
epochs = 10
batch_size = 200
# Neural network parameters
n_input = 784
n_hidden_1 = 30
n_hidden_2 = 30
n_classes = 10
# Placeholders x,y
x = tf.placeholder(tf.float32,[None,n_input])
y = tf.placeholder(tf.float32,n_classes])
# Creating the first layer
w1 = tf.Variable(tf.random_normal([n_input,n_hidden_1]))
b1 = tf.Variable(tf.random_normal([n_hidden_1]))
layer_1 = tf.nn.relu(tf.add(tf.matmul(x,w1),b1))
# Creating the second layer
w2 = tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2]))
b2 = tf.Variable(tf.random_normal([n_hidden_2]))
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1,w2),b2))
# Creating the output layer
w_out = tf.Variable(tf.random_normal([n_hidden_2,n_classes]))
bias_out = tf.Variable(tf.random_normal([n_classes]))
output = tf.matmul(layer_2,w_out) + bias_out
# Loss function
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = output,labels = y))
# Optimizer
optimizer = tf.train.AdamOptimizer().minimize(cost)
# Making predictions
predictions = tf.equal(tf.argmax(output,1),tf.argmax(y,1))
# accuracy
accuracy = tf.reduce_mean(tf.cast(predictions,tf.float32))
# Variables that will be used in the training cycle
train_size = x_train.shape[0]
total_batches = train_size / batch_size
# Initializing the variables
init = tf.global_variables_initializer()
# Opening the session
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(epochs):
# Loop through all batch iterations
for i in range(0,train_size,batch_size):
batch_x = x_train[i:i + batch_size]
batch_y = y_train[i:i + batch_size]
# Fit training
sess.run(optimizer,feed_dict={x: batch_x,y: batch_y})
# Running accuracy (with test data) on each epoch
acc_val = sess.run(accuracy,feed_dict={x: x_test,y: y_test})
# Showing results after each epoch
print ("Epoch: ","{}".format((epoch + 1)))
print ("accuracy_val = ","{:.3f}".format(acc_val))
print ("Training Completed!")
# Checking the final accuracy
checking = tf.equal(tf.argmax(output,1))
accuracy_final = tf.reduce_mean(tf.cast(checking,tf.float32))
print ("Model accuracy:",accuracy_final.eval({x: x_test,y: y_test}))
我正在同一实例中运行所有程序。谁能解释这种矛盾?