def train(self, sess, X_train, Y_train, X_test, Y_test,num_epochs, num_count,logs): res = 0.0 summary_writer = tf.summary.FileWriter(logs+'/train',sess.graph) summary_writer1 = tf.summary.FileWriter(logs+'/test',sess.graph) sess.run(self.init_op) datagen = util.get_generator() # 模型持久化器 saver = tf.train.Saver() batch_size = 128 print('batch_size:', batch_size) # 开始迭代 for e in range(num_epochs): yieldData = Data(X_train,Y_train) print('Epoch', e) batches = 0 if e != 0 and (e %(num_epochs-1) == 0 or e%20==0): #每迭代一定次数保存下模型 saver.save(sess, logs+'/'+str(e)+'model') #使用原始数据进行迭代 for i in range(1): for batch_X,batch_Y in yieldData.get_next_batch(batch_size): _, lossval, scoreval = sess.run([self.train_op,self.loss, self.score], feed_dict={self.X: batch_X, self.keep_prob:self.keep_prob_train, self.y:batch_Y,self.is_training:True}) summary2, accuval, scoreval, lossval = sess.run([self.merged, self.accuracy_test, self.score,self.loss], feed_dict={self.X: X_test, self.keep_prob:1, self.y:Y_test,self.is_training:False}) print("Test accuracy:", accuval) print("Test loss:",lossval) return 0
def train(self, sess, X_train, Y_train, X_test, Y_test, num_epoch=1000): sess.run(self.init_op) # 随机生成器需要fit总体样本数据 datagen = util.get_generator() saver = tf.train.Saver() # 因为datagen的特殊需求(bacthsize需要能够整除训练集总个数,并且这里样本也少,直接全体当batchsize) split = 500 batch_size = int(len(X_train) / split) #取split使得尽量为200左右 if len( X_train ) % split != 0: #这里仅仅是因为图片生成那里要求batchsize需要能够和输入数据个数 整除 所以如此做以确保这一点 remove = len(X_train) % split X_train = X_train[:-1 * remove] #如果后续数据多了大可以不必进行图片生成或者图片数据很多却依然做图片生成时则batch_size和这个可能需要再调整 Y_train = Y_train[:-1 * remove] print('batch_size:', batch_size) for e in range(num_epoch): yieldData = Data(X_train, Y_train) print('Epoch', e) batches = 0 if e != 0 and e % 5 == 0: saver.save(sess, 'savedmodel/' + str(e) + 'model') for x_batch, y_batch in datagen.flow(X_train, Y_train, batch_size=batch_size, save_to_dir=None): #print(x_batch[0]) #print(y_batch[0]) if batches % 100 == 0: print('batch: ', batches) '''训练集''' accuval, scoreval1, scoreval2, scoreval3, scoreval4, scoreval5, scoreval6, scoreval7, lengthval, proval = sess.run( [ self.accuracy, self.score1, self.score2, self.score3, self.score4, self.score5, self.score6, self.score7, self.length, self.probabi ], feed_dict={ self.X: x_batch, self.keep_prob: 1, self.y: y_batch }) print("Train accuracy:", accuval) if batches == split: print('train') predictions = self.makeprint(scoreval1, scoreval2, scoreval3, scoreval4, scoreval5, scoreval6, scoreval7, lengthval, proval) a = sess.run(predictions) print(a.shape) print(a) print(y_batch) '''测试集''' yieldTest = Data(X_test, Y_test) tmp = 0 for b_test_x, b_test_y in yieldTest.get_next_batch(1000): accuval, scoreval1, scoreval2, scoreval3, scoreval4, scoreval5, scoreval6, scoreval7, lengthval, proval = sess.run( [ self.accuracy, self.score1, self.score2, self.score3, self.score4, self.score5, self.score6, self.score7, self.length, self.probabi ], feed_dict={ self.X: b_test_x, self.keep_prob: 1, self.y: b_test_y }) tmp = tmp + accuval if batches == split: print('test') predictions = self.makeprint( scoreval1, scoreval2, scoreval3, scoreval4, scoreval5, scoreval6, scoreval7, lengthval, proval) a = sess.run(predictions) #print(a.shape) #print(a) #print(b_test_y) print("Test accuracy:", tmp / 5) if batches == split: batches = 0 break if batches <= split: _, lossval = sess.run( [self.train_op, self.loss], feed_dict={ self.X: x_batch, self.keep_prob: self.keep_prob_train, self.y: y_batch }) if batches % 25 == 0: print("loss:", lossval) batches += 1 #使用原始数据进行迭代 for i in xrange(1): for batch_X, batch_Y in yieldData.get_next_batch(batch_size): lossval, scoreval = sess.run( [self.train_op, self.loss], feed_dict={ self.X: batch_X, self.keep_prob: self.keep_prob_train, self.y: batch_Y })
def train(self, sess, X_train, Y_train, X_test, Y_test, split, num_epoch, logs): sess.run(self.init_op) seq_len = self.score.shape[1].value print('seq_len:', seq_len) # 随机生成器需要fit总体样本数据 datagen = util.get_generator() saver = tf.train.Saver() # 因为datagen的特殊需求(bacthsize需要能够整除训练集总个数,并且这里样本也少,直接全体当batchsize) batch_size = int(len(X_train) / split) #取split使得尽量为200左右 if len( X_train ) % split != 0: #这里仅仅是因为图片生成那里要求batchsize需要能够和输入数据个数 整除 所以如此做以确保这一点 remove = len(X_train) % split X_train = X_train[:-1 * remove] #如果后续数据多了大可以不必进行图片生成或者图片数据很多却依然做图片生成时则batch_size和这个可能需要再调整 Y_train = Y_train[:-1 * remove] print('batch_size:', batch_size) tacc = 0 for e in range(num_epoch): indices = list( range(len(Y_train)) ) # indices = the number of images in the source data set np.random.shuffle(indices) X_train = X_train[indices] Y_train = Y_train[indices] yieldData = Data(X_train, Y_train) #更新lr k = sess.run([self.global_epoch_assign_op], feed_dict={self.global_epoch_input: e}) #lrnow = sess.run(self.lr) #lrnow = self.lr print('Epoch---------', e) batches = 0 if e != 0 and e % 50 == 0: saver.save(sess, logs + '/' + str(e) + '-' + str(tacc) + 'model') Y_test_sparse = self.sparse_tuple_from(Y_test) for x_batch, y_batch in datagen.flow(X_train, Y_train, batch_size=batch_size, save_to_dir=None): y_sparse = self.sparse_tuple_from(y_batch) if batches % (split - 1) == 0 and batches != 0: '''训练集''' denseval, costval = sess.run( [self.dense_decoded, self.cost], feed_dict={ self.X: x_batch, self.keep_prob: 1, self.y: y_sparse, self.sequence_length: np.ones(len(y_batch)) * seq_len, self.is_training: False }) print("Train cost:", costval) print('denseval', denseval[0:10]) print('train_acc', self.calcacc(y_batch, denseval)) '''测试集''' denseval, costval = sess.run( [self.dense_decoded, self.cost], feed_dict={ self.X: X_test, self.keep_prob: 1, self.y: Y_test_sparse, self.sequence_length: np.ones(len(Y_test)) * seq_len, self.is_training: False }) print("Test cost:", costval) tacc = self.calcacc(Y_test, denseval) print('test_acc', tacc) if batches <= split: _, costval = sess.run( [self.train_op, self.cost], feed_dict={ self.X: x_batch, self.keep_prob: 0.5, self.y: y_sparse, self.sequence_length: np.ones(len(y_batch)) * seq_len, self.is_training: True }) if batches % (split - 1) == 0: print("cost:", costval) batches += 1 if batches == split + 1: break #使用原始数据进行迭代 for i in range(1): for batch_X, batch_Y in yieldData.get_next_batch(batch_size): y_sparse = self.sparse_tuple_from(batch_Y) _, lossval = sess.run( [self.train_op, self.loss], feed_dict={ self.X: batch_X, self.keep_prob: 0.5, self.y: y_sparse, self.sequence_length: np.ones(len(batch_Y)) * seq_len, self.is_training: True })