Tensorflow的Hello World---创建一个简单的神经网络

Coursera:convolutional neural networks tensorflow

https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/home/welcome

  • 传统编程和机器学习的区别:

Tensorflow的Hello World---创建一个简单的神经网络_第1张图片Tensorflow的Hello World---创建一个简单的神经网络_第2张图片

传统的编程输入的是数据和规则,而机器学习的输入则是数据和结果,从数据和结果里找到它们的对应规则

  • 一个例子:

给出 X 和 Y,预测 X = 10 时,Y 的值。

X = -1, 0, 1, 2, 3, 4

Y = -3, -1, 1, 3 ,5, 7

X = 10,Y = ?

其实就是找到 X 和 Y 之间的 Rule。

  • 用机器学习来实现:
import tensorflow as tf
import numpy as np
from tensorflow import keras

# Keras:API in TensorFlow,Keras make it really easy to define neural network
# Dense:define a layer of connected neurons
model = keras.Sequential([keras.layers.Dense(units = 1,input_shape = [1])])

# sgd: optimizer,stands for stochastic gradient descent
# mean_squared_error: loss function
model.compile(optimizer = 'sgd',loss = 'mean_squared_error')

xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype = float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype = float)

# epochs: will go through the training loop 500 times.
model.fit(xs,ys,epochs = 5000)
print(model.predict([7.0]))

neural network: is basically a set of functions which can learn patterns

loss function: 测量预测结果的好与坏,然后将数据传递给 optimizer

optimizer: thinks about how good or how badly the guess was done using the date from the loss function

Tensorflow的Hello World---创建一个简单的神经网络_第3张图片

可以看出在经过5000次epoch后我们的神经网络给出的预测结果已经非常接近正确值13

 

你可能感兴趣的:(Tensorflow的Hello World---创建一个简单的神经网络)