def session(dataset_path, train_path='', test_path=''): ''' 节点连接 :param dataset_path: 数据集路径 :param train_path: 训练集数据路径,默认为空 :param test_path: 测试集数据路径,默认为空 :return: None ''' #导入数据集 dataset = LoadFile(p=dataset_path) # dataset = guiyi(dataset) dataset = onehot(dataset) g = tf.Graph() with g.as_default(): with tf.name_scope('placeholder'): x_f = tf.placeholder(dtype=tf.float32, shape=[None, 4], name='x_f') x_l = tf.placeholder(dtype=tf.float32, shape=[None, 100], name='x_l') y = tf.placeholder(dtype=tf.float32, shape=[None, 3], name='y') learning_rate = tf.placeholder(dtype=tf.float32, name='lr') is_training = tf.placeholder(dtype=tf.bool, name='is_training') output = layers(x_f=x_f, x_l=x_l, is_training=is_training) with tf.name_scope('prediction'): # loss = -tf.reduce_mean(y * tf.log(output), name='loss') loss = tf.reduce_mean( tf.keras.losses.categorical_crossentropy(y_true=y, y_pred=output)) opt = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) acc = tf.reduce_mean(tf.cast( tf.equal(tf.keras.backend.argmax(output, axis=1), tf.keras.backend.argmax(y, axis=1)), tf.float32), name='pred') with tf.name_scope('etc'): init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=g) as sess: sess.run(init) #划分训练集和测试集 train_data, test_data = spliting(dataset, 3000) for i in range(6000): #6000 for data in input(dataset=train_data, batch_size=500): _ = sess.run(opt, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-3], y: data[:, -3:], learning_rate: 1e-2, is_training: False }) if i % 100 == 0: loss_ = sess.run(loss, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-3], y: data[:, -3:], is_training: False }) acc_1 = sess.run(acc, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-3], y: data[:, -3:], is_training: False }) if i % 100 == 0: acc_2 = sess.run(acc, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-3], y: test_data[:, -3:], is_training: False }) print('第%s轮训练集损失函数值为: %s 训练集准确率为: %s 测试集准确率为: %s' % (i, loss_, acc_1, acc_2)) tf.summary.FileWriter('log/first_graph', sess.graph)
def session(dataset_path, train_path='', test_path=''): ''' 节点连接 :param dataset_path: 数据集路径 :param train_path: 训练集数据路径,默认为空 :param test_path: 测试集数据路径,默认为空 :return: None ''' #导入数据集 dataset = LoadFile(p=dataset_path) g1 = tf.Graph() with g1.as_default(): with tf.name_scope('placeholder'): x_f = tf.placeholder(dtype=tf.float32, shape=[None, 4], name='x_f') x_l = tf.placeholder(dtype=tf.float32, shape=[None, 100], name='x_l') y = tf.placeholder(dtype=tf.float32, shape=[None, 1], name='y') learning_rate = tf.placeholder(dtype=tf.float32, name='lr') is_training = tf.placeholder(dtype=tf.bool, name='is_training') # output = res_regression(x_f=x_f, x_l=x_l, is_training=is_training) output = cnnlstm_regression(x_f=x_f, x_l=x_l, is_training=is_training) with tf.name_scope('prediction'): loss = tf.reduce_mean(tf.square(output - y)) opt = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) # opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) acc1 = acc_regression(Threshold=0.1, y_true=y, y_pred=output) acc2 = acc_regression(Threshold=0.16, y_true=y, y_pred=output) acc3 = acc_regression(Threshold=0.2, y_true=y, y_pred=output) acc4 = acc_regression(Threshold=0.3, y_true=y, y_pred=output) with tf.name_scope('etc'): init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=g1) as sess: sess.run(init) # 划分训练集和测试集 train_data, test_data = spliting(dataset, 3369) for i in range(60000): #20000 for data in input(dataset=train_data, batch_size=1000): _ = sess.run(opt, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], learning_rate: 1e-2, is_training: False }) if i % 100 == 0: loss_ = sess.run(loss, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) acc_1 = sess.run(acc1, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) acc_2 = sess.run(acc2, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) acc_3 = sess.run(acc3, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) acc_4 = sess.run(acc4, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) if i % 100 == 0: acc_5 = sess.run(acc1, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-1], y: test_data[:, -1][:, np.newaxis], is_training: False }) acc_6 = sess.run(acc2, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-1], y: test_data[:, -1][:, np.newaxis], is_training: False }) acc_7 = sess.run(acc3, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-1], y: test_data[:, -1][:, np.newaxis], is_training: False }) acc_8 = sess.run(acc4, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-1], y: test_data[:, -1][:, np.newaxis], is_training: False }) print( '第%s轮训练集损失函数值为: %s 训练集准确率为: %s:%s %s:%s %s:%s %s:%s 测试集准确率为: %s:%s %s:%s %s:%s %s:%s' % (i, loss_, 0.1, acc_1, 0.16, acc_2, 0.2, acc_3, 0.3, acc_4, 0.1, acc_5, 0.16, acc_6, 0.2, acc_7, 0.3, acc_8)) tf.summary.FileWriter('log/regression_cnnlstm', sess.graph) # 保存模型到文件当前脚本文件路径下的pb格式 saving_model = SaveImport_model( sess_ori=sess, file_suffix=r'/cnnlstm', ops=(output, x_f, x_l, is_training), usefulplaceholder_count=4, pb_file_path=r'/home/xiaosong/桌面/regression_cnnlstm') saving_model.save_pb()
def session(dataset_path, train_path='', test_path=''): ''' 节点连接 :param dataset_path: 数据集路径 :param train_path: 训练集数据路径,默认为空 :param test_path: 测试集数据路径,默认为空 :return: None ''' #导入数据集 dataset = LoadFile(p=dataset_path) g1 = tf.Graph() with g1.as_default(): with tf.name_scope('placeholder'): x_f = tf.placeholder(dtype=tf.float32, shape=[None, 4], name='x_f') x_l = tf.placeholder(dtype=tf.float32, shape=[None, 100], name='x_l') y = tf.placeholder(dtype=tf.float32, shape=[None, 1], name='y') learning_rate = tf.placeholder(dtype=tf.float32, name='lr') is_training = tf.placeholder(dtype=tf.bool, name='is_training') # output = res_regression(x_f=x_f, x_l=x_l, is_training=is_training) output = cnnlstm_regression(x_f=x_f, x_l=x_l, is_training=is_training) with tf.name_scope('prediction'): loss = tf.reduce_mean(tf.square(output - y)) opt = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) # opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) acc = acc_regression(Threshold=0.1, y_true=y, y_pred=output) with tf.name_scope('etc'): init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=g1) as sess: sess.run(init) # 划分训练集和测试集 train_data, test_data = spliting(dataset, 2269) for i in range(2000): #20000 for data in input(dataset=train_data, batch_size=1000): _ = sess.run(opt, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], learning_rate: 1e-2, is_training: False }) if i % 100 == 0: loss_ = sess.run(loss, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) acc_1 = sess.run(acc, feed_dict={ x_f: data[:, :4], x_l: data[:, 4:-1], y: data[:, -1][:, np.newaxis], is_training: False }) if i % 100 == 0: acc_2 = sess.run(acc, feed_dict={ x_f: test_data[:, :4], x_l: test_data[:, 4:-1], y: test_data[:, -1][:, np.newaxis], is_training: False }) print('第%s轮训练集损失函数值为: %s 训练集准确率为: %s 测试集准确率为: %s' % (i, loss_, acc_1, acc_2))
c.append(instance.getData()[(int(child)-1)][e[splitcase].index(min(e[splitcase]))]) children.append(c) index = 0 f1 = open(output,"w") line = "" for each in nodes: if each.split(" ")[0] == name: pass else: line = line + each +"\n" splitnames = "" for child in breakset(children): z = name+str(index) splitnames= splitnames+z+" " for each in child: z=z+" "+ each[0] line = line + z+"\n" index=index+1 line = line.strip("\n") f1.write(line) f1.close() print "Partition",name,"was replaced with partitions",splitnames,"using Feature ",e[splitcase].index(min(e[splitcase]))+1 if __name__ == '__main__': instance = id.input() main(instance)