def train_cnn(self):
    y_predict = self.model()
    print('>>> input batch predict shape: {}'.format(y_predict.shape))
    print('>>> End model test')
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_predict, labels=self.Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)
    predict = tf.reshape(y_predict, [(- 1), self.max_captcha, self.char_set_len])
    max_idx_p = tf.argmax(predict, 2)
    max_idx_l = tf.argmax(tf.reshape(self.Y, [(- 1), self.max_captcha, self.char_set_len]), 2)
    correct_pred = tf.equal(max_idx_p, max_idx_l)
    accuracy = tf.reduce_mean(tf.reduce_min(tf.cast(correct_pred, tf.float32), axis=1))
    saver = tf.train.Saver()
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        if os.path.exists(self.model_save_dir):
            try:
                saver.restore(sess, self.model_save_dir)
            except NotFoundError:
                print('model文件夹为空，将创建新模型')
        else:
            pass
        step = 1
        for i in range(3000):
            (batch_x, batch_y) = self.get_batch(i, size=128)
            (_, cost_) = sess.run([optimizer, cost], feed_dict={self.X: batch_x, self.Y: batch_y, self.keep_prob: 0.75})
            if ((step % 10) == 0):
                (batch_x_test, batch_y_test) = self.get_batch(i, size=100)
                acc = sess.run(accuracy, feed_dict={self.X: batch_x_test, self.Y: batch_y_test, self.keep_prob: 1.0})
                print('第{}次训练 >>> 准确率为 {} >>> loss {}'.format(step, acc, cost_))
                if (acc > 0.99):
                    saver.save(sess, self.model_save_dir)
                    break
            if ((i % 500) == 0):
                saver.save(sess, self.model_save_dir)
            step += 1
        saver.save(sess, self.model_save_dir)
def train_cnn(self):
    y_predict = self.model()
    print('>>> input batch predict shape: {}'.format(y_predict.shape))
    print('>>> End model test')
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_predict, labels=self.Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)
    predict = tf.reshape(y_predict, [(- 1), self.max_captcha, self.char_set_len])
    max_idx_p = tf.argmax(predict, 2)
    max_idx_l = tf.argmax(tf.reshape(self.Y, [(- 1), self.max_captcha, self.char_set_len]), 2)
    correct_pred = tf.equal(max_idx_p, max_idx_l)
    accuracy = tf.reduce_mean(tf.reduce_min(tf.cast(correct_pred, tf.float32), axis=1))
    saver = tf.train.Saver()
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        if os.path.exists(self.model_save_dir):
            try:
                saver.restore(sess, self.model_save_dir)
            except NotFoundError:
                print('model文件夹为空，将创建新模型')
        else:
            pass
        step = 1
        for i in range(3000):
            (batch_x, batch_y) = self.get_batch(i, size=128)
            (_, cost_) = sess.run([optimizer, cost], feed_dict={self.X: batch_x, self.Y: batch_y, self.keep_prob: 0.75})
            if ((step % 10) == 0):
                (batch_x_test, batch_y_test) = self.get_batch(i, size=100)
                acc = sess.run(accuracy, feed_dict={self.X: batch_x_test, self.Y: batch_y_test, self.keep_prob: 1.0})
                print('第{}次训练 >>> 准确率为 {} >>> loss {}'.format(step, acc, cost_))
                if (acc > 0.99):
                    saver.save(sess, self.model_save_dir)
                    break
            if ((i % 500) == 0):
                saver.save(sess, self.model_save_dir)
            step += 1
        saver.save(sess, self.model_save_dir)