《Tensorflow》Getting Started with Tensorflow

本文是参考并分析Tensorflow官网上的学习教程,谨以日后Review

学习链接:https://www.tensorflow.org/get_started/get_started


tf.train API

这里从训练一个线性回归模型来介绍tf.train API的使用

代码

# Date: 2017-07-01
# Author: Amusi
# Content: 在官网上学习tensorflow基础教程(二): 机器学习训练
# url: https://www.tensorflow.org/get_started/get_started#the_computational_graph/

# Summary: The completed trainable linear regression model
# calculate the w(should be -1) and b(should be 1)

import numpy as np
import tensorflow as tf

print(tf.__version__)   # the version of tensorflow

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

分析

概述:上述代码给定一组x和y点坐标集,试图用linear_model(y)= W*x + b来拟合该曲线(坐标点集)

变量W和b是float类型,分别代表了代数中的斜率截距
x和y是模型输入和输出变量(float类型)
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares

loss是损失值,即拟合模型与训练数据的拟合误差损失
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

用优化器(optimizer)中的 梯度下降算法的优化器tf.train.GradientDescentOptimizer,实现对模型的优化
这里的学习率为0.01
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]

x_train和y_train是训练数据

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

上述代码是循环训练1000次,生成训练模型

curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

使用sess.run来评估训练的准确率(accuracy)

运行结果

W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11
上述W(斜率),训练结果是-0.9999969,接近-1;b(截距)的训练结果是0.99999082,接近1;损失值为5.699973e-11,接近0
可见损失值loss非常小。

tf.contrib.learn

tf.contrib.learn是一个高级Tensorflow函数库,其简化机器学习的机制,包括:
运行训练循环(running training loops)
运行估计循环(running evaluation loops)
管理数据集(managing data sets)
管理供给数据(managing feeding)

基本用法

注意,tf.contrib.learn可以简化线性回归程序

代码

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)

代码分析

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.])
x_train和y_train是训练(training)数据
x_eval和y_eval是评估(evaluation)数据

estimator.fit(input_fn=input_fn, steps=1000)
调用(invoke)1000次训练循环

train_loss = estimator.evaluate(input_fn=input_fn)
eval_loss = estimator.evaluate(input_fn=eval_input_fn)
计算训练和评估损失值

运行结果

train loss: {'global_step': 1000, 'loss': 4.3049088e-08}
eval loss: {'global_step': 1000, 'loss': 0.0025487561}
训练和评估损失值都很小,可见训练模型有效

自定义模型

假设我们想要创建一个Tensorflow未集成的自定义模型,我们仍然可以保留(retain)tf.contrib.learn的数据集、供给数据(feeding)、训练等抽象。例如,我们可以用自己所学的知识来实现一个等价于线性回归(LinearRegressor)的模型

为了结合tf.contrib.learn来自定义模型,我们需要使用tf.contrib.learn.Estimator。tf.contrib.learn.LinearRegressor实质上是tf.contrib.learn.Estimator的子类。为了替代子类,我们只提供Estimator的model_fn函数来告诉tf.contrib.learn如何估计预测,训练步骤和损失值。

代码

官方代码(有问题)
import numpy as np
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
  # Build a linear model and predict values
  W = tf.get_variable("W", [1], dtype=tf.float64)
  b = tf.get_variable("b", [1], dtype=tf.float64)
  y = W*features['x'] + b
  # Loss sub-graph
  loss = tf.reduce_sum(tf.square(y - labels))
  # Training sub-graph
  global_step = tf.train.get_global_step()
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = tf.group(optimizer.minimize(loss),
                   tf.assign_add(global_step, 1))
  # ModelFnOps connects subgraphs we built to the
  # appropriate functionality.
  return tf.contrib.learn.ModelFnOps(
      mode=mode, predictions=y,
      loss=loss,
      train_op=train)

estimator = tf.contrib.learn.Estimator(model_fn=model)
# define our data sets
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, 4, num_epochs=1000)

# train
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)

修改后的代码

# Date: 2017-07-18
# Author: Amusi
# Content: 在官网上学习tensorflow基础教程(三): 机器学习训练
# url: https://www.tensorflow.org/get_started/get_started

# Summary: A custom model
# The custom model that shows how to implement our own equivalent model to LinearRegressor using our knowledge of the lower level TensorFlow API.
import numpy as np
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
  # Build a linear model and predict values
  W = tf.get_variable("W", [1], dtype=tf.float64)
  b = tf.get_variable("b", [1], dtype=tf.float64)
  y = W*features['x'] + b
  # Loss sub-graph
  loss = tf.reduce_sum(tf.square(y - labels))
  # Training sub-graph
  global_step = tf.train.get_global_step()
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = tf.group(optimizer.minimize(loss),
                   tf.assign_add(global_step, 1))
  # ModelFnOps connects subgraphs we built to the
  # appropriate functionality.
  return tf.contrib.learn.ModelFnOps(
      mode=mode, predictions=y,
      loss=loss,
      train_op=train)

estimator = tf.contrib.learn.Estimator(model_fn=model)
# define our data sets
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, 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)

# train
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)

代码分析

官方代码中,eval_input_fn缺少定义,请参考stackoverflow
于是,自己定义了eval_input_fn函数
eval_input_fn = tf.contrib.learn.io.numpy_input_fn(
    {"x":x_eval}, y_eval, batch_size=4, num_epochs=1000)

运行结果

官网提供
train loss: {'global_step': 1000, 'loss': 4.9380226e-11}
eval loss: {'global_step': 1000, 'loss': 0.01010081}

自己运行结果
train loss: {'global_step': 1000, 'loss': 2.8277852e-11}
eval loss: {'global_step': 1000, 'loss': 0.010100801}


开始学习机器学习可以是一个令人兴奋和有趣的过程。以下是一些帮助你入门的步骤和建议。 步骤1:学习基本概念和理论 了解机器学习的基本概念、算法和技术是开始的第一步。你可以通过阅读教科书、学术论文或参加在线教育平台上的机器学习课程来学习这些知识。确保你对基本的数学和统计学概念有一定的了解,因为它们与机器学习密切相关。 步骤2:选择适当的编程语言和工具 机器学习的实现通常需要编程来处理和分析数据集。选择适合你的需求的编程语言,如Python或R,并熟悉与机器学习相关的库和工具,如Scikit-Learn、TensorFlow或PyTorch。 步骤3:实践和探索数据集 找到适合初学者的数据集,可以是公开可用的数据集或者自己收集的数据。通过使用所选编程语言和工具,将数据导入和处理,并探索数据集的不同特征和模式。 步骤4:选择合适的机器学习算法 根据你的数据和问题类型选择合适的机器学习算法。有监督学习、无监督学习和强化学习是常见的算法类型。根据你的研究方向和目标,选择合适的算法来训练模型。 步骤5:训练和评估模型 使用你的数据集来训练机器学习模型,并使用评估指标来评估模型的性能。这可以帮助你了解模型的准确性和效果,并根据需要进行改进。 步骤6:调整和优化模型 通过调整模型的超参数、改变特征工程方法或尝试其他算法来进一步改进模型的性能。这是一个迭代的过程,可以帮助你逐渐提高模型的准确性和泛化能力。 步骤7:实际应用和持续学习 将机器学习模型应用到实际问题中,探索更多的数据和场景。保持对最新研究和技术的学习,并与机器学习社区保持联系,以不断提升自己的技能和知识。 通过以上步骤,你可以开始你的机器学习之旅,并逐渐提高自己在这个领域的技能水平。记住,持续学习和实践是成为一名优秀的机器学习从业者的关键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值