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. 次小生成树模板(poj1679)

    prim #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath&g ...

  2. 常见cout格式输出

    cout.setf(ios::fixed);//设置格式 cout.unsetf(ios::fixed);//取消格式 cout.setf(ios::scientific);//科学记数法 cout. ...

  3. C#动态删除控件

    foreach (Control var in panel.Controls) { if (var is Billet) { panel.Controls.Remove(var); var.Dispo ...

  4. 每个分片都是一个独立的Apache Lucene索引

    数据架构:索引+文档+文档类型+映射 [索引 文档 文档类型 映射] 索引index 对逻辑数据的逻辑存储:关系型数据库表.MongoDB集合.CouchDb数据库索引 index <---sh ...

  5. if控制器+循环控制器+计数器,控制接口分支

    但是我不想这么做,接口只想写一次,让循环控制器和if控制器去判断接口,执行我想要的分支.这里遇到了一个问题,if控制器通过什么去判断接下来的分支?我引入了一个计数器的概念.起始值为0,每次循环加1,将 ...

  6. github的代码上传成功但是不显示绿格子(一直拖....心痛的教训.....)

    损失了我特么的很多格子啊啊啊啊,必死强迫症啊!!!! 究其原因就是客户端绑定邮箱错误 本地 :git config user.email 显示邮箱是否和github中设定的一样? git config ...

  7. 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client

    一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...

  8. github 上 机器学习 的库推荐列表

    awesome-machine-learning: https://github.com/josephmisiti/awesome-machine-learning

  9. 剑指offer 面试29题

    面试29题: 题目:顺时针打印矩阵(同LeetCode 螺旋矩阵打印) 题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 ...

  10. admin 模块功能

    class CustomModelAdmin(admin.ModelAdmin): def has_module_permission(self, request): 是否会显示model def h ...