tensorflow基本操作小白快速构建线性回归和分类模型
作者:刘润森! 发布时间:2022-09-16 17:13:37
标签:tensorflow,线性回归,分类模型
TF 目前发布2.5 版本,之前阅读1.X官方文档,最近查看2.X的文档。
tensorflow是非常强的工具,生态庞大
tensorflow提供了Keras的分支
这里不再提供Keras相关顺序模型教程。
关于环境:ubuntu的 GPU,需要cuda和nvcc
不会安装:查看
完整的Ubuntu18.04深度学习GPU环境配置,英伟达显卡驱动安装、cuda9.0安装、cudnn的安装、anaconda安装
不安装,直接翻墙用colab
测试GPU
>>> from tensorflow.python.client import device_lib
>>> device_lib.list_local_devices()
这是意思是挂了一个显卡
具体查看官方文档:https://www.tensorflow.org/install
服务器跑Jupyter
Define tensor constants.
import tensorflow as tf
# Create a Tensor.
hello = tf.constant("hello world")
hello
# Define tensor constants.
a = tf.constant(1)
b = tf.constant(6)
c = tf.constant(9)
# tensor变量的操作
# (+, *, ...)
add = tf.add(a, b)
sub = tf.subtract(a, b)
mul = tf.multiply(a, b)
div = tf.divide(a, b)
# 通过numpy返回数值 和torch一样
print("add =", add.numpy())
print("sub =", sub.numpy())
print("mul =", mul.numpy())
print("div =", div.numpy())
add = 7
sub = -5
mul = 6
div = 0.16666666666666666
mean = tf.reduce_mean([a, b, c])
sum_ = tf.reduce_sum([a, b, c])
# Access tensors value.
print("mean =", mean.numpy())
print("sum =", sum_ .numpy())
mean = 5
sum = 16
# Matrix multiplications.
matrix1 = tf.constant([[1., 2.], [3., 4.]])
matrix2 = tf.constant([[5., 6.], [7., 8.]])
product = tf.matmul(matrix1, matrix2)
product
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[19., 22.],
[43., 50.]], dtype=float32)>
# Tensor to Numpy.
np_product = product.numpy()
print(type(np_product), np_product)
(numpy.ndarray,
array([[19., 22.],
[43., 50.]], dtype=float32))
Linear Regression
下面使用tensorflow快速构建线性回归模型,这里不使用kears的顺序模型,而是采用torch的模型定义的写法。
import numpy as np
import tensorflow as tf
# Parameters:
learning_rate = 0.01
training_steps = 1000
display_step = 50
# Training Data.
X = np.array([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])
Y = np.array([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])
random = np.random
# 权重和偏差,随机初始化。
W = tf.Variable(random.randn(), name="weight")
b = tf.Variable(random.randn(), name="bias")
# Linear regression (Wx + b).
def linear_regression(x):
return W * x + b
# Mean square error.
def mean_square(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y_true))
# 随机梯度下降优化器。
optimizer = tf.optimizers.SGD(learning_rate)
# 优化过程。
def run_optimization():
# 将计算包在GradientTape中,以便自动区分。
with tf.GradientTape() as g:
pred = linear_regression(X)
loss = mean_square(pred, Y)
# 计算梯度。
gradients = g.gradient(loss, [W, b])
# 按照梯度更新W和b。
optimizer.apply_gradients(zip(gradients, [W, b]))
#按给定的步数进行训练。
for step in range(1, training_steps + 1):
# 运行优化以更新W和b值。
run_optimization()
if step % display_step == 0:
pred = linear_regression(X)
loss = mean_square(pred, Y)
print("Step: %i, loss: %f, W: %f, b: %f" % (step, loss, W.numpy(), b.numpy()))
import matplotlib.pyplot as plt
plt.plot(X, Y, 'ro', label='Original data')
plt.plot(X, np.array(W * X + b), label='Fitted line')
plt.legend()
plt.show()
分类模型
本例使用MNIST手写数字
数据集包含60000个训练示例和10000个测试示例。
这些数字已经过大小标准化,并在一个固定大小的图像(28x28像素)中居中,值从0到255。
在本例中,每个图像将转换为float32,标准化为[0,1],并展平为784个特征(28×28)的一维数组。
import numpy as np
import tensorflow as tf
# MNIST data
num_classes = 10 # 0->9 digits
num_features = 784 # 28 * 28
# Parameters
lr = 0.01
batch_size = 256
display_step = 100
training_steps = 1000
# Prepare MNIST data
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Convert to Float32
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
# Flatten images into 1-D vector of 784 dimensions (28 * 28)
x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])
# [0, 255] to [0, 1]
x_train, x_test = x_train / 255, x_test / 255
# 打乱顺序: tf.data API to shuffle and batch data
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.repeat().shuffle(5000).batch(batch_size=batch_size).prefetch(1)
# Weight of shape [784, 10] ~= [number_features, number_classes]
W = tf.Variable(tf.ones([num_features, num_classes]), name='weight')
# Bias of shape [10] ~= [number_classes]
b = tf.Variable(tf.zeros([num_classes]), name='bias')
# Logistic regression: W*x + b
def logistic_regression(x):
# 应用softmax函数将logit标准化为概率分布
out = tf.nn.softmax(tf.matmul(x, W) + b)
return out
# 交叉熵损失函数
def cross_entropy(y_pred, y_true):
# 将标签编码为一个one_hot向量
y_true = tf.one_hot(y_true, depth=num_classes)
# 剪裁预测值避免错误
y_pred = tf.clip_by_value(y_pred, 1e-9, 1)
# 计算交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_true * tf.math.log(y_pred), 1))
return cross_entropy
# Accuracy
def accuracy(y_pred, y_true):
correct = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))
return tf.reduce_mean(tf.cast(correct, tf.float32))
# 随机梯度下降优化器
optimizer = tf.optimizers.SGD(lr)
# Optimization
def run_optimization(x, y):
with tf.GradientTape() as g:
pred = logistic_regression(x)
loss = cross_entropy(y_pred=pred, y_true=y)
gradients = g.gradient(loss, [W, b])
optimizer.apply_gradients(zip(gradients, [W, b]))
# Training
for step, (batch_x, batch_y) in enumerate(train_dataset.take(training_steps), 1):
# Run the optimization to update W and b
run_optimization(x=batch_x, y=batch_y)
if step % display_step == 0:
pred = logistic_regression(batch_x)
loss = cross_entropy(y_pred=pred, y_true=batch_y)
acc = accuracy(y_pred=pred, y_true=batch_y)
print("Step: %i, loss: %f, accuracy: %f" % (step, loss, acc))
pred = logistic_regression(x_test)
print(f"Test Accuracy: {accuracy(pred, y_test)}")
Test Accuracy: 0.892300009727478
import matplotlib.pyplot as plt
n_images = 5
test_images = x_test[:n_images]
predictions = logistic_regression(test_images)
# 预测前5张
for i in range(n_images):
plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
plt.show()
print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))
Model prediction: 7
Model prediction: 2
Model prediction: 1
Model prediction: 0
Model prediction: 4
来源:https://maoli.blog.csdn.net/article/details/119043878


猜你喜欢
- 调用时输入参数如: www.jb51.net/表示删除www.jb51.net首页的缓存, www.jb51.net/test.
- 1、 下载MYSQL5.1.48源码,CMAKE,VS2008 2、 安装CMAKE和VS2008,解压MYSQL5.1.48到D:\mys
- 最近帮人做了个贪吃蛇的游戏(交作业用),很简单,界面如下:开始界面:游戏中界面:是不是很简单、朴素。(欢迎大家访问GitHub)游戏是基于P
- #这是Python中的一个字典 dic = { 'str': 'this is a string',
- 本文实例讲述了Python轻量级ORM框架Peewee访问sqlite数据库的方法。分享给大家供大家参考,具体如下:ORM框架就是 obje
- DataFrame对象本质上是带有行列索引的二维矩阵,所以欲对DataFrame对象进行转置操作,需要交换行列索引,同时使二维矩阵转置。首先
- mysql在查询上千万级数据的时候,通过索引可以解决大部分查询优化问题。但是在处理上亿数据的时候,索引就不那么友好了。数据表(日志)是这样的
- 描述:让Len,Left,Right函数识别中文;对中文识别为两个字符,ASCII码为一个;可用此函数代替Len,Left,Right函数。
- 本文实例讲述了Python使用matplotlib实现在坐标系中画一个矩形的方法。分享给大家供大家参考。具体实现方法如下:import ma
- 问题描述项目中需要用到流程图,如果用js的echarts处理,不同层级建动态计算位置比较复杂,考虑用python来实现测试demo实现效果如
- 如何编写一个只在Web服务关闭时执行的程序?如:<SCRIPT LANGUAGE="VBScript"&
- window对文件夹的操作主要包括移动/剪切/复制,本篇文章主要用jQuery来实现,下面一起来了解一下把。1.先看下效果吧!2.在添加一个
- 1、方法一在使用多线程更新 MongoDB 数据时,需要注意以下几个方面:确认您的数据库驱动程序是否支持多线程。在 PyMongo 中,默认
- 时间库—arrow使用背景日期时间处理在实际应用场景中无处不在,所以这也成了编程语言中必不可少的模块,Python 也不例外。但是,你知道在
- Pytorch:dtype不一致RuntimeError: Expected object of scalar type Double bu
- 最近Google Code推出了一个面向网站开发者的 * Google DocType。它来自于网站开发者同时又面
- 还是先上代码吧 ,可以先看 SQL语句去掉重复记录,获取重复记录ALTER procedure [dbo].[PROC_ITEMMASTER
- 通常情况下,我们想构建一张表单时会在模板文件login.html中写入<form action="/your-name/&q
- 栅格系统的形成1692年,新登基的法国国王路易十四感到法国的印刷水平强差人意,因此命令成立一个管理印刷的皇家特别委员会。他们的首要任务是设计
- 引言阿刁是一个自动化测试用例,从一出生他就被赋予终生使命,去测试一个叫登录的过程是否合理。他一直就被关在一个小黑屋里面,从来也没有出去过,小