tflearn 实现DNN 全连接
https://github.com/tflearn/tflearn/blob/master/examples/others/recommender_wide_and_deep.py
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tflearn
from tflearn.data_utils import to_categorical
#matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray' # for auto-reloading extenrnal modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
#%load_ext autoreload
#%autoreload 2 # 模块1-1 画图 展示样本形式
np.random.seed(0)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D))
y = np.zeros(N*K, dtype='uint8')
for j in range(K):
ix = range(N*j,N*(j+1))
r = np.linspace(0.0,1,N) # radius
t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
y[ix] = j
fig = plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim([-1,1])
plt.ylim([-1,1])
#fig.savefig('spiral_raw.png') #模块1-2 训练 两层
import tensorflow as tf
import tflearn
from tflearn.data_utils import to_categorical with tf.Graph().as_default():
net = tflearn.input_data([None, 2])
net = tflearn.fully_connected(net, 100, activation='relu', weights_init='normal',
regularizer='L2', weight_decay=0.001)
net = tflearn.fully_connected(net, 3, activation='softmax')
sgd = tflearn.SGD(learning_rate=1.0, lr_decay=0.96, decay_step=500)
net = tflearn.regression(net, optimizer=sgd, loss='categorical_crossentropy') # gd=tf.train.GradientDescentOptimizer(learning_rate=1.0)
# net = tflearn.regression(net, optimizer=gd, loss='categorical_crossentropy') Y = to_categorical(y, 3)
model = tflearn.DNN(net)
model.fit(X, Y, show_metric=True, batch_size=len(X), n_epoch=100, snapshot_epoch=False)
#print model.predict(X)
Z = np.argmax(model.predict(X))
#print Z # 模块1-3 画图展示效果 plot the resulting classifier
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = np.argmax(model.predict(np.c_[xx.ravel(), yy.ravel()]), axis=1)
Z = Z.reshape(xx.shape)
fig = plt.figure()
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
#fig.savefig('spiral_net.png')
print("Accuracy: {}%".format(100 * np.mean(y == np.argmax(model.predict(X), axis=1))))
代码2 自己的数据
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tflearn
from tflearn.data_utils import to_categorical
import os label=[]
feature=[]
f_tain=open("smp_pub_50d",'r')
for line in f_tain.readlines()[0:50000]:
samp=eval(line)
gender=int(samp[2])
feature1=samp[5:]
feature.append(feature1)
label.append(gender)
f_tain.close() X=np.array(feature)
Y=np.array(label) #模块1-2 训练 两层
import tensorflow as tf
import tflearn
from tflearn.data_utils import to_categorical with tf.Graph().as_default():
net = tflearn.input_data([None, 50])
net = tflearn.fully_connected(net, 100, activation='relu', weights_init='normal',
regularizer='L2', weight_decay=0.001)
net = tflearn.fully_connected(net, 2, activation='softmax')
sgd = tflearn.SGD(learning_rate=1.0, lr_decay=0.96, decay_step=500)
net = tflearn.regression(net, optimizer=sgd, loss='categorical_crossentropy') # gd=tf.train.GradientDescentOptimizer(learning_rate=1.0)
# net = tflearn.regression(net, optimizer=gd, loss='categorical_crossentropy') Y = to_categorical(Y, 2)
model = tflearn.DNN(net)
model.fit(X, Y, validation_set=0.3,show_metric=True, batch_size=20000, n_epoch=100, snapshot_epoch=False)
#print model.predict(X)
Z = np.argmax(model.predict(X))
#print Z
tflearn 实现DNN 全连接的更多相关文章
- TensorFlow之DNN(二):全连接神经网络的加速技巧(Xavier初始化、Adam、Batch Norm、学习率衰减与梯度截断)
在上一篇博客<TensorFlow之DNN(一):构建“裸机版”全连接神经网络>中,我整理了一个用TensorFlow实现的简单全连接神经网络模型,没有运用加速技巧(小批量梯度下降不算哦) ...
- TensorFlow之DNN(一):构建“裸机版”全连接神经网络
博客断更了一周,干啥去了?想做个聊天机器人出来,去看教程了,然后大受打击,哭着回来补TensorFlow和自然语言处理的基础了.本来如意算盘打得挺响,作为一个初学者,直接看项目(不是指MINIST手写 ...
- PyTorch全连接ReLU网络
PyTorch全连接ReLU网络 1.PyTorch的核心是两个主要特征: 一个n维张量,类似于numpy,但可以在GPU上运行 搭建和训练神经网络时的自动微分/求导机制 本文将使用全连接的ReLU网 ...
- Linq连接查询之左连接、右连接、内连接、全连接、交叉连接、Union合并、Concat连接、Intersect相交、Except与非查询
内连接查询 内连接与SqL中inner join一样,即找出两个序列的交集 Model1Container model = new Model1Container(); //内连接 var query ...
- SQL多表查询:内连接、外连接(左连接、右连接)、全连接、交叉连接
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAADCCAIAAADrUpiXAAAGYklEQVR4nO3dQXqjuAJFYa1LC9J6tB
- SQL Server之内连接 左连接 右连接 全连接 交叉连接
SELECT * FROM Table_A GO SELECT * FROM Table_B GO --内连接 SELECT a.*, b.* FROM Table_A a JOIN Table_B ...
- SQL 四种连接:内连接、左外连接、右外连接、全连接--转载
原文:http://zwdsmileface.iteye.com/blog/2191730 个人理解 内连接(INNER JOIN)(典型的连接运算,使用像 = 或 <> ...
- caffe之(四)全连接层
在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层.卷积操作层.pooling层.非线性变换层.内积运算层.归一化层.损失计算层等:本篇主要 ...
- 全连接的BP神经网络
<全连接的BP神经网络> 本文主要描述全连接的BP神经网络的前向传播和误差反向传播,所有的符号都用Ng的Machine learning的习惯.下图给出了某个全连接的神经网络图. 1前向传 ...
随机推荐
- CentOS6.5 搭建在线yum源
CentOS6.5 搭建在线yum源 发布时间: 2017-04-21 浏览次数: 611 下载次数: 1 问题描述 尽管有很多的免费镜像提供yum源服务,但是还是有必要建立自己的yum服务器 ...
- bzoj3326: [Scoi2013]数数
Description Fish 是一条生活在海里的鱼,有一天他很无聊,就开始数数玩. 他数数玩的具体规则是: 1. 确定数数的进制B 2. 确定一个数数的区间[L, R] 3. 对于[L, R] 间 ...
- win2003 server的域用户加入本地管理员组
很久没有搞windows的域管理,今天碰到一个问题,一直找不到解决方法,一直在网络找资料,直到刚刚才找到方法,方法如下: C:\>net localgroup administrators li ...
- Linux中make, make install命令分别是什么
用于linux源码安装软件,一般下载源码包得到文件:xxxx.tgz====================================1.解包软件 tar zxf xxxx.tgz======= ...
- pandas的read_csv函数
pd.read_csv(filepath_or_buffer, sep=',', delimiter=None, header='infer', names=None, index_col=None, ...
- FDD vs TDD
双工方式 FDD vs TDD 频分双工(FDD) 收发信各占用一个频率. 优点是收.发信号同时进行,时延小,技术成熟,缺点是设备成本高. 时分双工(TDD) 收发信使用同一个频率,但使用不同 ...
- 第3课 进化后的 const分析
1. C语言中的const (1)const修饰的变量是只读的,使得变量具有只读属性,但本质还是变量.所以不是真正的常量,它只是告诉编译器该变量不能出现在赋值符号的左边. (2)const修饰的局部 ...
- MySQL ALTER讲解
当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...
- spark streaming 概述
批处理 & 流处理 像这个是批处理 像这样就是流处理 为什么需要流处理--更多场景需要 Spark Core & RDD 本质上是离线运算 Spark Streaming是什么(分布式 ...
- 基于Linux的Samba开源共享解决方案测试(五)
对于客户端的网络监控如图: 双NAS网关50Mb码率视音频文件的稳定写测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 16个稳 ...