keras与低水平TF相比非常慢吗?

时间:2018-12-01 21:15:27

标签: python tensorflow keras

我对Keras感到好奇。 信息:输入数据集形状

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

问题:回归

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

var arrayarray= [
  "1.jpg",
  "2.jpg",
  "3.jpg",
  "4.jpg"
]; 
var clicked =[];
function asdasd (){
   for (var i = 0, j = arrayarray.length; i < j; i++) {
     var fmg = document.createElement('img');
       fmg.setAttribute("src", arrayarray[i]);
       fmg.setAttribute("class", "fmss");
       container.appendChild(fwfw);
       if (??){
         clicked.push(i);
       }
    }
}

此代码运行得很好,并且在我的笔记本电脑上需要5秒钟的1000个时间段。现在,我用代码开发了与keras相同的网络

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_)

此代码需要43秒。有谁知道情况如何?现在我希望Keras会慢一些,但不会慢得多。我想念什么?

谢谢,翁贝托

1 个答案:

答案 0 :(得分:0)

好的,我找到了原因……这是我的错误。由于一系列错误,由于在午夜(...)之后的晚上进行编程,我意识到我正在比较批GD和小型批GD。我向所有人道歉,并感谢今天注意到我的错误。 如果有人认为应该删除它对我很好。

现在Keras和平原TF花费的时间完全相同。谢谢大家阅读。

最佳,翁贝托