用Keras搭建神经网络 简单模版(一)——Regressor 回归
首先需要下载Keras,可以看到我用的是TensorFlow 的backend

自己构建虚拟数据,x是-1到1之间的数,y为0.5*x+2,可视化出来
# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) #for reproducibility再现性
from keras.models import Sequential#按层
from keras.layers import Dense#全连接层
import matplotlib.pyplot as plt #creat some data
X = np.linspace(-1,1,200) #200个x,-1到1之间
np.random.shuffle(X) #randomize the data
Y = 0.5*X +2 + np.random.normal(0,0.05,(200,))
#plot data
plt.scatter(X,Y)
plt.show
X_train,Y_train= X[:160],Y[:160]#160个
X_test,Y_test = X[160:], Y[160:]#40个

接下来搭建1层神经网络
#build a neural network from the 1st layer to the the last layer
model = Sequential()
model.add(Dense(output_dim=1,input_dim=1))#加一层 #choose loss function and optimizing method
#mse方差
model.compile(loss='mse',optimizer='sgd')
最后,训练测试,输出结果
#training
print("Training~~~~~~~~")
for step in range(301):
cost = model.train_on_batch(X_train,Y_train)#一批一批的数据,这里一批选择全部数据
if step %100==0:
print('train cost:',cost) #test
print('\nTesting~~~~~~~~')
cost = model.evaluate(X_test,Y_test,batch_size=40)
print('test cost:',cost)
W,b = model.layers[0].get_weights()
print('Weights=',W,'\nbiases=',b) #plotting the prediction
Y_pred =model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pred)
输出结果


用Keras搭建神经网络 简单模版(一)——Regressor 回归的更多相关文章
- 用Keras搭建神经网络 简单模版(六)——Autoencoder 自编码
import numpy as np np.random.seed(1337) from keras.datasets import mnist from keras.models import Mo ...
- 用Keras搭建神经网络 简单模版(二)——Classifier分类(手写数字识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
- 用Keras搭建神经网络 简单模版(五)——RNN LSTM Regressor 循环神经网络
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) import matplotlib.pyplot as plt from ...
- 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
- 用Keras搭建神经网络 简单模版(四)—— RNN Classifier 循环神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) from keras.datasets import mnist fro ...
- keras搭建神经网络快速入门笔记
之前学习了tensorflow2.0的小伙伴可能会遇到一些问题,就是在读论文中的代码和一些实战项目往往使用keras+tensorflow1.0搭建, 所以本次和大家一起分享keras如何搭建神经网络 ...
- Keras(三)backend 兼容 Regressor 回归 Classifier 分类 原理及实例
backend 兼容 backend,即基于什么来做运算 Keras 可以基于两个Backend,一个是 Theano,一个是 Tensorflow 查看当前backend import keras ...
- 深度学习实践系列(3)- 使用Keras搭建notMNIST的神经网络
前期回顾: 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型 深度学习实践系列(2)- 搭建notMNIST的深度神经网络 在第二篇系列中,我们使用了TensorFlow搭建了第一个深度 ...
- 对比学习用 Keras 搭建 CNN RNN 等常用神经网络
Keras 是一个兼容 Theano 和 Tensorflow 的神经网络高级包, 用他来组件一个神经网络更加快速, 几条语句就搞定了. 而且广泛的兼容性能使 Keras 在 Windows 和 Ma ...
随机推荐
- Nodejs之mssql模块的封装
在nodejs中,mssql模块支持sqlserver数据库操作.今天将mssql模块的某些功能封装为一个类,方便以后调用.封装的功能有执行存储过程,执行查询语句操作等.如果本篇文章对大家有帮助,那就 ...
- DevExpress v18.1新版亮点——WinForms篇(七)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v18.1 的新功能,快来下载试用新版本! ...
- 怎样更新PE内的工具
准备工作:1. UltraISO - 下载:http://yunpan.cn/Q5XuHwG4ydv85 (访问密码:6263) 2. 7-zip - 下载:http://yunpan.c ...
- spring数据源
包含三部分内容 1.spring jdbc 2. spring datasource 3.spring Connection pooling 完整的项目请往百度云盘下载: https://pan.ba ...
- Texas Instruments matrix-gui-2.0 hacking -- app_description.php
<?php /* * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistrib ...
- valgrind- 内存泄漏-how to install and use
1.how to install my host computer is ARM, U need to Attention yours... valgrind下载: http://valgrind.o ...
- Linux博客系统服务器搭建
linux(CentOS)服务器搭建 前言 拿到购买的服务器信息后,会给出一个服务器的账号的密码,看你自己设置,账号一般为root. 拿到后,可在阿里云官网登录进入服务器.然后就可以进行一下的流程从而 ...
- 2N7002
就相当于一个开关,当G>S的一定电压就导通,若没有达到就没有导通. 并且注意的是D脚不能直接接电压,中间要接一个电阻. 这用是G输入,S接地,D输出. 跟三极管差不多.分N和P
- [LeetCode&Python] Problem 917. Reverse Only Letters
Given a string S, return the "reversed" string where all characters that are not a letter ...
- python去掉字符串'\xa0'
AssertionError: '5\xa0e\xa0*\xa0*\xa0*\xa05' != '5e***5'mystr = '5\xa0e\xa0*\xa0*\xa0*\xa05'mystr = ...