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前向传 ...
随机推荐
- C#设计模式之:抽象工厂模式与反射
抽象工厂模式[实例]:定义一个用于创建对象的接口,让子类决定实例化哪一个类 UML 代码class User{ private int _id; public int Id { get = ...
- 4G的添加
(ZTE 中兴 ME3760_V2 ) 1. 复制myLte文件到相关目录,设置权限为755 2.修改openwrt1407/package/base-files/files/etc/rc.local ...
- java 网络编程UDP
获得主机名 和 ip 的操作 简单示例 发送 接收 发送:键盘录入获得数据 接收:接收端持续接收数据 配合多线程可以完成一个聊天的功能.
- orace学习操作(4)
Orace游标: 一.游标简介: 使用游标,我们可以对具体操作数据,比如查询的结果,对行,列数据进行更加细致的处理.以及对其他DML操作进行判断等操作: 二.显示游标: 1.静态的指定变量类型: SQ ...
- [转]修改DLL
部分内容来自:http://blog.csdn.net/csdncshuan/article/details/51477705 为没有源码的DLL文件添加强名称 如果项目中引用了其他没有源码的dll文 ...
- [转]NSIS:安装、卸载时检查程序是否正在运行
原文链接 http://www.flighty.cn/html/bushu/20110402_115.html 如果我们要安装或升级的程序正在运行,文件肯定会替换失败,以下代码可以提示用户结束正在运行 ...
- 查看iPhoneCPU、内存占用
使用Xcode可以查看iPhone cpu 内存 disk 网络占用读取 Xcode-Opem Developer Tool-Instruments, 在打开的窗口里选择Activity Minito ...
- 特征选择:方差选择法、卡方检验、互信息法、递归特征消除、L1范数、树模型
转载:https://www.cnblogs.com/jasonfreak/p/5448385.html 特征选择主要从两个方面入手: 特征是否发散:特征发散说明特征的方差大,能够根据取值的差异化度量 ...
- oracle11g的dmp文件导入oracle10g时报错:头部验证失败
因为本机安装的10g版本的Oracle,需要导入一个11g版本导出的dmp文件,Oracle数据库版本之间存在兼容的问题,低版本的库不能导入高版本的dmp文件,在CMD中导入dmp文件总是出现如下错误 ...
- extract 用法说明
PHP extract() 函数从数组中把变量导入到当前的符号表中 定义和用法 PHP extract() 函数从数组中把变量导入到当前的符号表中. 对于数组中的每个元素,键名用于变量名,键值用于变量 ...