tensorflow入门

代码举例

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import tensorflow as tf


# In[2]:


a = tf.constant(2)
b = tf.constant(3)
c=a+b
d=a*b


# In[5]:


c
# 这里可以知道其实并不是计算c的步骤,要计算从c要使用session会话激活c


# In[4]:


# 看成一个model
sess=tf.Session()
print (sess.run(c))
print (sess.run(d))


# In[9]:


# 相当于数据的接口,也是传入数据的方式
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)


# In[10]:


a


# In[7]:


add = tf.add(a, b)
mul = tf.multiply(a, b)
# 以字典形式赋值
print (sess.run(add, feed_dict={a: 2, b: 3}))
print (sess.run(mul, feed_dict={a: 2, b: 3}))


# In[11]:


matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
# 矩阵乘法
product = tf.matmul(matrix2, matrix1)
print (sess.run(product))


# In[12]:


mat1=tf.Variable(tf.random_normal([3,2]))
mat2=tf.Variable(tf.random_normal([2,3]))
product=tf.matmul(mat1,mat2)


# In[13]:


m1=[[1,3],[2,1],[0,5]]
m2=[[3,2,1],[1,2,3]]
# 输出矩阵相乘结果,以字典赋值的数据进行运算
print (sess.run(product,feed_dict={mat1:m1,mat2:m2}))


# In[14]:


import tensorflow as tf
import numpy
rng = numpy.random


# In[15]:


# 学习率
learning_rate = 0.02
# 轮数
training_epochs = 3000
display_step=50


# In[16]:


train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]


# In[17]:


X = tf.placeholder("float")
Y = tf.placeholder("float")


# In[18]:


# 参数
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")


# In[19]:


# Construct a linear model
# 线性模型(前向)
pred = tf.add(tf.multiply(X, W), b)


# In[20]:


# Mean squared error
# 损失函数(反向)
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
# 梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


# In[21]:


# Initializing the variables
# 初始化变量
init = tf.global_variables_initializer()


# In[22]:


# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c),                 "W=", sess.run(W), "b=", sess.run(b))


    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

 

    # the testing data
    test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
    test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])

    print("Tssting...")
    testing_cost = sess.run(
        tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]),
        feed_dict={X: test_X, Y: test_Y})  # same function as cost above
    print("Test LOSS=", testing_cost)
    print("Final Loss:", abs(
        training_cost - testing_cost))


线性回归

线性关系来描述输入到输出的映射关系
优化方法:梯度下降
山坡高度:LOSS
地面位置:参数
山坡最低点:LOSS MINIMAL
最低点位置:目标参数
怎么到达:下坡方向、梯度下降
怎么找方向:高度对地面方向求导

梯度下降

1.随机初始化参数
2.开启循环:
带入结果求出结果
与真值进行比较得到loss
对变量求导得到梯度
更新参数
当loss足够小停止

局限性

能够清楚描述分割线性分布的数据,对非线性分布无法适用

从线性到非线性

非线性激励

考量标准:1.正向对输入的调整
2.反向梯度损失

1.sigmoid

y(x)=sigmoid(x)=11+e−xy(x)=sigmoid(x)=\frac{1}{1+e^{-x}}y(x)=sigmoid(x)=1+ex1
y(x)′=y(x)(1−y(x))y(x)'=y(x)(1-y(x))y(x)=y(x)(1y(x))
将输入数据映射到【0,1】中
但是当输入很大或者很小时几乎没有梯度-梯度饱和、梯度弥散

2.tahn

f(x)=tahn(x)=21+e−2x−1f(x)=tahn(x)=\frac{2}{1+e^{-2x}}-1f(x)=tahn(x)=1+e2x21
f′(x)=1−f2(x)f'(x)=1-f^2(x)f(x)=1f2(x)
将数据映射到【-1,1】中
同上

3.relu

f(x)=max(0,x)f(x)=max(0,x)f(x)=max(0,x)
优点1:Krizhevsky et al. 发现使用 ReLU 得到的SGD的收敛速度会比 sigmoid/tanh 快很多(如上图右)。有人说这是因为它是linear,而且梯度不会饱和
优点2:相比于 sigmoid/tanh需要计算指数等,计算复杂度高,ReLU 只需要一个阈值就可以得到激活值。
缺点1: ReLU在训练的时候很”脆弱”,一不小心有可能导致神经元”坏死”。

4.leaky relu

f(x)=max(0.01x,x)f(x)=max(0.01x,x)f(x)=max(0.01x,x)
Leaky ReLUs 就是用来解决ReLU坏死的问题的。和ReLU不同,当x<0时,它的值不再是0,而是一个较小斜率(如0.01等)的函数。也就是说f(x)=1(x<0)(ax)+1(x>=0)(x),其中a是一个很小的常数。这样,既修正了数据分布,又保留了一些负轴的值,使得负轴信息不会全部丢失。a不能太大,不然就变成线性问题了。
注意:没有线性回归网络,不然所有的都可以归为一层了

神经网络构建

每一个神经元的数值由前一层神经元数值,神经元参数W,b以及激励函数共同决定第n+1层第k个神经元的方程可由公式表示为
zn+1,k=∑i=1mWn,k,ixn,i+bn,kz_{n+1,k}=\sum_{i=1}^{m}{W_{n,k,i}x_{n,i}+b_{n,k}}zn+1,k=i=1mWn,k,ixn,i+bn,k 利用前一层所有的值通过不同的权值和偏置计算
yn+1,k=11+e−zn+1,ky_{n+1,k}=\frac{1}{1+e^{-z_{n+1,k}}}yn+1,k=1+ezn+1,k1然后通过飞机或函数进行计算下一层的值

神经网络优化

链式法则

计算梯度

loss−−δy−−δx−−δwloss--\delta{y}--\delta{x}--\delta{w}lossδyδxδw

代码示例

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(".", one_hot=True)

import tensorflow as tf

# Parameters
learning_rate = 0.001
training_epochs = 30
# 每次训练的个数
batch_size = 100
display_step = 1

# Network Parameters
# 神经元的个数/相当于层的宽度,深度为2
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 512 # 2nd layer number of features
# 输入的维度
n_input = 784 # MNIST data input (img shape: 28*28)
# 输出10个类
n_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])


# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    # 第一层
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    # 激活函数
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

    #we can add dropout layer
    # 防止过拟合
    # drop_out = tf.nn.dropout(layer_2, 0.75)


    # Output layer with linear activation
    # 到输出层
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Store layers weight & biases
# 权值
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
# 偏置
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
# 得到输出
pred = multilayer_perceptron(x, weights, biases)

# Define loss and optimizer
# 损失和目标
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Initializing the variables
# 初始化
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    # 开始训练
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                                          y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
    print("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

神经网络的配件

损失函数loss

预测值和真值之间的差值

1.softmax

δzj=ezj∑k=1Kezk\delta{z}_j=\frac{e^{z_j}}{\sum_{k=1}^{K}{e^{z_k}}}δzj=k=1Kezkezj
该函数是单调增函数,即输入值越大,输出也就越大,关注最大的

2.corss entropy

L(w)=−1N∑n=1N[ynlogy^n+(1−yn)log(1−y^n)]L(w)=-\frac{1}{N}\sum_{n=1}^{N}{[y_nlog \hat{y}_n+(1-y_n)log (1-\hat{y}_n)]}L(w)=N1n=1N[ynlogy^n+(1yn)log(1y^n)]
当误差越大,梯度就越大,参数w调整得越快,训练速度也就越快,生成【0,1】

3.自定义

学习率

动量

正常 x+=−leanrningrate∗dxx+=-leanrningrate*dxx+=leanrningratedx
动量
v=mu∗v−learningrate∗dxv=mu*v-learningrate*dxv=muvlearningratedx
x+=vx+=vx+=v
动量比直接调大学习率会找到更好的方向

过拟合

应对:
1.正则化regularization,即使参数更加趋于平衡weigh decay
2.dropout,将一些参数随机置零,也能达到参数均衡的效果
3.fine-tuning大部分的参数不同更新

<think>好的,我需要回答用户关于TensorFlow入门教程的问题。首先,用户引用了几个相关的资料,我需要结合这些引用内容来组织答案。根据系统级指令,要使用中文,并且结构清晰,分步骤解决问题。同时,需要生成相关问题,并在引用处添加标识。 首先,我应该从用户提供的引用中提取有用的信息。引用[1]提到TensorFlow的官方文档是重要资源,包括API参考、教程和指南,特别是模型园部分。引用[2]和[3]强调了TensorFlow作为开源框架在深度学习中的重要性,以及它能够构建和部署各种模型。引用[4]提到实战书籍,有代码实例和操作步骤,适合由浅入深学习。引用[5]介绍了Skip-gram模型,属于词向量训练,这可能属于自然语言处理的应用案例。 接下来,我需要整理TensorFlow的基础知识入门路径。用户想学习基础知识,可能需要了解安装、核心概念(如张量、计算图、会话)、基本模型构建步骤、常见应用案例,以及官方资源推荐。根据引用内容,可以分步骤讲解这些内容。 在回答中,需要确保所有数学表达式用$...$格式,独立公式用$$...$$。例如,提到线性回归模型时,损失函数可能需要用LaTeX表示。同时,要自然地插入引用标记,比如在提到官方文档时添加[^1],提到实战书籍时添加[^4],等等。 然后,生成相关问题。可能需要围绕用户可能感兴趣的下一步学习步骤提问,比如性能优化、模型部署、应用场景等,确保问题与主题相关,并基于引用内容。 最后,检查是否符合所有格式要求,避免使用Markdown,保持口语化,但结构清晰。确保每个引用只在段落末尾标注,并且正确对应引用的来源。</think>### TensorFlow 入门教程与机器学习框架基础 TensorFlow 是由 Google 开发的开源机器学习框架,广泛用于构建深度学习模型和其他机器学习任务。以下是逐步入门指南: --- #### **1. 安装与环境配置** - **安装方式**:通过 `pip` 安装 TensorFlow: ```bash pip install tensorflow ``` 若需 GPU 支持,则安装 `tensorflow-gpu`(需提前配置 CUDA 和 cuDNN)[^3]。 - **验证安装**: ```python import tensorflow as tf print(tf.__version__) # 输出版本号,如 2.10.0 ``` --- #### **2. 核心概念** - **张量(Tensor)**:TensorFlow 的基础数据结构,可理解为多维数组。例如: - 标量:`tf.constant(3)`,即 $3$。 - 向量:`tf.constant([1,2])`,即 $\begin{bmatrix}1 & 2\end{bmatrix}$。 - **计算图(Graph)**:定义模型的计算流程(TensorFlow 2.x 默认启用即时执行模式,无需手动建图)[^2]。 - **层(Layer)**:预定义的神经网络组件,如 `tf.keras.layers.Dense`。 --- #### **3. 构建第一个模型:线性回归** 以预测房价为例,模型表达式为 $y = wx + b$,通过最小化均方误差(MSE)训练参数: $$ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - (wx_i + b))^2 $$ **代码实现**: ```python import tensorflow as tf # 生成数据 X = tf.constant([1, 2, 3, 4], dtype=tf.float32) y = tf.constant([2, 4, 6, 8], dtype=tf.float32) # 定义模型 model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])]) # 编译模型(指定优化器和损失函数) model.compile(optimizer='sgd', loss='mean_squared_error') # 训练模型 model.fit(X, y, epochs=100) # 预测 print(model.predict([5])) # 输出接近 10 ``` 此案例展示了如何用 TensorFlow 实现简单的监督学习。 --- #### **4. 应用场景与扩展** - **图像分类**:使用预训练模型(如 ResNet)快速实现图像识别。 - **自然语言处理**:如引用[^5]提到的 Skip-gram 模型,用于生成词向量。 - **部署与优化**:通过 TensorFlow Lite 将模型部署到移动端,或使用 TensorFlow Serving 进行服务化。 --- #### **5. 学习资源推荐** - **官方文档**:TensorFlow 官网提供完整的教程和 API 文档,适合系统性学习[^1]。 - **实战书籍**:如引用提到的书籍,通过案例逐步掌握框架。 - **社区与课程**:Kaggle 竞赛和 Coursera 专项课程可提升实战能力。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值