tensorFlow 神经网络2
learnrate 太大容易跑飞,设置激活函数 可以一定程度上增加learnrate,不跑飞
self.saver = tf.train.Saver() 和 self.init_variable = tf.global_variables_initializer() self.sess.run(self.init_variable)一定要写在所有变量的最后
GradientDescentOptimizer优化器可以继续训练 AdadeltaOptimizer优化器的训练结果,反之则不行
# -*- coding: utf-8 -*- import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops is_continue = True
path_prefix = "../../../" class neural_network:
def __init__(self):
ops.reset_default_graph()
self.model_path = path_prefix + "data/model/model"
self.batch_size = 2000
self.learning_rate = 0.5
self.input_size = 15193
self.output_size = 1
self.sess = tf.InteractiveSession()
self.input = tf.placeholder(tf.float32, [None, self.input_size]) # 15193个维度
self.label = tf.placeholder(tf.float32, [None, self.output_size])
self.add_layers(self.input_size, self.output_size, tf.nn.relu)
self.loss = tf.reduce_mean(tf.reduce_sum(tf.square(self.output - self.label),
reduction_indices=[1])) self.optimizer = tf.train.AdadeltaOptimizer(learning_rate=self.learning_rate).minimize(loss=self.loss)
self.saver = tf.train.Saver()
if is_continue:
self.saver.restore(self.sess, self.model_path)
else:
self.init_variable = tf.global_variables_initializer()
self.sess.run(self.init_variable)
self.data_generator = self.data_generator_f(
path_prefix + ("data/trainData_local"),
50000) def add_layers(self, input_size, output_size, activation_function=None):
layer1_size = 200
weight1 = tf.Variable(tf.zeros([input_size, layer1_size]) + 0.1)
biaes1 = tf.Variable(tf.zeros([1, layer1_size]) + 0.1)
output_layer1 = tf.matmul(self.input, weight1) + biaes1 # 200 50
input_layer2 = output_layer1
if activation_function is not None:
input_layer2 = activation_function(output_layer1) layer2_size = 50
weight2 = tf.Variable(tf.zeros([layer1_size, layer2_size]) + 0.01)
biaes2 = tf.Variable(tf.zeros([1, layer2_size]) + 0.1)
output_layer2 = tf.matmul(input_layer2, weight2) + biaes2 # 200 10
input_layer3 = output_layer2
if activation_function is not None:
input_layer3 = activation_function(output_layer2) weight3 = tf.Variable(tf.zeros([layer2_size, self.output_size]) + 0.01)
biaes3 = tf.Variable(tf.zeros([1, self.output_size]) + 0.1)
output_layer3 = tf.matmul(input_layer3, weight3) + biaes3 if activation_function is not None:
self.output = activation_function(output_layer3)
else:
self.output = output_layer3 return self.output def train(self):
for i in range(0, 20000000):
labels, feature = self.data_generator.next()
self.sess.run(self.optimizer, feed_dict={
self.input: feature,
self.label: np.asarray(labels).reshape(self.batch_size, 1)
}) if (i % 10) == 0:
print(self.sess.run(self.loss, feed_dict={
self.input: feature,
self.label: np.asarray(labels).reshape(self.batch_size, 1)
}))
if (i % 100) == 0:
self.saver.save(self.sess, self.model_path)
print("save complete... ...") def predict(self, feature):
print "--start predict--"
return self.sess.run(self.output, feed_dict={
self.input: feature
}) def data_generator_f(self, input_dir, count):
input_filename = input_dir
while count > 0:
labels = np.zeros(self.batch_size)
rets = np.empty(shape=[self.batch_size, 15193])
i = 0
for line in open(input_filename, "r"):
# print "trainData", line
data = line.split(" ")
label = int(float(data[0]))
ids = []
values = []
for fea in data[1:]:
id, value = fea.split(":")
ids.append(int(id))
values.append(float(value))
ret = np.zeros([1, 15193])
for (index, d) in zip(ids, values):
ret[0][index] = d
labels[i] = int(label)
rets[i] = ret
i += 1
if i > self.batch_size - 1:
i = 0
yield labels, rets
print("train count:", count)
count -= 1 if __name__ == '__main__':
print('start run... ...')
trainer = neural_network()
trainer.train()
"""
trainer.batch_size = 100
data = trainer.data_generator_f("/home/panteng/下载/20171231.libsvm", 3)
threshold = 0.55 for i in range(0, 100):
labels, features = data.next()
pre = trainer.predict(feature=features)
print ("P:", np.sum(np.array([(labels > threshold)])), " N:", np.sum(np.array([(labels < threshold)]))) print("accuracy:", np.sum(
(np.array([(labels > threshold)]).reshape(trainer.batch_size, 1) == (pre > threshold)).reshape(1, trainer.batch_size)
) * 1.0 / trainer.batch_size)
print("precision:",
np.sum((np.array([(labels > threshold)]).reshape(trainer.batch_size, 1) == (pre > threshold)) & (
pre > threshold)) * 1.0 / np.sum(np.array([(pre > threshold)]))
)
print("recall:",
np.sum((np.array([(labels > threshold)]).reshape(trainer.batch_size, 1) == (pre > threshold)) & (
pre > threshold)) * 1.0 / np.sum(np.array([(labels > threshold)]))
)
"""
2:
# coding:utf-8
import tensorflow as tf
import numpy as np tf.app.flags.DEFINE_string('train_dir', '')
tf.app.flags.DEFINE_integer('capacity', 30000, 'indicates training epoch')
FLAGS = tf.app.flags.FLAGS features = tf.placeholder(tf.float32, shape=[None, 151])
label = tf.placeholder(tf.float32, shape=[None, 1]) def read_csv():
file_queue = tf.train.string_input_producer(tf.train.match_filenames_once(FLAGS.train_dir + "*"))
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(file_queue)
data = tf.decode_csv(value, record_defaults=[[0.0] for _ in range(152)], field_delim="\t")
return tf.train.shuffle_batch(data, batch_size=1024, capacity=FLAGS.capacity,
min_after_dequeue=FLAGS.capacity / 2, num_threads=8) def build_net():
deep_layer1 = tf.layers.dense(inputs=features, units=80, activation=tf.nn.relu)
deep_layer2 = tf.layers.dense(inputs=deep_layer1, units=32, activation=tf.nn.relu)
deep_layer3 = tf.layers.dense(inputs=deep_layer2, units=8, activation=tf.nn.relu)
deep_layer4 = tf.layers.dense(inputs=deep_layer3, units=5, activation=tf.nn.relu)
wide_layer1 = tf.layers.dense(inputs=features, units=1, activation=tf.nn.sigmoid) wide_deep_layer = tf.concat([wide_layer1, deep_layer4], axis=1)
return tf.layers.dense(inputs=wide_deep_layer, units=1, activation=tf.nn.sigmoid) with tf.Session() as sess:
output = build_net()
loss = tf.reduce_mean(tf.reduce_sum(tf.squared_difference(output, label)))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) train_data = read_csv()
init = (tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
data_set = sess.run(train_data)
label_in = np.array(data_set[0], dtype='int').ravel()
feature_in = np.transpose(data_set[1:])
for i in range(100):
sess.run(optimizer,
feed_dict={features: np.array(feature_in).reshape([-1, 151]),
label: np.array(label_in).reshape([-1, 1])})
print sess.run(loss,
feed_dict={features: np.array(feature_in).reshape([-1, 151]),
label: np.array(label_in).reshape([-1, 1])})
tensorFlow 神经网络2的更多相关文章
- TensorFlow神经网络集成方案
TensorFlow神经网络集成方案 创造张力流create_tensorflow_neuropod 将TensorFlow模型打包为neuropod包. create_tensorflow_neur ...
- 【Magenta 项目初探】手把手教你用Tensorflow神经网络创造音乐
原文链接:http://www.cnblogs.com/learn-to-rock/p/5677458.html 偶然在网上看到了一个让我很感兴趣的项目 Magenta,用Tensorflow让神经网 ...
- Tensorflow 神经网络
Tensorflow让神经网络自动创造音乐 前几天看到一个有意思的分享,大意是讲如何用Tensorflow教神经网络自动创造音乐.听起来好好玩有木有!作为一个Coldplay死忠粉,第一想法就是自动生 ...
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
TensorFlow运行方式.加载数据.定义超参数,构建网络,训练模型,评估模型.预测. 构造一个满足一元二次函数y=ax^2+b原始数据,构建最简单神经网络,包含输入层.隐藏层.输出层.Tensor ...
- TensorFlow 神经网络相关函数
TensorFlow 激活函数 激活操作提供用于神经网络的不同类型的非线性.这些包括平滑的非线性(sigmoid,tanh,elu,softplus,和softsign),连续的,但不是到处可微函数( ...
- TensorFlow 神经网络教程
TensorFlow 是一个用于机器学习应用程序的开源库.它是谷歌大脑的第二代系统,在取代了近源的 DistBelief 之后,被谷歌用于研究和生产应用.TensorFlow 提供了很多种语言接口,包 ...
- tensorflow神经网络拟合非线性函数与操作指南
本实验通过建立一个含有两个隐含层的BP神经网络,拟合具有二次函数非线性关系的方程,并通过可视化展现学习到的拟合曲线,同时随机给定输入值,输出预测值,最后给出一些关键的提示. 源代码如下: # -*- ...
- TensorFlow神经网络中的激活函数
激活函数是人工神经网络的一个极其重要的特征.它决定一个神经元是否应该被激活,激活代表神经元接收的信息与给定的信息有关. 激活函数对输入信息进行非线性变换. 然后将变换后的输出信息作为输入信息传给下一层 ...
- Tensorflow神经网络进行fiting function
使用Tensorflow中的神经网络来拟合函数(y = x ^ 3 + 0.7) # -*- coding:utf-8 -*-import tensorflow as tf import numpy ...
- 封装TensorFlow神经网络
为了参加今年的软件杯设计大赛,这几个月学习了很多新知识.现在大赛的第二轮作品优化已经提交,开始对这四个月所学知识做一些总结与记录. 用TensorFlow搭建神经网络.TensorFlow将神经网络的 ...
随机推荐
- Android无线测试之—UiAutomator UiObject API介绍三
拖拽与滑动 一.拖拽与滑动的示意图 二.拖拽与滑动相关的API 返回值 API 描述 boolean dragTo(UiObject destObj, int setps) 拖拽对象到另一个对象位置上 ...
- 1624 取余最长路(set)
1624 取余最长路 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱 ...
- Oracle 常用
1. 级联查询 在ORACLE 数据库中有一种方法可以实现级联查询select * //要查询的字段from table //具有子接点ID与父 ...
- <2013 12 01> 一篇很好的关于windows编程的入门指导(2013年末写的,比较前沿)
我之前做了不少嵌入式开发,从单片机到ARM到RTOS到Linux等等,可以说走的是电气工程师的路线,对编程也是实用性的,跟计算机学院的科班套路不同.最近同学做一个windowsCE的项目请我帮忙,之前 ...
- 在VM虚拟机中安装Centos7操作系统(三)
首先我们要下载 Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...
- 地址栏输入url按回车之后发生了什么
地址栏输入url按回车之后发生了什么? 1.我们在浏览器中输入网址 2.浏览器到DNS查找域名对应的IP地址 3. 浏览器打开TCP连接(默认端口为80),向该IP的服务器发送一条HTTP请求,如果浏 ...
- JS添加标签
<script> function show(){ $('.add').unbind(); $('.low ...
- PhpStorm2017.1版激活方法、汉化方法以及界面配置
本教程仅对2017.1版有效!!!!!! PhpStorm激活和汉化文件下载网址(提取密码:62cg) PhpStorm的介绍 PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率, ...
- 给俺的 CSDN 博客加背景音乐 - 高大尚的《心经》背景音乐
给俺的 CSDN 博客加背景音乐 - 高大尚的<心经>背景音乐 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途- ...
- kettle连接sqlserver
1.Sql Server的 数据库链接 Sql server链接有两类,MS SQL SERVER 和 MS SQL SERVER(NATIVE),这两个有什么区别呢,且看下面. 第一类,MS SQL ...