“[…]”用十位数在磁盘上加载数据是什么意思

2021-05-16 08:12:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我从tensorflow和机器学习开始,但我有一本非常有用的书。我想实施小批量梯度下降。我完全按照他们说的去做,但没有成功。你知道吗

它的解释是:“最后,在执行阶段,一个接一个地获取小批,然后在计算依赖于其中任何一个的节点时,通过feed\u dict参数提供X和Y的值。”

我在用Jupyter notebooktensorflow 1.3.0.

以下是我尝试的:

    n_epochs=1000
    learning_rate=0.0001
    #X=tf.constant(housing_data_plus_bias,dtype=tf.float32,name="X")
    X=tf.placeholder(tf.float32,shape=(None,n+1),name="X")
    Y=tf.placeholder(tf.float32,shape=(None,1),name="Y")
    batch_size=100
    n_batches=int(np.ceil(m/batch_size))
    #Y=tf.constant(housing.target.reshape(-1,1),dtype=tf.float32,name="Y")
    theta=tf.Variable(tf.random_uniform([n+1,1],-1.0,1.0),name="theta")
    y_pred=tf.matmul(X,theta,name="predictions")  #eq 1.4
    error=y_pred - Y
    mse=tf.reduce_mean(tf.square(error),name="mse") #eq 1.5
    #gradients=tf.gradients(mse,[theta])[0]
    gradients= (2/(m*mse) ) * tf.matmul(tf.transpose(X),error) 
    training_op = tf.assign(theta,theta - learning_rate * gradients)

    def fetch_batch(epoch,batch_index,batch_size):
        [...]   #Load DATA FROM DISK (SEE NOTEBOOK)
        return X_batch, Y_batch

    init=tf.global_variables_initializer()

    with tf.Session() as sess:
       sess.run(init)
       for epoch in range(n_epochs):
           for batch_index in range(n_batches):
                X_batch,Y_batch=fetch_batch(epoch,batch_index,batch_size)
                sess.run(training_op,feed_dict={X:X_batch,Y:Y_batch})


    best_theta=theta.eval()
    print(best_theta)

下面是错误:

 ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-41-f199ccce6734> in <module>
     27     for epoch in range(n_epochs):
     28         for batch_index in range(n_batches):
---> 29             X_batch,Y_batch=fetch_batch(epoch,batch_index,batch_size)
     30             sess.run(training_op,feed_dict={X:X_batch,Y:Y_batch})
     31 

<ipython-input-41-f199ccce6734> in fetch_batch(epoch, batch_index, batch_size)
     19 def fetch_batch(epoch,batch_index,batch_size):
     20     [...]
---> 21     return X_batch, Y_batch
     22 
     23 init=tf.global_variables_initializer()

NameError: name 'X_batch' is not defined

所以我的问题是,我应该怎么处理这个[…],它是从磁盘加载数据的一种真正的方法,还是应该用一些东西来替换它?你知道吗