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=" ") 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])})