Deep learning with python 学习笔记(一)

一、前馈神经网络

 第三章: 编写前馈神经网络的代码:

#Layer Neural Network for Regression

import autograd.numpy as np
import autograd.numpy.random as npr
from  autograd import  grad
import sklearn.metrics
import pylab

#Generate Dataset 初始化数据集
examples = 1000
features = 100
D = (npr.randn(examples,features),npr.randn(examples))

#specify the network 定义神经网络
layer1_units = 10
layer2_units = 1
w1 = npr.rand(features,layer1_units)  #定义w,b
b1 = npr.rand(layer1_units)
w2 = npr.rand(layer1_units,layer2_units)
b2 = 0.0
theta = (w1,b1,w2,b2)  #θ

#define the loss function 定义损失函数
def squared_loss(y,y_hat):    #y_hat:即为f函数
    return np.dot((y-y_hat),(y-y_hat))  #np.dot 矩阵的乘法

#output layer 输出层
def binary_cross_entropy(y,y_hat):  #交叉熵
    return np.sum(-(y*np.log(y_hat))+(1-y)*np.log(1-y_hat))

#wraper around the  NN  包装神经网络
def neural_network(x,theta):
    w1,b1,w2,b2 = theta
    return np.tanh(np.dot((np.tanh(np.dot(x,w1)+b1)),w2)+b2)

#wrapper around the bojective function  to be optimised 最优化
def objective(theta,idx):
    return  squared_loss(D[1][idx],neural_network(D[0][idx],theta))

#update  更新
def updata_theta(theta,delta,alpha):
    w1,b1,w2,b2 = theta
    w1_delta,b1_delta,w2_delta,b2_delta = delta #delta 增量  △

    w1_new = w1 - alpha * w1_delta
    b1_new = b1 - alpha * b1_delta
    w2_new = w2 - alpha * w2_delta
    b2_new = b2 - alpha * b2_delta

    new_theta = (w1_new,w2_new,b1_new,b2_new)
    return  new_theta

#compute gradient  计算梯度
grad_objective = grad(objective)

#Train the NN  训练神经网络
epochs = 10
print("RMSE before traning:",sklearn.metrics.mean_squared_error(D[1],neural_network(D[0],theta))) #标准误差
rmse = []

for i in range(0,epochs):
    for j in range(0,examples):
        delta = grad_objective(theta,j)
        theta = updata_theta(theta,delta,0.01)

rmse.append(sklearn.metrics.mean_squared_error(D[1],neural_network(D[0],theta)))
print("RMSE after traning",sklearn.metrics.mean_squared_error(D[1],neural_network(D[0],theta)))
pylab.plot(rmse)
pylab.show()

结果:

RMSE before traning: 1.89717170214

RMSE after traning 1.06282114173

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Deep Learning Projects: 9 projects demystifying neural network and deep learning models for building intelligent systems By 作者: Matthew Lamons – Rahul Kumar – Abhishek Nagaraja ISBN-10 书号: 1788997093 ISBN-13 书号: 9781788997096 出版日期: 2018-10-31 pages 页数: (670) Deep learning has been gradually revolutionizing every field of artificial intelligence, making application development easier. Python Deep Learning Projects imparts all the knowledge needed to implement complex deep learning projects in the field of computational linguistics and computer vision. Each of these projects is unique, helping you progressively master the subject. You’ll learn how to implement a text classifier system using a recurrent neural network (RNN) model and optimize it to understand the shortcomings you might experience while implementing a simple deep learning system. Similarly, you’ll discover how to develop various projects, including word vector representation, open domain question answering, and building chatbots using seq-to-seq models and language modeling. In addition to this, you’ll cover advanced concepts, such as regularization, gradient clipping, gradient normalization, and bidirectional RNNs, through a series of engaging projects. By the end of this book, you will have gained knowledge to develop your own deep learning systems in a straightforward way and in an efficient way Contents 1: BUILDING DEEP LEARNING ENVIRONMENTS 2: TRAINING NN FOR PREDICTION USING REGRESSION 3: WORD REPRESENTATION USING WORD2VEC 4: BUILDING AN NLP PIPELINE FOR BUILDING CHATBOTS 5: SEQUENCE-TO-SEQUENCE MODELS FOR BUILDING CHATBOTS 6: GENERATIVE LANGUAGE MODEL FOR CONTENT CREATION 7: BUILDING SPEECH RECOGNITION WITH DEEPSPEECH2 8: HANDWRITTEN DIGITS CLASSIFICATION USING CONVNETS 9: OBJECT DETECTION USING OPENCV AND TENSORFLOW 10: BUILDING FACE RECOGNITION USING FACENET 11: AUTOMATED IMAGE CAPTIONING 12: POSE ESTIMATION ON 3D MODELS USING CONVNETS 13: IMAGE TRANSLATION USING GANS FOR STYLE TRANSFER
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值