티스토리 뷰

5. 파이썬

[텐서플로] 인공지능 계산기

패스트코드블로그 2020. 5. 16. 09:22

calculator.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
 
class Calculator:
    def create_adder(self):
        w1 = tf.placeholder(tf.float32, name='w1')
        w2 = tf.placeholder(tf.float32, name='w2')
        feed_dict = {'w1'8.0'w2'2.0}
        r = tf.add(w1, w2, name='op_add')
        sess = tf.Session()
        _ = tf.Variable(initial_value='fake_variable')
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        result = sess.run(r, {w1: feed_dict['w1'], w2: feed_dict['w2']})
        print('덧셈결과: {}'.format(result))
        saver.save(sess, './calc_add_model/model', global_step=1000)
 
    def create_subber(self):
        w1 = tf.placeholder(tf.float32, name='w1')
        w2 = tf.placeholder(tf.float32, name='w2')
        feed_dict = {'w1'8.0'w2'2.0}
        r = tf.subtract(w1, w2, name='op_sub')
        sess = tf.Session()
        _ = tf.Variable(initial_value='fake_variable')
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        result = sess.run(r, {w1: feed_dict['w1'], w2: feed_dict['w2']})
        print('뺄셈결과: {}'.format(result))
        saver.save(sess, './calc_sub_model/model', global_step=1000)
 
    def create_multifier(self):
        w1 = tf.placeholder(tf.float32, name='w1')
        w2 = tf.placeholder(tf.float32, name='w2')
        feed_dict = {'w1'8.0'w2'2.0}
        r = tf.multiply(w1, w2, name='op_mul')
        sess = tf.Session()
        _ = tf.Variable(initial_value='fake_variable')
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        result = sess.run(r, {w1: feed_dict['w1'], w2: feed_dict['w2']})
        print('곱셈결과: {}'.format(result))
        saver.save(sess, './calc_mul_model/model', global_step=1000)
 
    def create_divder(self):
        w1 = tf.placeholder(tf.float32, name='w1')
        w2 = tf.placeholder(tf.float32, name='w2')
        feed_dict = {'w1'8.0'w2'2.0}
        r = tf.divide(w1, w2, name='op_div')
        sess = tf.Session()
        _ = tf.Variable(initial_value='fake_variable')
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        result = sess.run(r, {w1: feed_dict['w1'], w2: feed_dict['w2']})
        print('나눗셈결과: {}'.format(result))
        saver.save(sess, './calc_div_model/model', global_step=1000)
 
 
def service(num1, num2, opcode):
    tf.reset_default_graph()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.import_meta_graph(f'model/calc_{opcode}_model/model-1000.meta')
        saver.restore(sess, tf.train.latest_checkpoint(f'model/calc_{opcode}_model'))
        graph = tf.get_default_graph()
        w1 = graph.get_tensor_by_name('w1:0')
        w2 = graph.get_tensor_by_name('w2:0')
        feed_dict = {w1: float(num1), w2: float(num2)}
        for key in feed_dict.keys():
            print(key, ':', feed_dict[key])
        op_to_restore = graph.get_tensor_by_name(f'op_{opcode}:0')
        result = sess.run(op_to_restore, feed_dict)
        print('텐서가 계산한 결과: {}'.format(result))
    return result
 
if __name__ == '__main__':
    c = Calculator()
    # c.create_adder()
    # c.create_subber()
    # c.create_multifier()
    c.create_divder()
cs

 

index.html

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>머신러닝 플라스크</title>
</head>
<body>
<h1>인덱스</h1>
<a href='/move/calculator'>계산기</a>
</body>
</html>
cs

 

calculator.html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>계산기</title>
</head>
<body>
<form action="/calc" method="post">
    첫번째 수: <br>
    <input type="text" name="num1"> <br>
    두번째 수: <br>
    <input type="text" name="num2"> <br>
    <select name="opcode" id="">
        <option value="add">+</option>
        <option value="sub">-</option>
        <option value="mul">*</option>
        <option value="div">/</option>
    </select>
    <br>
    <input type="submit" value="SUBMIT">
</form>
{% if result %}
<p>결과: {{result}}</p>
{% endif %}
<a href="/">홈으로</a>
</body>
</html>
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask
from flask import render_template, request, jsonify
 
app = Flask(__name__)
 
@app.route('/')
def index():
    return render_template('index.html')
 
@app.route('/move/calculator')
def move():
    return render_template('calculator.html')
 
@app.route('/calc', methods=["POST"])
def calc():
    num1 = request.form['num1']
    num2 = request.form['num2']
    opcode = request.form['opcode']
    print('넘어온 num1 값: {}'.format(num1))
    print('넘어온 num2 값: {}'.format(num2))
    print('넘어온 연산자 값: {}'.format(opcode))
    result = int(num1) + int(num2)
    render_params = {}
    render_params['result'= result
    return render_template('calculator.html'**render_params)
 
 
 
 
if __name__ == '__main__':
    app.debug = True
    app.run()
cs

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함