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的更多相关文章

  1. TensorFlow神经网络集成方案

    TensorFlow神经网络集成方案 创造张力流create_tensorflow_neuropod 将TensorFlow模型打包为neuropod包. create_tensorflow_neur ...

  2. 【Magenta 项目初探】手把手教你用Tensorflow神经网络创造音乐

    原文链接:http://www.cnblogs.com/learn-to-rock/p/5677458.html 偶然在网上看到了一个让我很感兴趣的项目 Magenta,用Tensorflow让神经网 ...

  3. Tensorflow 神经网络

    Tensorflow让神经网络自动创造音乐 前几天看到一个有意思的分享,大意是讲如何用Tensorflow教神经网络自动创造音乐.听起来好好玩有木有!作为一个Coldplay死忠粉,第一想法就是自动生 ...

  4. 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数

    TensorFlow运行方式.加载数据.定义超参数,构建网络,训练模型,评估模型.预测. 构造一个满足一元二次函数y=ax^2+b原始数据,构建最简单神经网络,包含输入层.隐藏层.输出层.Tensor ...

  5. TensorFlow 神经网络相关函数

    TensorFlow 激活函数 激活操作提供用于神经网络的不同类型的非线性.这些包括平滑的非线性(sigmoid,tanh,elu,softplus,和softsign),连续的,但不是到处可微函数( ...

  6. TensorFlow 神经网络教程

    TensorFlow 是一个用于机器学习应用程序的开源库.它是谷歌大脑的第二代系统,在取代了近源的 DistBelief 之后,被谷歌用于研究和生产应用.TensorFlow 提供了很多种语言接口,包 ...

  7. tensorflow神经网络拟合非线性函数与操作指南

    本实验通过建立一个含有两个隐含层的BP神经网络,拟合具有二次函数非线性关系的方程,并通过可视化展现学习到的拟合曲线,同时随机给定输入值,输出预测值,最后给出一些关键的提示. 源代码如下: # -*- ...

  8. TensorFlow神经网络中的激活函数

    激活函数是人工神经网络的一个极其重要的特征.它决定一个神经元是否应该被激活,激活代表神经元接收的信息与给定的信息有关. 激活函数对输入信息进行非线性变换. 然后将变换后的输出信息作为输入信息传给下一层 ...

  9. Tensorflow神经网络进行fiting function

    使用Tensorflow中的神经网络来拟合函数(y = x ^ 3 + 0.7) # -*- coding:utf-8 -*-import tensorflow as tf import numpy ...

  10. 封装TensorFlow神经网络

    为了参加今年的软件杯设计大赛,这几个月学习了很多新知识.现在大赛的第二轮作品优化已经提交,开始对这四个月所学知识做一些总结与记录. 用TensorFlow搭建神经网络.TensorFlow将神经网络的 ...

随机推荐

  1. 汇编 DOS的中断调用 INT 21H

    DOS系统功能调用 这个汇编指令是用于提供DOS系统功能调用. 它是由DOS提供的一组实现特殊功能的子程序供程序猿在编写自己的程序时调用,以减轻编程的工作量. 分两种,re=view"> ...

  2. 复制对象(一)copy和mutableCopy方法

    本文转载至 http://www.tuicool.com/articles/Fn6rMn CSDN博客原文  http://blog.csdn.net/u010962810/article/detai ...

  3. JQuery实现动态生成树形菜单

    jQuery实现动态生成树形菜单 有一个需求:菜单导航条需要依据不同的权限动态提取出来.计划是将功能模块与用户权限之间的关系保持到一个配置表中.所以功能菜单的话就需要动态提取出来再显示.借助jquer ...

  4. mongodb基础操作

    查询选择器>db.customers.find({age:{$lt:102}})查询age小于102的数据$lte表示小于或等于$gt表示大于$gte表示大于或等于>db.customer ...

  5. 多进程端口监听 How nginx processes a request Server names

    网络编程( 六):端口那些事儿 - 知乎专栏  https://zhuanlan.zhihu.com/p/20365900 不停服务reload.restart 多进程端口监听 我们都有一个计算机网络 ...

  6. 类 String、StringBuffer、StringBuilder

    类 String String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现.字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持 ...

  7. 使用mybatis向oracle数据库插入数据异常

    遇到了使用mybatis向oracle数据库插入数据异常的问题, 具体的报错如下:org.springframework.jdbc.UncategorizedSQLException: ### Err ...

  8. float 与 double

    一.原因 单精度和双精度数值类型最早出现在C语言中,在C语言中单精度类型称为浮点类型(Float),顾名思义是通过浮动小数点来实现数据的存储.这两个数据类型最早是为了科学计算而产生的, 他能够给科学计 ...

  9. android密码显示和隐藏

    if (showPwd){ //设置EditText文本为可见的 password.setTransformationMethod(HideReturnsTransformationMethod.ge ...

  10. Integrate-And-Fire Models(转)

    Integrate-And-Fire Models 基础知识 轴突:动作电位(电位差形成电流)=神经递质发放=脉冲产生树突或细胞体:神经递质的接受=产生内外膜电位差(电流产生)=接收脉冲脉冲编码:多采 ...