plt.xlabel('Predicted label') def write_log(callback, names, logs, batch_no): for name, value in zip(names, logs): summary = tf.Summary() summary_value = summary.value.add() summary_value.simple_value = value summary_value.tag = name callback.writer.add_summary(summary, batch_no) callback.writer.flush() matplotlib.use("Qt5Agg") data = dataSet() train_data, train_alert_labels, train_rul_minutes, train_condition, test_data, test_alert_labels, test_rul_minutes, test_condition = data.get_all_cache_data( ) train_rul_minutes = train_rul_minutes.reshape(-1, 1) test_rul_minutes = test_rul_minutes.reshape(-1, 1) PREDICT = True TOTAL_EPOCH = 10000 SAVE_MODEL_PER_EPOCH = 50 BATCH_SIZE = 64 # 238 INITIAL_EPOCH = 79 TOTAL_BATCH_NUM = train_alert_labels.shape[0]
INPUT_SIZE = 64 OUTPUT_SIZE = 10 # check if GPU is feasible for training train_on_gpu = torch.cuda.is_available() if not train_on_gpu: print('Training on CPU ...') else: print('Training on GPU ...') criterionMSE = nn.MSELoss() criterionCE = nn.CrossEntropyLoss() train_set = dataSet('data\optdigits.tra', 8) train_loader = torch.utils.data.DataLoader(train_set, batch_size=32, shuffle=True) test_set = dataSet('data\optdigits.tes', 8) test_loader = torch.utils.data.DataLoader(test_set, batch_size=32, shuffle=True) def testing(model, criterion, load_path, data_loader, gpu, CE=True): ''' get accuracy of the model ''' # load the model model.load_state_dict(torch.load(load_path))
flatten_layer = NetTool.create_flatten_layer(inception9_concat) fc_input_size = flatten_layer.get_shape()[1:4].num_elements() fc_layer = NetTool.create_fc_layer(flatten_layer, [fc_input_size, 1000], 0.4) out_layer = NetTool.create_fc_layer(fc_layer, [1000, 3], 0.4, use_relu=False) pred_Y = tf.nn.softmax(out_layer, name='pred_Y') loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=pred_Y)) optimizer = tf.train.AdamOptimizer().minimize(loss) # learning_rate=0.0001 temp = tf.equal(tf.arg_max(pred_Y, 1), tf.arg_max(Y, 1)) accuracy = tf.reduce_mean(tf.cast(temp, tf.float32)) print('开始加载训练数据集') trainSet = dataset.dataSet(filePath, classes, way='txt', txtPath=txtPath) print('开始加载测试数据集') txtFilePath = '/Volumes/Seagate Backup Plus Drive/服务外包/picture/2019-03-05/body1' testSet = dataset.dataSet(txtFilePath, classes, way='image') print('数据集加载完成') saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(10001): batchX, batchY, _ = trainSet.next_batch(32) sess.run(optimizer, feed_dict={X: batchX, Y: batchY}) if i % 25 == 0: _, train_ac = sess.run([optimizer, accuracy], feed_dict={
def __init__(self, rootSet): self.rootNode = Node(rootSet.getLabel()) self.rootSet = rootSet def printTree(self): for pre, fill, node in RenderTree(self.rootNode): print('%s%s' % (pre, node.name)) def generateBranches(self, currentDataSet, parentNode): if currentDataSet.isLeaf(): # print('Is leaf', currentDataSet.getTuples()[0]) #class variable classVar = currentDataSet.getTuples()[0][0] Node(currentDataSet.getClassName() + '='+classVar, parent=parentNode) # Start splitting tree and recursing else: splitting_attribute_index = currentDataSet.getSplitAttributeByGain()['attribute_index'] split_data = currentDataSet.splitDataOnAttribute(splitting_attribute_index) for branch in split_data: nodeLabel = branch.getLabel() print('node label',nodeLabel) newNode = Node(nodeLabel, parent=parentNode) self.generateBranches(branch, newNode) def runTree(self): self.generateBranches(self.rootSet, self.rootNode) self.printTree() records = CSVReader('../Sources/Wine/red.csv').read([1,2,3,4,5,6,7]) # print(records) recordSet = dataSet(records['data'], records['attributes'], 'root') testTree = decisionTree(recordSet) testTree.runTree()