|
最近由于毕设的需要,想要做一个通过输入各种参数,预测冷热负荷的机器学习系统。可以在网页上直接输入房屋参数,预测出房屋所需冷热负荷和能耗水平。
关于机器学习那块相信网上已经有不少教程,这里就不再赘述,本文主要总结一下Django和heroku搭配来建立网站。
Django
这个是根据tutorial建立起来的网站的文件目录。
- mysite/
- manage.py
- mysite/
- __init__.py
- settings.py
- urls.py
- wsgi.py
- polls/
- __init__.py
- admin.py
- migrations/
- __init__.py
- 0001_initial.py
- models.py
- static/
- polls/
- images/
- background.gif
- style.css
- templates/
- polls/
- detail.html
- index.html
- results.html
- tests.py
- urls.py
- views.py
- templates/
- admin/
- base_site.html
复制代码 最顶层是mysite使我们的project,然后在这个project下面添加了一个app叫做polls。
在project下面又有一个文件夹叫mysite,里面放了一些setting,urls的重要文件。
url是指明Uniform Resource Locator的文件,即各种资源的索引位置。
wsgi.py是方便之后部署的文件。
在polls文件夹中,migration文件夹主要是负责数据库的连接。
models文件里定义了一些类,是我们数据库中会记录的东西。
views是我们给访问者呈现什么数据,它和templates是搭配的。
templates文件夹里,还有一个polls文件夹(虽然我自己后来建的时候放在polls文件夹中的html文件总是找不到就不再加这个polls子文件夹了,但是合理的情况是放在polls中的),存放着html格式的文件,和views搭配负责页面如何呈现。
其中在我自己的project里面最重要的是表单,即用户输入数据然后表单通过request接收,views.py处理,return回去。
这是我views.py文件的内容:
- # -*- coding: utf-8 -*-
- from django.shortcuts import render, render_to_response
- from django.views.decorators import csrf
- import tensorflow as tf
- import numpy as np
- def add_layer(inputs, insize, outsize, n, activation_function=None):
- layer_name = 'layer%s' % n
- with tf.name_scope(layer_name):
- Weights = tf.Variable(tf.random_normal([insize, outsize]), name='w')
- tf.summary.histogram(layer_name + 'Weights', Weights)
- bias = tf.Variable(tf.zeros([1, outsize]))
- tf.summary.histogram(layer_name + 'bias', bias)
- wx_b = tf.add(tf.matmul(inputs, Weights), bias)
- if activation_function is None:
- output = wx_b
- else:
- output = activation_function(wx_b, )
- return output
- def input(request):
- return render_to_response('get.html')
- def calculate(request):
- # xdata = [0.98, 514.50, 294.00, 110.25, 7.00, 2, 0.10, 1]
- request.encoding = 'utf-8'
- context = {}
- xdata = []
- if request.GET:
- for num in range(1, 9):
- xdata.append(float(request.GET['X%i' % num]))
- xdata = np.array(xdata).reshape([1, 8])
- xdata = 1.0 / (1 + np.exp(xdata))
- # print('xdata: ', xdata)
- # context['heatload'] = xdata[0]
- # context['coolload'] = xdata[2]
-
- tf.reset_default_graph()
- xs = tf.placeholder(tf.float32, xdata.shape, name='xinput')
- l1 = add_layer(xs, xdata.shape[1], 10, 1, activation_function=tf.nn.relu) # 10 is layer units
- l2 = add_layer(l1, 10, 10, 2, activation_function=tf.nn.relu) # 10 is layer units
- prediction = add_layer(l2, 10, 2, 3, activation_function=None) # predicted output
- saver = tf.train.Saver()
- with tf.Session() as sess:
- # you cannot initialize here
- saver.restore(sess, './my_net/save_net.ckpt')
- rlt = sess.run(prediction, feed_dict={xs: xdata})
- # print('result: ', rlt)
- context['heatload'] = rlt[0, 0]
- context['coolload'] = rlt[0, 1]
- print('context', context)
- return render(request, "result.html", context)
复制代码 get.html
- <!DOCTYPE html>
- <html lang="en" xmlns="http://www.w3.org/1999/html">
- <head>
- <meta charset="UTF-8">
- <title>Calculate the hear load and cool load</title>
- </head>
- <body>
- <form action = "/result/" method = "get">
- {% csrf_token %}
- Relative Compactness (surface-area-to-volume ratio): <input type="number" step = "any" name="X1" value = 0.764167> <br />
- Surface Area: <input type="number" name="X2" step = "any" value = 671.708333> <br />
- Wall Area: <input type="number" name="X3" step = "any" value = 318.50000> <br />
- Roof Area: <input type="number" name="X4" step = "any" value = 176.604167> <br />
- Overall Height: <input type="number" name="X5" step = "any" value = 5.25> <br />
- Orientation: <input type="number" name="X6" step = "any" value = 3.5> <br />
- Glazing Area: <input type="number" name="X7" step = "any" value = 0.234375> <br />
- Glazing Area Distribution: <input type="text" name="X8" step = "any" value = 2.812500> <br />
- <input type="submit" value="Submit">
- </form>
- <p></p>
- </body>
- </html>
复制代码 返回结果的页面get.html。里面还用到一些html判断语句来判断能耗等级。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>result</title>
- </head>
- <body>
- heat load: <p> {{ heatload }} BTU </p> <br />
- cool load: <p> {{ coolload }} BTU </p> <br />
- {% if heatload < 11.67 and coolload < 14.52 %}
- the efficiency classification: very low heating and cooling requirement
- {% elif 15.92 > heatload and heatload >= 11.67 and 18.65 > coolload and coolload >= 14.52 %}
- the efficiency classification: low heating and cooling requirement
- {% elif 26.27 > heatload and heatload >= 15.92 and 28.27 > coolload and coolload >= 18.65 %}
- the efficiency classification: medium heating and cooling requirement
- {% elif 32.32 > heatload and heatload >= 26.27 and 34.03 > coolload and coolload >= 28.27 %}
- the efficiency classification: high heating and cooling requirement
- {% else %}
- the efficiency classification: very high heating and cooling requirement
- {% endif %}
- </body>
- </html>
复制代码 然后下面这个是mysite/urls,是指明了url和views或者下面app的url关联关系
- from django.contrib import admin
- from django.urls import path, include
- from energy.views import calculate
- urlpatterns = [
- path('', include('energy.urls')),
- path('result/', calculate),
- path('admin/', admin.site.urls),
- ]
复制代码
|
|