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

最新文章

  1. BZOJ4570: [Scoi2016]妖怪
  2. dump redo日志文件的信息
  3. (1) 第一章 Java体系结构介绍
  4. jQuery图片无缝滚动JS代码ul/li结构
  5. SKD
  6. 因為 Hypervisor 未執行,所以無法啟動虛擬機器
  7. Android ViewPager
  8. 联系电话正则表达式(jquery表单验证)
  9. libIconv.lib编码库的生成和使用
  10. 文件处理-Directory类 (C#)
  11. 04_过滤器Filter_04_Filter生命周期
  12. php中strlen和{}的效率对比
  13. sigaction 用法实例
  14. Java线程:什么是线程
  15. 轮询、长轮询与Web Socket的前端实现
  16. 小程序tab切换 点击左右滑动
  17. 未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0
  18. JS读取.properties文件的方法
  19. 外网访问内网Elasticsearch WEB
  20. python基础——字典

热门文章

  1. Oracle学习操作(3)
  2. Bootstrap-CSS:概况
  3. python unittest单元测试框架-3用例执行顺序、多级目录、装饰器、fixtures
  4. jquery对象和javascript对象的console.log结果
  5. [UE4]C 语言动态数组
  6. 17monipdb.dat
  7. PostMan 使用Interceptor 发送带cookie的请求一直loading
  8. Python 常用PEP8规范
  9. Linux系统运行级与启动机制剖析
  10. Chrome 鼠标左键-新标签打开