【问题标题】:Could someone explain me the basic tutorial of TensorFlow?有人可以解释一下TensorFlow的基本教程吗?
【发布时间】:2017-06-24 08:36:56
【问题描述】:

我正在尝试阅读 TensorFlow 第一篇教程的第二部分: https://www.tensorflow.org/get_started/get_started

“基本用法”:

import tensorflow as tf
# NumPy is often used to load, manipulate and preprocess data.
import numpy as np

# Declare list of features. We only have one real-valued feature. There are many
# other types of columns that are more complicated and useful.
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]

# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# logistic regression, linear classification, logistic classification, and
# many neural network classifiers and regressors. The following code
# provides an estimator that does linear regression.
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

# TensorFlow provides many helper methods to read and set up data sets.
# Here we use two data sets: one for training and one for evaluation
# We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7, 0.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x_train}, y_train,
                                              batch_size=4,
                                              num_epochs=1000)
eval_input_fn = tf.contrib.learn.io.numpy_input_fn(
    {"x":x_eval}, y_eval, batch_size=4, num_epochs=1000)

# We can invoke 1000 training steps by invoking the  method and passing the
# training data set.
estimator.fit(input_fn=input_fn, steps=1000)

# Here we evaluate how well our model did.
train_loss = estimator.evaluate(input_fn=input_fn)
eval_loss = estimator.evaluate(input_fn=eval_input_fn)
print("train loss: %r"% train_loss)
print("eval loss: %r"% eval_loss)

谁能解释一下,这段代码中隐藏的计算图在哪里? 我没有看到对 tf.Graph()tf.Session() 的任何呼叫。

features 变量的用途是什么?数据似乎永远不会进入其中,因为数据提供者是“input_fn”。 如何查看会话和图形的实际计算图?

为什么有两个地方可以设置 epoch 的数量? (estimator.fitnumpy_input_fn

如果我有两个不同的估算器 estimator1.fit(..., steps=20)estimator2.fit(..., steps=50) 怎么办? 我需要设置num_epochs=70吗?还是num_epochs=max(20,50)input_fn如何控制线程数,如果是从fit调用的,反之不行?

【问题讨论】:

    标签: machine-learning tensorflow deep-learning


    【解决方案1】:
    1. tf.Graph() 和 tf.Session() 在哪里?

    Tensorflow 会为您创建一个默认的计算图。所以所有的 您在上面定义的变量和操作将默认使用 图形。会话在估算器函数中定义。为了 示例 estimator.fit() 创建一个受监控的培训课程。

    1. 特征变量有什么用?

    用于初始化 LinearRegressor() 模型。根据特征变量设置线性回归参数。

    1. 在 input_fn 和估计器上设置的时期?

    input_fn 是一个队列,所以 'input_fn' 中的 epoch 告诉我们多少次 每个输入数据都需要弹出到队列中。 “适合”措施的时代 每个输入需要在训练中使用多少次。因此,如果您的 fit epoch 大于 queue epoch,则训练将在 queue 时停止 停下来。

    1. 两个不同估计器的队列大小。

    'input_fn' epoch 应该大于或等于 估计器中的时期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 2016-12-12
      • 2018-03-29
      • 2019-06-05
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多