首页 > 解决方案 > 与低级TF相比,keras非常慢?

问题描述

我对 Keras 有过一次奇怪的经历。信息:输入数据集形状

16 个特征,5000 个观测值 目标变量:1 维

问题:回归

在为学生编写代码时,我使用以下代码使用 tf 开发了一个玩具网络(我知道这不是一个完整的示例,但我希望它能为您提供足够的信息)

n1 = 15 # Number of neurons in layer 1
n2 = 15 # Number of neurons in layer 2 
n3 = 15
nx = number_of_x_points
n_dim = nx
n4 = 1

stddev_f = 0.1

tf.set_random_seed(5)

X = tf.placeholder(tf.float32, [n_dim, None])
Y = tf.placeholder(tf.float32, [10, None])
W1 = tf.Variable(tf.random_normal([n1, n_dim], stddev=stddev_f)) 
b1 = tf.Variable(tf.constant(0.0, shape = [n1,1]) )
W2 = tf.Variable(tf.random_normal([n2, n1], stddev=stddev_f)) 
b2 = tf.Variable(tf.constant(0.0, shape = [n2,1])) 
W3 = tf.Variable(tf.random_normal([n3,n2], stddev = stddev_f))
b3 = tf.Variable(tf.constant(0.0, shape = [n3,1]))
W4 = tf.Variable(tf.random_normal([n4,n3], stddev = stddev_f))
b4 = tf.Variable(tf.constant(0.0, shape = [n4,1]))


X = tf.placeholder(tf.float32, [nx, None]) # Inputs
Y = tf.placeholder(tf.float32, [1, None]) # Labels


Z1 = tf.nn.sigmoid(tf.matmul(W1, X) + b1) # n1 x n_dim * n_dim x n_obs = n1 x n_obs
Z2 = tf.nn.sigmoid(tf.matmul(W2, Z1) + b2) # n2 x n1 * n1 * n_obs = n2 x n_obs
Z3 = tf.nn.sigmoid(tf.matmul(W3, Z2) + b3)
Z4 = tf.matmul(W4, Z3) + b4
y_ = tf.sigmoid(Z4)

cost = tf.reduce_mean(tf.square(y_-Y))
learning_rate = 0.005
training_step = tf.train.AdamOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

training_epochs = 1000
cost_history = np.empty(shape=[1], dtype = float)
cost_meas_history = np.empty(shape=[1], dtype = float)

train_x = np.transpose(data)
train_y = np.transpose(targets)

cost_history = []
for epoch in range(training_epochs+1):

    for i in range(0, train_x.shape[0], batch_size):
        x_batch = train_x[i:i + batch_size,:]
        y_batch = train_y[i:i + batch_size,:]

        sess.run(training_step, feed_dict = {X: x_batch, Y: y_batch})

    cost_ = sess.run(cost, feed_dict={ X:train_x, Y: train_y})
    cost_history = np.append(cost_history, cost_)

    if (epoch % 5000 == 0):
        print("Reached epoch",epoch,"cost J =", cost_)

这段代码运行良好,它在我的笔记本电脑上运行 1000 epochs 5 秒。现在我用代码用 keras 开发了相同的网络

model = tf.keras.Sequential()
model.add(layers.Dense(15, input_dim=16, activation='sigmoid'))
model.add(layers.Dense(15, activation='sigmoid'))
model.add(layers.Dense(15, activation='sigmoid'))

model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer=tf.train.AdamOptimizer(0.005),
          loss='mse',
          metrics=['mae'])

# Training Phase
model.fit(train_x.transpose(), train_y.transpose()/100.0, epochs=1000, batch_size=100,verbose = 0)

此代码需要 43 秒。有谁知道这是怎么回事?现在我预计 Keras 会慢一些,但不会很多。我错过了什么?

谢谢,翁贝托

标签: pythontensorflowkeras

解决方案


好的,我找到了原因……这是我的错误。由于一系列错误,由于在午夜后的晚上编程(...),我意识到我正在比较批处理 GD 和 mini-batch GD。我向大家道歉,感谢今天注意到我的错误......如果有人认为应该删除这对我来说很好。

现在 Keras 和 plain TF 所用的时间完全相同。感谢大家阅读。

最好的,翁贝托


推荐阅读