美文网首页
19、tensorflow神经网络

19、tensorflow神经网络

作者: 羽天驿 | 来源:发表于2020-05-12 11:09 被阅读0次

一、原理

“TensorFlow是一个使用数据流图进行数值计算的开源软件库。图中的节点表示数学运算,而图边表示在它们之间传递的多维数据阵列(又称张量)。灵活的体系结构允许你使用单个API将计算部署到桌面、服务器或移动设备中的一个或多个CPU或GPU。
[图片上传失败...(image-3d5b1e-1589252952983)])

(一)、Tensorflow

  • 所有的深度学习的原理都是类似的。
  • pip install tensorflow
  • 这个包比较大,包扣cpu,Gpu

(二)、深度学习

  • keras--高就api
  • 对方法进行了简单的封装
  • pytorch 是Facebook开发的
  • cntk--微软开发的
  • paddlepaddle百度开发的
  • 上面这些的基本的原理都是类似的
  • 我们主要介绍tensorflow pytorch
  • 市场需要--tensorflow需求最多的

(三、)tensorflow入门及原理

  • 官方网站:
    http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html
  • 什么是机器?
  • 涉及多个领域
  • 原来使用sklearn机器学习
  • 现在使用TensorFlow
  • 差异只是计算方式不一样
    *深度学习,神经网络模仿人类大脑的工作方式
    *解决实际的问题
  • 什么是深度学习(神经网络)?
  • 是一种模仿动物的神经网络结构进行信息处理的数学的模型,在工程与学术界也常直接简称为神经网络或类神经网络。
  • 深度学习:思维的转变---深度学习模仿大脑的操作,有一些模型是不可以解释的,它的模型解决问题了,但是不知道原理是怎么样的。
  • 模型相当的复杂,它是由几百万或者上千万的参数组成的,这就是相当复杂的。
  • 所以深度学习这个模型,和sklearn的模型是完全不一样的。
  • 深度学习不仅仅是代码--更是一种人类的思想.。
  • 不能把机器学习理解成简单的编程=代码的实现,更是要理解成一种人工智能神经网络的实现。

(四、)mnist数据加载的不同的方式。

  • 方式一:
  • from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
# Import MNIST
from tensorflow.examples.tutorials.mnist import input_data
# 电脑上没有压缩文件,自动网上下载!!!
mnist = input_data.read_data_sets(one_hot=True,validation_size = 0)# 告诉这个方法,去当前目录下加载树
# Load data
X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels
Extracting C:/Users/softpo.DESKTOP-PN692CT/tfdata/train-images-idx3-ubyte.gz
Extracting C:/Users/softpo.DESKTOP-PN692CT/tfdata/train-labels-idx1-ubyte.gz
Extracting C:/Users/softpo.DESKTOP-PN692CT/tfdata/t10k-images-idx3-ubyte.gz
Extracting C:/Users/softpo.DESKTOP-PN692CT/tfdata/t10k-labels-idx1-ubyte.gz

使用Tensorflow中的API对数据,进行批量获取

后面目的:同tf.data为神经网咯提供数据

data = tf.data.Dataset.from_tensor_slices((X_train,Y_train)).repeat(10).shuffle(5000).batch(1000)
i = 0
for d in data:
    i+=1
print(i)
600

可视化

import matplotlib.pyplot as plt
plt.imshow(X_train[10000].reshape(28,28))
digit = Y_train[10000] #独热编码,在索引7的位置,占位是1
print(digit)
print(digit.argmax())
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
3
output_4_1.png
  • 方式二
  • from tensorflow.keras.datasets import mnist

导包keras中加载的mnist

from tensorflow.keras.datasets import mnist
import tensorflow as tf
(X_train,y_train),(X_test,y_test) = mnist.load_data()

可视化

import matplotlib.pyplot as plt
plt.imshow(X_train[1022].reshape(28,28))
<matplotlib.image.AxesImage at 0x14bde488>
output_5_1.png

将数据打乱顺序批量获取数据

# Tensor 矢量,张量,numpy数组
# TensorFlow 张量的流动计算
# slices 切片
data = tf.data.Dataset.from_tensor_slices((X_train,y_train))#加载数据
data = data.repeat(10)#重复
data = data.shuffle(5000)#打乱顺序
data = data.batch(1000)#一批去多少数据
# 原数据 60000个,一批取1000个取 60次,重复10次 总共取出600次
i = 0
for d in data:
    i+=1
print(i)
600

(五、)tensorflow基本操作

  • 加减乘除---等
  • 使用tf进行数据的计算的时候,数据的类型必须是对应的。
  • a2=tf.cast(a,tf.float32)这个是可以转换数据的类型的。
  • tf.reshape() tf中改变数据的形状
  • tensorflow的基本操作和numpy中操作是类似的,许多方法名字都是一样的,基本都是一样的、
  • 代码示例:
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.randint(0,10,size=(3,5)))
b = tf.constant(np.random.randint(0,10,size=(1,5)))

c=a+b
c
# 上面的a b的形式不一样的但是还是可以进行运算是你因为,numpy广播极致
<tf.Tensor: shape=(3, 5), dtype=int32, numpy=
array([[13, 15, 15,  8,  8],
       [12, 11, 15,  5,  3],
       [11, 18, 17, 12,  5]])>
# tensorflow为我们提供了方法
tf.add(a,b)
tf.multiply(a,b)

<tf.Tensor: shape=(3, 5), dtype=int32, numpy=
array([[36, 54, 56, 16,  0],
       [32, 18, 56,  4,  0],
       [28, 81, 72, 32,  0]])>
tf.pow(8.0,0.5)
# 幂运算
<tf.Tensor: shape=(), dtype=float32, numpy=2.828427>
a2=tf.cast(a,tf.float32)
a2
# 数据类型的转换
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[9., 6., 7., 4., 8.],
       [8., 2., 7., 1., 3.],
       [7., 9., 9., 8., 5.]], dtype=float32)>
tf.pow(a2,0.5)
# 注意使用tf计算的时候,数据的类型必须是对应的
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[3.       , 2.4494898, 2.6457512, 2.       , 2.828427 ],
       [2.828427 , 1.4142135, 2.6457512, 1.       , 1.7320508],
       [2.6457512, 3.       , 3.       , 2.828427 , 2.236068 ]],
      dtype=float32)>
tf.math.log(10.0)
# int类型是无法进行计算的。浮点数是可以的
# 对数的计算
<tf.Tensor: shape=(), dtype=float32, numpy=2.3025851>

降维操作

a=tf.cast(a,tf.int32)
# tf.cast()===类型转换方法
# 上面的a是一个Tensor对
# 这个numpy就是之前的numpy
# 获取数据
a.numpy()
array([[9, 6, 7, 4, 8],
       [8, 2, 7, 1, 3],
       [7, 9, 9, 8, 5]])
a.shape
TensorShape([3, 5])
a.numpy().sum(axis=0)
array([24, 17, 23, 13, 16])
tf.reduce_sum(a,axis=0)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([24, 17, 23, 13, 16])>
# 这个是tf提供的求和的方法。这个是降维的操作

tensorflow 和我们之前学过的numpy都是类似的

tf的矩阵运算

b=tf.constant(tf.random.normal(shape=[15]))
b=tf.reshape(b,shape=(5,3))
# tf.reshape  tf中改变数据的形状
# tf.float32  tf中改变数据类型的方法

a=tf.cast(a,tf.float32)
# a 和b进行矩阵运算
tf.matmul(a,b)
# b的形状不对应,
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[-13.390648 ,  -5.2129335,  12.440157 ],
       [ -5.132538 ,  -8.135062 ,   6.5191073],
       [-19.185709 , -15.828344 ,  19.550457 ]], dtype=float32)>

(六、)tensorflow的变量

  • 常量: tf.constant()常量
  • 变量:tf.Variable() 变量
  • 算法训练过程中,变量是调整的,变大变小,适应我们的数据--这就是变量
  • 常量是无法修改的数据
  • 变量是可以修改的数据
  • 变量的应用:大小的变换,参数得是可以调整的都是适用变量来定义的。
  • 总体 来说变量的定义和我们所指定的变量的定义是一样的,不同的是定义的方式是不一样的。
    *常量变量代码如下:

相关文章

网友评论

      本文标题:19、tensorflow神经网络

      本文链接:https://www.haomeiwen.com/subject/efxdvhtx.html